분류 전체보기
-
프로그래머스 - 명예의 전당알고리즘 2024. 4. 17. 01:08
import java.util.*; class Solution { public int[] solution(int k, int[] score) { int[] answer = new int[score.length]; PriorityQueue priorityQueue = new PriorityQueue(); int temp = 0; for(int i = 0; i k) { priorityQueue.poll(); } answer[i] = priorityQueue.peek(); } return answer; } } 명예의 전당 목록의 k 순위의 가장 최하위 점수를 찾는 문제입..
-
프로그래머스 - 콜라 문제알고리즘 2024. 4. 14. 00:45
class Solution { public int solution(int a, int b, int n) { int answer = 0; while(n >= a) { int bottle = (n / a) * b; int remainder = n % a; answer += bottle; n = bottle + remainder; } return answer; } } 마트에 콜라병을 가져갔을 때 받을 수 있는 병의 갯수(bottle)를 구하고, 가져가고 남은 갯수(remainder)를 구하여 값을 더 해주고 더 이상 나눌 수 없을 때 ( 콜라병을 받을 수 없는 ) 까지 반복하여 갯수를 구했습니다.
-
프로그래머스 - 푸드 파이트 대회알고리즘 2024. 4. 13. 12:19
class Solution { public String solution(int[] food) { StringBuilder sb = new StringBuilder(); StringBuilder answer = new StringBuilder(); String left = ""; String right = ""; for(int i = 1; i < food.length; i++) { int n = food[i] / 2; for(int j = 0; j < n; j++) sb.append(i); } left = sb.toString(); right = sb.reverse().toString(); answer.append(left).append("0").append(right); return answer.toSt..
-
프로그래머스 - 두 개 뽑아서 더하기알고리즘 2024. 4. 13. 11:19
import java.util.*; class Solution { public int[] solution(int[] numbers) { Set set = new HashSet(); for(int i = 0; i < numbers.length - 1; i++) { for(int j = i + 1; j < numbers.length; j++) { set.add(numbers[i] + numbers[j]); } } int[] answer = set.stream().mapToInt(Integer::intValue).toArray(); Arrays.sort(answer); return answer; } } 중복이 없는 Set을 이용하여, 배열을 순회해 두 수를 더해 set에 넣고 배열로 변환하여 정렬해 반환했습니다.
-
프로그래머스 - 비밀지도알고리즘 2024. 4. 12. 18:18
class Solution { public String[] solution(int n, int[] arr1, int[] arr2) { String[] answer = new String[n]; String[] binary1 = new String[n]; String[] binary2 = new String[n]; for(int i = 0; i < n; i++) binary1[i] = Integer.toString(arr1[i], 2); for(int i = 0; i < n; i++) binary2[i] = Integer.toString(arr2[i], 2); for(int i = 0; i < n; i++) { StringBuilder sb = new StringBuilder(); // ex) 9 = 10..
-
프로그래머스 - 문자열 내 마음대로 정렬하기알고리즘 2024. 4. 12. 17:44
import java.util.*; class Solution { public String[] solution(String[] strings, int n) { String[] answer = new String[strings.length]; for(int i = 0; i < strings.length; i++) { char c = strings[i].charAt(n); answer[i] = c + strings[i]; // 원하는 index의 문자를 가장 앞에 오도록 } Arrays.sort(answer); // 후에 정렬 for(int i = 0; i < answer.length; i++) { answer[i] = answer[i].substring(1, answer[i].length()); } ret..
-
프로그래머스 - 가장 가까운 같은 글자알고리즘 2024. 4. 12. 00:30
import java.util.*; class Solution { public int[] solution(String s) { int[] answer = new int[s.length()]; char[] arr = s.toCharArray(); Map map = new HashMap(); for(int i = 0; i < arr.length; i++) { if(!map.containsKey(arr[i])){ answer[i] = -1; map.put(arr[i], i); }else { int index = map.get(arr[i]); answer[i] = i - index; map.put(arr[i], i); } } return answer; } } String 을 char 배열로 변환하여, 각 글자를..
-
프로그래머스 - 최소 직사각형알고리즘 2024. 4. 12. 00:05
class Solution { public int solution(int[][] sizes) { int width = 0; // 큰 값 int height = 0; // 작은 값 for(int[] card : sizes) { int card_width = Math.max(card[0], card[1]); int card_height = Math.min(card[0], card[1]); if(width < card_width) width = card_width; if(height < card_height) height = card_height; } return width * height; } } 명함은 회전이 가능하기 때문에 큰 값을 가로로 작은 값을 세로로 정하여 값을 비교하여 넓이를 구했습니다.