본문 바로가기

알고리즘 & 자료구조569

영어 끝말잇기 package programmers; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class 영어끝말잇기 { // 1. 1번부터 번호 순서대로 한 사람씩 단어 말함 // 2. 마지막 사람이 말하면 다시 1번 시작 // 3. 앞 사람이 말한 단어의 마지막 문자로 시작하는 단어를 말함 // 4. 이전에 등장했던 단어는 사용할 수 없음 // 5. 한 글자 단어는 사용 불가 public static int[] solution(int n, String[] words) { List list = new ArrayList(); int idx = 0; list.add(words[0]); for (int i = 1; i.. 2023. 1. 4.
짝지어 제거하기 package programmers; import java.util.Stack; public class 짝지어제거하기 { public static int solution(String s) { Stack stack = new Stack(); int idx = 0; if (s.length() == 0) { return 0; } stack.push(s.charAt(idx++)); while (idx < s.length()) { char c = s.charAt(idx++); if (!stack.isEmpty() && stack.peek() == c) { stack.pop(); } else { stack.push(c); } } return stack.isEmpty() ? 1 : 0; } public static .. 2023. 1. 3.
카펫 package programmers; import java.util.Arrays; public class 카펫 { public static int[] solution(int brown, int yellow) { int sum = brown + yellow; return find(yellow, sum); } private static int[] find(int yellow, int sum) { int y = 0, x = 0; for (int i = 1; i 2023. 1. 2.
다음 큰 숫자 package programmers; public class 다음큰숫자 { //1. n의 다음 큰 숫자는 n보다 큰 자연수 //2. n의 다음 큰 숫자와 n은 2진수로 변환했을 때의 1의 갯수가 같다. //3. n의 다음 큰 숫자는 조건 1, 2,를 만족하는 수 중 가장 작은 수 // 78(1001110) 1 = 4개 0 3개 // 83(1010011) 1 = 4개 0 3개 public static int solution(int n) { int count = Integer.bitCount(n); return findNextBiggerNumber(count, n); } private static int findNextBiggerNumber(int count, int n) { while (true) { n+.. 2023. 1. 1.
피보나치 수 package programmers; public class 피보나치수 { public static int solution(int n) { int[] D = new int[n + 1]; D[0] = 0; D[1] = 1; for (int i = 2; i 2022. 12. 31.
숫자의 표현 package programmers; public class 숫자의표현 { public static int solution(int n) { int[] sum = new int[n + 1]; sum[0] = 0; for (int i = 1; i 2022. 12. 30.