본문 바로가기

알고리즘 & 자료구조569

H-Index package programmers; import java.util.Arrays; public class H_Index { //h-index = 논문 n 편중 h번 인용된 논문이 h편 이상이고, 나머지 논문이 h번 이하 인용되었되었을 때 최대값 //1 2023. 1. 11.
멀리 뛰기 package programmers; public class 멀리뛰기 { //한번에 1칸 or 2칸 //1 1 = 1 //2 2 = 1+1, 2 //3 3 = 1+1+1,1+2 2+1, //5 4 = 1+1+1+1, 1+2+1, 1+1+2, 2+1+1, 2+2 public static long solution(int n) { long[] d = new long[n + 1]; if (n == 1) return 1; if (n == 2) return 2; d[0] = 0; d[1] = 1L; d[2] = 2L; for (int i = 3; i 2023. 1. 10.
점프와 순간이동 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.