프로그래머스 (귤 고르기)
import java.util.*;
public class problem443 {
class Solution {
public int solution(int k, int[] tangerine) {
Map<Integer, Integer> map = new HashMap<>();
// map객체의 값 저장
for (int i = 0; i < tangerine.length; i++) {
map.put(tangerine[i], map.getOrDefault(tangerine[i], 0) + 1);
}
// map의 value값을 list에 저장
List<Integer> list = new ArrayList<>(map.values());
Collections.sort(list);
// 1 1 2 2 2
int sum = 0;
int count = 0;
for (int i = list.size() - 1; i >= 0; i--) {
sum += list.get(i);
count++;
if (sum >= k) {
break;
}
}
return count;
}
}
}
프로그래머스 (괄호 회전하기)
import java.util.Stack;
public class problem444 {
class Solution {
public int solution(String s) {
int answer = 0;
int length = s.length();
for (int i = 0; i < length; i++) {
Stack<Character> stack = new Stack<>();
for (char ch : s.toCharArray()) {
if (stack.isEmpty()) {
stack.push(ch);
} else if (ch == ')' && stack.peek() == '(') {
stack.pop();
} else if (ch == ']' && stack.peek() == '[') {
stack.pop();
} else if (ch == '}' && stack.peek() == '{') {
stack.pop();
} else {
stack.push(ch);
}
}
if (stack.isEmpty()) {
answer++;
}
// 문자열을 이동
s = s.substring(1) + s.charAt(0);
}
return answer;
}
}
}
프로그래머스 (H - Index)
import java.util.Arrays;
public class problem445 {
class Solution {
public int solution(int[] citations) {
Arrays.sort(citations);
int answer = 0;
// 0 1 3 5 6
for (int i = 0; i < citations.length; i++) {
int start = citations[i]; // 현재 논문 인용 횟수
int end = citations.length - i; // 논문의 갯수
if (start >= end) {
answer = Math.max(answer, end);
break;
}
}
return answer;
}
}
}