휴지통665 점프와 순간이동 package programmers; public class 점프와순간이동 { //K칸을 앞으로 점프하거나 //(현재까지온거리 x 2)에 해당하는 위치로 순간이동 // 순간이동을 하면 건전지 사용 x // 점프하면 K 만큼 건전지 사용 // N만큼 떨어진 거리로 이동 // 홀수, 짝수 이용 // 홀수 일때 1 추가 // 5000 -> 2500 -> 1250 -> 625 = 624 + 1 -> // 312 -> 156 -> 78 -> 39 = 38 + 1 -> // 19 = 18 +1 -> 9 = 8+1 -> 4 -> 2 -> // 1 = 0+1 -> 0 public static int solution(int n) { // return solution1(n); return Integer.bitCount(n.. 2023. 1. 8. 예상 대진표 package programmers; public class 예상대진표 { public static int solution(int n, int a, int b) { int round = 1; while (Math.abs(a - b) != 1 || a / 2 == b / 2) { a = nextNumber(a); b = nextNumber(b); round++; } return round; } public static int nextNumber(int x) { if (x % 2 == 1) { x = x / 2 + 1; } else { x /= 2; } return x; } public static void main(String[] args) { System.out.println(solution(8, 4, .. 2023. 1. 7. N개의 최소공배수 package programmers; public class N개의최소공배수 { public static int solution(int[] arr) { if (arr.length == 1) { return arr[0]; } int lcm = arr[0]; for (int i = 1; i < arr.length; i++) { int gcd = gcd(lcm, arr[i]); lcm = lcm(lcm, arr[i], gcd); } return lcm; } private static int lcm(int a, int b, int gcd) { return a * b / gcd; } private static int gcd(int a, int b) { int tmp = a % b; if (tmp == 0) { r.. 2023. 1. 6. 구명 보트 package programmers; import java.util.ArrayDeque; import java.util.Arrays; import java.util.Deque; public class 구명보트 { public static int solution(int[] people, int limit) { Arrays.sort(people); Deque deque = new ArrayDeque(50001); for (int person : people) { deque.add(person); } int answer = 0; while (!deque.isEmpty()) { int weight = deque.pollLast(); if (!deque.isEmpty() && weight + deque.peekF.. 2023. 1. 5. 영어 끝말잇기 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. 이전 1 ··· 18 19 20 21 22 23 24 ··· 111 다음