알고리즘
-
프로그래머스 - 덧칠하기알고리즘 2024. 4. 29. 11:33
class Solution { public int solution(int n, int m, int[] section) { int answer = 0; boolean[] wall = new boolean[n]; for(int i = 0; i 길이가 n인 벽을 배열로 선언해주고, 새롭게 덧칠을 해야하는 부분을 true로 변경 후, wall 배열을 탐색하여 true인 부분부터 m만큼 색을 다시 덧칠해주고 횟수를 증가시킵니다.이 때, 배열의 크기를 넘길 수 있으므로 조건을 걸어줍니다.
-
프로그래머스 - 모의고사알고리즘 2024. 4. 21. 08:11
import java.util.*; class Solution { public int[] solution(int[] answers) { int[] p1 = {1, 2, 3, 4, 5}; int[] p2 = {2, 1, 2, 3, 2, 4, 2, 5}; int[] p3 = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5}; int[] scores = new int[3]; int max = 0; List list = new ArrayList(); for(int i = 0; i < answers.length; i++) { if(answers[i] == p1[i % p1.length]) scores[0]++; if(answers[i] == p2[i % p2.length]) scores[1]++; if(an..
-
프로그래머스 - 명예의 전당알고리즘 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..