본문 바로가기

Java66

[Java] 프로그래머스 [Level-2] 올바른 괄호 import java.util.LinkedList; import java.util.Queue; public class Solution { // 올바른 괄호 static boolean solution(String s) { boolean answer = true; char tempChar; Queue q = new LinkedList(); for (int i = 0; i < s.length(); i++) { tempChar = s.charAt(i); if (tempChar == '(') { // '('라면 q.offer(tempChar); // 큐에 넣기 } else { // ')'라면 if (q.isEmpty()) { // 이 시점에 큐가 비어있는 상태라면 짝이 맞지 않는 것이므로 answer = fals.. 2022. 11. 28.
[Java] 프로그래머스 [Level-2] 구명보트 import java.util.Arrays; public class Solution { // 구명보트 public static int solution(int[] people, int limit) { int answer = 0; Arrays.sort(people); // 50, 50, 70, 80 int maxWeightIndex = people.length - 1; // 구조되지 않은 사람 중 가장 무거운 무게를 가진 사람의 인덱스 int minWeightIndex = 0; // 구조되지 않은 사람 중 가장 가벼운 무게를 가진 사람의 인덱스 for (int i = maxWeightIndex; i >= 0; i--) { // 구조되지 않은 사람 중 가장 무거운 무게를 가진 사람부터 순서대로 구조 answe.. 2022. 11. 27.
[Java] 프로그래머스 [Level-4] 행렬과 연산 import java.util.ArrayDeque; import java.util.Deque; public class Solution { // 행렬과 연산 // Deque 자료구조에서 // add(A) + peek(B) or remove(B)가 있을 때 // (A)와 (B)는 First 또는 Last // (A)와 (B)가 같다면 스택(Stack)처럼 동작 // (A)와 (B)가 다르다면 큐(Queue)처럼 동작 // addFirst로 쌓고 있는 구조에서 제일 앞에 원소를 추가하고 싶다면 addLast로 추가 // addLast로 쌓고 있는 구조에서 제일 앞에 원소를 추가하고 싶다면 addFirst로 추가 static int r = 0; // rc의 행 수 static int c = 0; // rc의 열.. 2022. 11. 27.
[Java] 프로그래머스 [Level-2] 두 큐 합 같게 만들기 import java.util.Arrays; import java.util.LinkedList; import java.util.Queue; public class Solution { // 두 큐 합 같게 만들기 public static int solution(int[] queue1, int[] queue2) { int answer = 0; int length = queue1.length; long queue1Sum = 0; // queue1의 합계만을 사용할 것 long totalSum = 0; long targetSum = 0; Queue q1 = new LinkedList(); Queue q2 = new LinkedList(); queue1Sum = Arrays.stream(queue1).sum();.. 2022. 11. 27.
[Java] 프로그래머스 [Level-1] 성격 유형 검사하기 import java.util.*; public class Solution { //성격 유형 검사하기 static String[] typeArr = {"RT", "CF", "JM", "AN"}; public static String typeCheck(HashMap hm) { StringBuilder sb = new StringBuilder(); char firstChar; char secondChar; char typeChar; for (String s : typeArr) { // "RT", "CF", "JM", "AN" firstChar = s.charAt(0); // 'R', 'C', 'J', 'A' secondChar = s.charAt(1);// 'T', 'F', 'M', 'N' typeChar .. 2022. 11. 27.
[Java] 프로그래머스 [Level-4] 도둑질 두 번째 시도(시간 초과) public class Solution { // 도둑질 // 두 번째 시도(시간 초과) public static int getMaxMoneyExceptTargetIndex(int[] money, int targetIndex) { int[] dp = new int[money.length - 1]; // 제일 먼저 1을 뺐을 경우 2,3,1,4,2,5에서 개미전사 문제 int num1 = (targetIndex + 1) % (money.length); // 타겟 인덱스의 바로 다음 인덱스 int num2 = (targetIndex + 2) % (money.length); // 타겟 인덱스의 다음 다음 인덱스 int num3 = 0; dp[0] = money[num1]; dp[1] .. 2022. 11. 27.