본문 바로가기

알고리즘 & 자료구조569

예산 package programmers; import java.util.Arrays; public class 예산2 { public static int solution(int[] budgets, int M) { return binarySearch(budgets, M); } private static int binarySearch(int[] budgets, int M) { int left = 0; int right = 0; int answer = 0; //참고자료 //https://jypthemiracle.medium.com/java-stream-api%EB%8A%94-%EC%99%9C-for-loop%EB%B3%B4%EB%8B%A4-%EB%8A%90%EB%A6%B4%EA%B9%8C-50dec4b9974b /.. 2022. 11. 19.
가장 큰 수 package programmers; import java.util.Arrays; public class 가장큰수 { public static String solution(int[] numbers) { // 1. int[] array to String[] array String[] numbersArray = Arrays.stream(numbers).mapToObj(String::valueOf) .toArray(String[]::new); // 2. 정렬 기준 (합친숫자가 큰 기준으로, DESC) Arrays.sort(numbersArray, (o1, o2) -> (o2 + o1).compareTo(o1 + o2)); // 3. 정렬 후 가장 큰 값이 0이면 배열은 0으로만 이루어져 있다. if (numb.. 2022. 11. 17.
기지국 설치 package programmers; public class 기지국설치 { public static int solution(int n, int[] stations, int w) { int spreadRange = 2 * w + 1; // 전파 최대 범위 int answer = 0; int idx = 1; // 탐색 시작 idx for (int station : stations) { if (idx < station - w) {// 설치되어있는 기지국 전파범위보다 idx가 작을 경우 int start = station - w; // 전파범위 시작점 answer += getCount(start - idx, spreadRange); } idx = station + w + 1; // 설치되어 있는 기지국 전파범위.. 2022. 11. 16.
성격 유형 검사하기 package programmers; import java.util.HashMap; import java.util.Map; class 성격유형검사하기 { public String solution(String[] survey, int[] choices) { int n = survey.length; Map map = new HashMap(); initMap(map); for (int i = 0; i 4) { String s = survey.charAt(1) + ""; int ju.. 2022. 11. 15.
숫자 짝궁 package programmers; public class 숫자짝꿍 { public static String solution(String X, String Y) { int[] countX = new int[10]; int[] countY = new int[10]; getNumberCount(X, countX); getNumberCount(Y, countY); StringBuilder sb = new StringBuilder(); for (int i = 0; i < 10; i++) { if (isNotZeroAndEquals(countX, countY, i)) { sb.append(i); countX[i]--; countY[i]--; i = -1; } } if (sb.length() == 0) { ret.. 2022. 11. 13.
푸드 파이트 대회 package programmers; public class 푸드파이트대회 { public static String solution(int[] food) { StringBuilder sb = new StringBuilder(); for (int i = 1; i < food.length; i++) { for (int j = 1; j < food[i]; j += 2) { sb.append(i); } } String answer = sb + "0"; answer += sb.reverse(); return answer; } public static void main(String[] args) { System.out.println(solution(new int[] {1, 3, 4, 6})); System.out... 2022. 11. 11.