From 5acfc12802b3f5283a790410bc2f0bca7ee4dd08 Mon Sep 17 00:00:00 2001 From: ndb796 Date: Sun, 9 Aug 2020 11:10:59 +0900 Subject: [PATCH] Update --- 11/1.java | 31 ++++++++++++++++++++++++ 11/2.java | 25 ++++++++++++++++++++ 11/3.java | 33 ++++++++++++++++++++++++++ 11/4.java | 27 +++++++++++++++++++++ 11/5.java | 29 +++++++++++++++++++++++ 11/6.java | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 14/1.java | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 14/2.java | 19 +++++++++++++++ 14/3.java | 62 ++++++++++++++++++++++++++++++++++++++++++++++++ 14/4.java | 30 ++++++++++++++++++++++++ 16/1.java | 50 +++++++++++++++++++++++++++++++++++++++ 16/2.java | 36 ++++++++++++++++++++++++++++ 16/3.java | 35 ++++++++++++++++++++++++++++ 16/4.java | 41 ++++++++++++++++++++++++++++++++ 16/5.java | 40 +++++++++++++++++++++++++++++++ 16/6.java | 50 +++++++++++++++++++++++++++++++++++++++ 16 files changed, 645 insertions(+) diff --git a/11/1.java b/11/1.java index e69de29..a6db606 100644 --- a/11/1.java +++ b/11/1.java @@ -0,0 +1,31 @@ +import java.util.*; + +public class Main { + + public static int n; + public static ArrayList arrayList = new ArrayList<>(); + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + n = sc.nextInt(); + + for (int i = 0; i < n; i++) { + arrayList.add(sc.nextInt()); + } + + Collections.sort(arrayList); + + int result = 0; // 총 그룹의 수 + int count = 0; // 현재 그룹에 포함된 모험가의 수 + + for (int i = 0; i < n; i++) { // 공포도를 낮은 것부터 하나씩 확인하며 + count += 1; // 현재 그룹에 해당 모험가를 포함시키기 + if (count >= arrayList.get(i)) { // 현재 그룹에 포함된 모험가의 수가 현재의 공포도 이상이라면, 그룹 결성 + result += 1; // 총 그룹의 수 증가시키기 + count = 0; // 현재 그룹에 포함된 모험가의 수 초기화 + } + } + + System.out.println(result); + } +} \ No newline at end of file diff --git a/11/2.java b/11/2.java index e69de29..e77eae5 100644 --- a/11/2.java +++ b/11/2.java @@ -0,0 +1,25 @@ +import java.util.*; + +public class Main { + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + String str = sc.next(); + + // 첫 번째 문자를 숫자로 변경한 값을 대입 + long result = str.charAt(0) - '0'; + + for (int i = 1; i < str.length(); i++) { + // 두 수 중에서 하나라도 '0' 혹은 '1'인 경우, 곱하기보다는 더하기 수행 + int num = str.charAt(i) - '0'; + if (num <= 1 || result <= 1) { + result += num; + } + else { + result *= num; + } + } + + System.out.println(result); + } +} \ No newline at end of file diff --git a/11/3.java b/11/3.java index e69de29..d757686 100644 --- a/11/3.java +++ b/11/3.java @@ -0,0 +1,33 @@ +import java.util.*; + +public class Main { + + public static String str; + public static int count0 = 0; // 전부 0으로 바꾸는 경우 + public static int count1 = 0; // 전부 1로 바꾸는 경우 + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + str = sc.next(); + + // 첫 번째 원소에 대해서 처리 + if (str.charAt(0) == '1') { + count0 += 1; + } + else { + count1 += 1; + } + + // 두 번째 원소부터 모든 원소를 확인하며 + for (int i = 0; i < str.length() - 1; i++) { + if (str.charAt(i) != str.charAt(i + 1)) { + // 다음 수에서 1로 바뀌는 경우 + if (str.charAt(i + 1) == '1') count0 += 1; + // 다음 수에서 0으로 바뀌는 경우 + else count1 += 1; + } + } + + System.out.println(Math.min(count0, count1)); + } +} \ No newline at end of file diff --git a/11/4.java b/11/4.java index e69de29..7d8adbb 100644 --- a/11/4.java +++ b/11/4.java @@ -0,0 +1,27 @@ +import java.util.*; + +public class Main { + + public static int n; + public static ArrayList arrayList = new ArrayList<>(); + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + n = sc.nextInt(); + + for (int i = 0; i < n; i++) { + arrayList.add(sc.nextInt()); + } + + Collections.sort(arrayList); + + int target = 1; + for (int i = 0; i < n; i++) { + // 만들 수 없는 금액을 찾았을 때 반복 종료 + if (target < arrayList.get(i)) break; + target += arrayList.get(i); + } + + System.out.println(target); + } +} \ No newline at end of file diff --git a/11/5.java b/11/5.java index e69de29..98b58d4 100644 --- a/11/5.java +++ b/11/5.java @@ -0,0 +1,29 @@ +import java.util.*; + +public class Main { + + public static int n, m; + // 1부터 10까지의 무게를 담을 수 있는 배열 + public static int[] arr = new int[11]; + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + n = sc.nextInt(); + m = sc.nextInt(); + + for (int i = 0; i < n; i++) { + int x = sc.nextInt(); + arr[x] += 1; + } + + int result = 0; + + // 1부터 m까지의 각 무게에 대하여 처리 + for (int i = 1; i <= m; i++) { + n -= arr[i]; // 무게가 i인 볼링공의 개수(A가 선택할 수 있는 개수) 제외 + result += arr[i] * n; // B가 선택하는 경우의 수와 곱해주기 + } + + System.out.println(result); + } +} \ No newline at end of file diff --git a/11/6.java b/11/6.java index e69de29..33fe539 100644 --- a/11/6.java +++ b/11/6.java @@ -0,0 +1,70 @@ +import java.util.*; + +class Food implements Comparable { + + private int time; + private int index; + + public Food(int time, int index) { + this.time = time; + this.index = index; + } + + public int getTime() { + return this.time; + } + + public int getIndex() { + return this.index; + } + + // 시간이 짧은 것이 높은 우선순위를 가지도록 설정 + @Override + public int compareTo(Food other) { + return Integer.compare(this.time, other.time); + } +} + +class Solution { + public int solution(int[] food_times, long k) { + // 전체 음식을 먹는 시간보다 k가 크거나 같다면 -1 + long summary = 0; + for (int i = 0; i < food_times.length; i++) { + summary += food_times[i]; + } + if (summary <= k) return -1; + + // 시간이 작은 음식부터 빼야 하므로 우선순위 큐를 이용 + PriorityQueue pq = new PriorityQueue<>(); + for (int i = 0; i < food_times.length; i++) { + // (음식 시간, 음식 번호) 형태로 우선순위 큐에 삽입 + pq.offer(new Food(food_times[i], i + 1)); + } + + summary = 0; // 먹기 위해 사용한 시간 + long previous = 0; // 직전에 다 먹은 음식 시간 + long length = food_times.length; // 남은 음식의 개수 + + // summary + (현재의 음식 시간 - 이전 음식 시간) * 현재 음식 개수와 k 비교 + while (summary + ((pq.peek().getTime() - previous) * length) <= k) { + int now = pq.poll().getTime(); + summary += (now - previous) * length; + length -= 1; // 다 먹은 음식 제외 + previous = now; // 이전 음식 시간 재설정 + } + + // 남은 음식 중에서 몇 번째 음식인지 확인하여 출력 + ArrayList result = new ArrayList<>(); + while (!pq.isEmpty()) { + result.add(pq.poll()); + } + // 음식의 번호 기준으로 정렬 + Collections.sort(result, new Comparator() { + @Override + public int compare(Food a, Food b) { + return Integer.compare(a.getIndex(), b.getIndex()); + } + }); + return result.get((int) ((k - summary) % length)).getIndex(); + } +} \ No newline at end of file diff --git a/14/1.java b/14/1.java index e69de29..7c1a206 100644 --- a/14/1.java +++ b/14/1.java @@ -0,0 +1,67 @@ +import java.util.*; + +class Student implements Comparable { + + private String name; + private int kor; + private int eng; + private int m; + + public Student(String name, int kor, int eng, int m) { + this.name = name; + this.kor = kor; + this.eng = eng; + this.m = m; + } + + /* + [ 정렬 기준 ] + 1) 두 번째 원소를 기준으로 내림차순 정렬 + 2) 두 번째 원소가 같은 경우, 세 번째 원소를 기준으로 오름차순 정렬 + 3) 세 번째 원소가 같은 경우, 네 번째 원소를 기준으로 내림차순 정렬 + 4) 네 번째 원소가 같은 경우, 첫 번째 원소를 기준으로 오름차순 정렬 + */ + + public String getName() { + return this.name; + } + + // 정렬 기준은 '점수가 낮은 순서' + @Override + public int compareTo(Student other) { + if (this.kor == other.kor && this.eng == other.eng && this.m == other.m) { + return this.name.compareTo(other.name); + } + if (this.kor == other.kor && this.eng == other.eng) { + return Integer.compare(other.m, this.m); + } + if (this.kor == other.kor) { + return Integer.compare(this.eng, other.eng); + } + return Integer.compare(other.kor, this.kor); + } +} + +public class Main { + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int n = sc.nextInt(); + + ArrayList students = new ArrayList<>(); + for (int i = 0; i < n; i++) { + String name = sc.next(); + int kor = sc.nextInt(); + int eng = sc.nextInt(); + int m = sc.nextInt(); + students.add(new Student(name, kor, eng, m)); + } + + Collections.sort(students); + + // 정렬된 학생 정보에서 이름만 출력 + for (int i = 0; i < n; i++) { + System.out.println(students.get(i).getName()); + } + } +} \ No newline at end of file diff --git a/14/2.java b/14/2.java index e69de29..5147380 100644 --- a/14/2.java +++ b/14/2.java @@ -0,0 +1,19 @@ +import java.util.*; + +public class Main { + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int n = sc.nextInt(); + + ArrayList arrayList = new ArrayList<>(); + for (int i = 0; i < n; i++) { + arrayList.add(sc.nextInt()); + } + + Collections.sort(students); + + // 중간값(median)을 출력 + System.out.println(v[(n - 1) / 2]); + } +} \ No newline at end of file diff --git a/14/3.java b/14/3.java index e69de29..9c51311 100644 --- a/14/3.java +++ b/14/3.java @@ -0,0 +1,62 @@ +import java.util.*; + +class Node implements Comparable { + + private int stage; + private double fail; + + public Node(int stage, double fail) { + this.stage = stage; + this.fail = fail; + } + + public int getStage() { + return this.stage; + } + + @Override + public int compareTo(Node other) { + if (this.fail == other.fail) { + return Integer.compare(this.stage, other.stage); + } + return Double.compare(other.fail, this.fail); + } +} + +class Solution { + public int[] solution(int N, int[] stages) { + int[] answer = new int[N]; + ArrayList arrayList = new ArrayList<>(); + int length = stages.length; + + // 스테이지 번호를 1부터 N까지 증가시키며 + for (int i = 1; i <= N; i++) { + // 해당 스테이지에 머물러 있는 사람의 수 계산 + int cnt = 0; + for (int j = 0; j < stages.length; j++) { + if (stages[j] == i) { + cnt += 1; + } + } + + // 실패율 계산 + double fail = 0; + if (length >= 1) { + fail = (double) cnt / length; + } + + // 리스트에 (스테이지 번호, 실패율) 원소 삽입 + arrayList.add(new Node(i, fail)); + length -= cnt; + } + + // 실패율을 기준으로 각 스테이지를 내림차순 정렬 + Collections.sort(arrayList); + + // 정렬된 스테이지 번호 반환 + for (int i = 0; i < N; i++) { + answer[i] = arrayList.get(i).getStage(); + } + return answer; + } +} \ No newline at end of file diff --git a/14/4.java b/14/4.java index e69de29..c3919c0 100644 --- a/14/4.java +++ b/14/4.java @@ -0,0 +1,30 @@ +import java.util.*; + +public class Main { + + public static int n, result; + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + n = sc.nextInt(); + + PriorityQueue pq = new PriorityQueue<>(); + // 힙(Heap)에 초기 카드 묶음을 모두 삽입 + for (int i = 0; i < n; i++) { + pq.offer(sc.nextInt()); + } + + // 힙(Heap)에 원소가 1개 남을 때까지 + while (pq.size() != 1) { + // 가장 작은 2개의 카드 묶음 꺼내기 + int one = pq.poll(); + int two = pq.poll(); + // 카드 묶음을 합쳐서 다시 삽입 + int summary = one + two; + result += summary; + pq.offer(summary); + } + + System.out.println(result); + } +} \ No newline at end of file diff --git a/16/1.java b/16/1.java index e69de29..e82761d 100644 --- a/16/1.java +++ b/16/1.java @@ -0,0 +1,50 @@ +import java.util.*; + +public class Main { + + static int testCase, n, m; + static int[][] arr = new int[20][20]; + static int[][] dp = new int[20][20]; + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + // 테스트 케이스(Test Case) 입력 + testCase = sc.nextInt(); + for (int tc = 0; tc < testCase; tc++) { + // 금광 정보 입력 + n = sc.nextInt(); + m = sc.nextInt(); + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + arr[i][j] = sc.nextInt(); + } + } + // 다이나믹 프로그래밍을 위한 2차원 DP 테이블 초기화 + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + dp[i][j] = arr[i][j]; + } + } + // 다이나믹 프로그래밍 진행 + for (int j = 1; j < m; j++) { + for (int i = 0; i < n; i++) { + int leftUp, leftDown, left; + // 왼쪽 위에서 오는 경우 + if (i == 0) leftUp = 0; + else leftUp = dp[i - 1][j - 1]; + // 왼쪽 아래에서 오는 경우 + if (i == n - 1) leftDown = 0; + else leftDown = dp[i + 1][j - 1]; + // 왼쪽에서 오는 경우 + left = dp[i][j - 1]; + dp[i][j] = dp[i][j] + Math.max(leftUp, Math.max(leftDown, left)); + } + } + int result = 0; + for (int i = 0; i < n; i++) { + result = Math.max(result, dp[i][m - 1]); + } + System.out.println(result); + } + } +} \ No newline at end of file diff --git a/16/2.java b/16/2.java index e69de29..a480f1f 100644 --- a/16/2.java +++ b/16/2.java @@ -0,0 +1,36 @@ +import java.util.*; + +public class Main { + + static int n; + static int[][] dp = new int[500][500]; // 다이나믹 프로그래밍을 위한 DP 테이블 초기화 + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + n = sc.nextInt(); + for (int i = 0; i < n; i++) { + for (int j = 0; j < i + 1; j++) { + dp[i][j] = sc.nextInt(); + } + } + // 다이나믹 프로그래밍으로 2번째 줄부터 내려가면서 확인 + for (int i = 1; i < n; i++) { + for (int j = 0; j <= i; j++) { + int upLeft, up; + // 왼쪽 위에서 내려오는 경우 + if (j == 0) upLeft = 0; + else upLeft = dp[i - 1][j - 1]; + // 바로 위에서 내려오는 경우 + if (j == i) up = 0; + else up = dp[i - 1][j]; + // 최대 합을 저장 + dp[i][j] = dp[i][j] + Math.max(upLeft, up); + } + } + int result = 0; + for (int i = 0; i < n; i++) { + result = Math.max(result, dp[n - 1][i]); + } + System.out.println(result); + } +} \ No newline at end of file diff --git a/16/3.java b/16/3.java index e69de29..e848ab7 100644 --- a/16/3.java +++ b/16/3.java @@ -0,0 +1,35 @@ +import java.util.*; + +public class Main { + + static int n; // 전체 상담 개수 + static int[] t = new int[15]; // 각 상담을 완료하는데 걸리는 기간 + static int[] p = new int[15]; // 각 상담을 완료했을 때 받을 수 있는 금액 + static int[] dp = new int[16]; // 다이나믹 프로그래밍을 위한 1차원 DP 테이블 초기화 + static int maxValue; + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + n = sc.nextInt(); + + for (int i = 0; i < n; i++) { + t[i] = sc.nextInt(); + p[i] = sc.nextInt(); + } + + // 배열을 뒤에서부터 거꾸로 확인 + for (int i = n - 1; i >= 0; i--) { + int time = t[i] + i; + // 상담이 기간 안에 끝나는 경우 + if (time <= n) { + // 점화식에 맞게, 현재까지의 최고 이익 계산 + dp[i] = Math.max(p[i] + dp[time], maxValue); + maxValue = dp[i]; + } + // 상담이 기간을 벗어나는 경우 + else dp[i] = maxValue; + } + + System.out.println(maxValue); + } +} \ No newline at end of file diff --git a/16/4.java b/16/4.java index e69de29..27f5c9f 100644 --- a/16/4.java +++ b/16/4.java @@ -0,0 +1,41 @@ +import java.util.*; + +public class Main { + + static int n; + static ArrayList v = new ArrayList(); + static int[] dp = new int[2000]; + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + n = sc.nextInt(); + + for (int i = 0; i < n; i++) { + v.add(sc.nextInt()); + } + + // 순서를 뒤집어 '최장 증가 부분 수열' 문제로 변환 + Collections.reverse(v); + + // 다이나믹 프로그래밍을 위한 1차원 DP 테이블 초기화 + for (int i = 0; i < n; i++) { + dp[i] = 1; + } + + // 가장 긴 증가하는 부분 수열(LIS) 알고리즘 수행 + for (int i = 1; i < n; i++) { + for (int j = 0; j < i; j++) { + if (v.get(j) < v.get(i)) { + dp[i] = Math.max(dp[i], dp[j] + 1); + } + } + } + + // 열외해야 하는 병사의 최소 수를 출력 + int maxValue = 0; + for (int i = 0; i < n; i++) { + maxValue = Math.max(maxValue, dp[i]); + } + System.out.println(n - maxValue); + } +} \ No newline at end of file diff --git a/16/5.java b/16/5.java index e69de29..f5212b8 100644 --- a/16/5.java +++ b/16/5.java @@ -0,0 +1,40 @@ +import java.util.*; + +public class Main { + + static int n; + static int[] ugly = new int[1000]; // 못생긴 수를 담기 위한 테이블 (1차원 DP 테이블) + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + n = sc.nextInt(); + + // 2배, 3배, 5배를 위한 인덱스 + int i2 = 0, i3 = 0, i5 = 0; + // 처음에 곱셈 값을 초기화 + int next2 = 2, next3 = 3, next5 = 5; + + ugly[0] = 1; // 첫 번째 못생긴 수는 1 + // 1부터 n까지의 못생긴 수들을 찾기 + for (int l = 1; l < n; l++) { + // 가능한 곱셈 결과 중에서 가장 작은 수를 선택 + ugly[l] = Math.min(next2, Math.min(next3, next5)); + // 인덱스에 따라서 곱셈 결과를 증가 + if (ugly[l] == next2) { + i2 += 1; + next2 = ugly[i2] * 2; + } + if (ugly[l] == next3) { + i3 += 1; + next3 = ugly[i3] * 3; + } + if (ugly[l] == next5) { + i5 += 1; + next5 = ugly[i5] * 5; + } + } + + // n번째 못생긴 수를 출력 + System.out.println(ugly[n - 1]); + } +} \ No newline at end of file diff --git a/16/6.java b/16/6.java index e69de29..4e44b24 100644 --- a/16/6.java +++ b/16/6.java @@ -0,0 +1,50 @@ +import java.util.*; + +public class Main { + + static String str1; + static String str2; + + // 최소 편집 거리(Edit Distance) 계산을 위한 다이나믹 프로그래밍 + static int editDist(String str1, String str2) { + int n = str1.length(); + int m = str2.length(); + + // 다이나믹 프로그래밍을 위한 2차원 DP 테이블 초기화 + int[][] dp = new int[n + 1][m + 1]; + + // DP 테이블 초기 설정 + for (int i = 1; i <= n; i++) { + dp[i][0] = i; + } + for (int j = 1; j <= m; j++) { + dp[0][j] = j; + } + + // 최소 편집 거리 계산 + for (int i = 1; i <= n; i++) { + for (int j = 1; j <= m; j++) { + // 문자가 같다면, 왼쪽 위에 해당하는 수를 그대로 대입 + if (str1.charAt(i - 1) == str2.charAt(j - 1)) { + dp[i][j] = dp[i - 1][j - 1]; + } + // 문자가 다르다면, 세 가지 경우 중에서 최솟값 찾기 + else { // 삽입(왼쪽), 삭제(위쪽), 교체(왼쪽 위) 중에서 최소 비용을 찾아 대입 + dp[i][j] = 1 + Math.min(dp[i][j - 1], Math.min(dp[i - 1][j], dp[i - 1][j - 1])); + } + } + } + + return dp[n][m]; + } + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + String str1 = sc.next(); + String str2 = sc.next(); + + // 최소 편집 거리 출력 + System.out.println(editDist(str1, str2)); + } +} \ No newline at end of file