- Today
- Total
- MST
- λ°±μλ
- ꡬν
- java
- tree
- λ€μ΅μ€νΈλΌ
- Graph
- μλ£κ΅¬μ‘°
- 벨λ§ν¬λ
- array
- μΈν΄
- spring
- λ¬Έλ²
- μμμ λ ¬
- λ°±μ€
- database
- CS
- BFS
- νλ‘κ·Έλλ¨Έμ€
- μλ°
- OOP
- μλ°μμ μ
- λ°μ΄ν°λ² μ΄μ€
- leetcode
- Algorithm
- pytorch
- μ‘Έμ μν
- 그리λ
- PS
- dp
Partially Committed
[λ°±μ€ 18870/2565/2470/4195] μλ° λ³Έλ¬Έ
(title: [λ°±μ€ 18770/2565/2470/4195] μλ°)
A. μ’νμμΆ BOJ 18870
https://www.acmicpc.net/problem/18870
λμ΄λ | Silver 2 |
νμ΄ μκ° | 10 λΆ |
λΆλ₯ | μ λ ¬, Hash map |
μκ°λ³΅μ‘λ | 1. μ
λ ₯μΌλ‘ μ£Όμ΄μ§λ n κ°μ μ«μλ₯Ό λ°°μ΄μ μ μ₯νλ λΆλΆ O(n) 2. μμ ν λΉμ μν temp λ°°μ΄μ μ λ ¬νλ λΆλΆ O(nlogn) 3. hash map μ temp λ°°μ΄μ μμλ₯Ό μΆκ°νλ©΄μ μ€λ³΅λ κ°μ κ±Έλ¬λ΄λ λΆλΆμ temp λ°°μ΄μ μ²μλΆν° λκΉμ§ μμ°¨νμνλ―λ‘ O(n) βΆ O(n+nlogn+n) ~= O(nlogn) |
곡κ°λ³΅μ‘λ | 1. arr λ°°μ΄ O(n) 2. temp λ°°μ΄ O(n) 3. hash map O(n) βΆ O(n+n+n) ~= O(n) |
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
System.setIn(new FileInputStream("src/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer stk;
int n = Integer.parseInt(br.readLine());
Map<Integer, Integer> hm = new HashMap<>();
int [] arr = new int[n];
Integer [] temp = new Integer[n];
stk = new StringTokenizer(br.readLine());
for(int i = 0 ; i < n ; i++){
arr[i] = Integer.parseInt(stk.nextToken());
temp[i] = arr[i];
}
Arrays.sort(temp); int rank = 0;
for(int i = 0; i < n; i++){
if(hm.containsKey(temp[i])){
continue;
} else {
hm.put(temp[i], rank++);
}
}
for(int i = 0 ; i < n ; i++){
bw.write(hm.get(arr[i])+" ");
} bw.write("\n");
bw.flush();
bw.close();
}
}
B. μ κΉμ€ BOJ 2565
https://www.acmicpc.net/problem/2565
λμ΄λ | Gold 5 |
νμ΄ μκ° | 30 λΆ |
λΆλ₯ | LIS, DP |
μκ°λ³΅μ‘λ | O(n^2) ; n <= 100 μ΄λΌ κ°λ₯ n μ΄ ν¬λ€λ©΄, binary search λ₯Ό μ΄μ©ν nlogn LIS νμ΄λ₯Ό μ¬μ©ν΄μΌ ν¨. |
곡κ°λ³΅μ‘λ | O(n) |
import java.io.*;
import java.util.*;
public class Main {
static int n;
static ArrayList<link> arr;
static class link implements Comparable<link>{
int left;
int right;
public link(int left, int right) {
this.left = left;
this.right = right;
}
@Override
public int compareTo(link o) {
if(this.left > o.left)
return 1;
else return -1;
}
}
public static void main(String[] args) throws IOException {
System.setIn(new FileInputStream("src/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer stk;
n = Integer.parseInt(br.readLine());
arr = new ArrayList<>();
for(int i = 0 ; i < n; i++){
stk = new StringTokenizer(br.readLine());
int a = Integer.parseInt(stk.nextToken());
int b = Integer.parseInt(stk.nextToken());
arr.add(new link(a, b));
}
int [] dp = new int[n];
for(int i = 0 ; i < n; i++){
dp[i] = 1;
}
Collections.sort(arr);
int max = -1;
for(int i = 0; i <n; i++){
for(int j = 0 ; j < i ; j++){
if(arr.get(j).right < arr.get(i).right){
dp[i] = Math.max(dp[i], dp[j]+1);
max = Math.max(max, dp[i]);
}
}
}
int ans = n-max;
bw.write(ans+"\n");
bw.flush();
bw.close();
}
}
C. λ μ©μ‘ BOJ 2470
https://www.acmicpc.net/problem/2470
λμ΄λ | Gold 5 |
νμ΄ μκ° | 45λΆ |
λΆλ₯ | ν¬ ν¬μΈν° |
μκ°λ³΅μ‘λ | O(nlogn) |
곡κ°λ³΅μ‘λ | O(n) |
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
System.setIn(new FileInputStream("src/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer stk;
int n = Integer.parseInt(br.readLine());
int[] arr = new int[n];
int[] ans = new int[2];
stk = new StringTokenizer(br.readLine());
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(stk.nextToken());
}
Arrays.sort(arr); // nlogn
int left = 0;
int right = n - 1;
int val = Integer.MAX_VALUE;
int t = 0;
while (left < right) {
t = Math.abs(arr[left]+arr[right]);
if(t<val){
val = t;
ans[0] = arr[left];
ans[1] = arr[right];
}
if(arr[left]+arr[right]<0){
left++;
} else {
right--;
}
}
bw.write(ans[0]+" "+ans[1]+"\n");
bw.flush();
bw.close();
}
}
D. μΉκ΅¬ λ€νΈμν¬ BOJ 4195
μ΄ λ¬Έμ λ κΈ°μ΅ν΄λλ§ν ν¬μΈνΈκ° μλ€κ³ μκ°ν΄μ μλμ μμΈνκ² μ€λͺ μ μΆκ°νμλ€!
https://www.acmicpc.net/problem/4195
λμ΄λ | Gold 2 |
νμ΄ μκ° | 50 λΆ |
λΆλ₯ | μ λμ¨ νμΈλ(Union-Find) |
μκ°λ³΅μ‘λ | O(N) |
곡κ°λ³΅μ‘λ | O(N) |
βΆ union-find λ₯Ό μ΄μ§ μμ©νλ λ¬Έμ μ΄λ€. λ€λ§, union μ ν λλ§λ€ ν΄λΉ μ§ν©μ ν¬κΈ°λ₯Ό μΆλ ₯ν΄μΌ νλλ° μ²μμλ λ¨μνκ² μκ°ν΄μ μκ°μ΄κ³Όλ₯Ό λ°μλ€. π’
μ²μμ μκ°ν λ°©λ²μ μλμ κ°λ€.
βΆ union(a, b) λ₯Ό νλ€λ©΄, a μ b λ₯Ό μΌλ¨ ν©μΉκ³ , parent λ°°μ΄μ νμ¬ λ±λ‘λ μΉκ΅¬ κ°μλ§νΌ μννλ©΄μ a μ κ°μ μ‘°μμ κ°μ§κ³ μλμ§ ( find(a) == find(iter)? ) κ°μλ₯Ό μΈμ΄μ£Όλ λ°©μμ ννμλ€. νμ§λ§ μ΄λ κ² νλ©΄ λμΆ© O(N) λ²μ μ°μ°μ΄ λ νμνκΈ° λλ¬Έμ κ²°κ΅ O(N^2) μ μκ°λ³΅μ‘λλ₯Ό κ°μ§κ² λμ΄ μκ° μ΄κ³Όκ° λ°μνλ€.
βΆ μ¦, νΉμ μμκ° μ£Όμ΄μ§λ©΄ ν΄λΉ μμκ° μν μ§ν©μ ν¬κΈ°λ₯Ό ꡬνλ λ°©λ²μ λ³΄λ€ ν¨μ¨μ μΌλ‘ κ°μ ν΄μΌ νλ€.
κ°μ ν λ°©λ²μ λ€μκ³Ό κ°λ€.
βΆ μ΄μ°¨νΌ, μμκ° μν μ§ν©μ ν¬κΈ°λ Union μ ν λμλ§ λ³νλ κ² λ§λ€.
βΆ κ·Έλ¬λ©΄ κ°κ°μ μμκ° μν μ§ν©μ μ¬μ΄μ¦λ₯Ό μ μ₯ν λ°°μ΄μ λ°λ‘ μ μΈν λ€μμ, union μ°μ°μ΄ λ°μν λ size λ°°μ΄μ update ν΄μ£Όλ©΄ λμ§ μμκΉ?
βΆ κ·Έλ κ² ν΄μ£Όλ©΄, a λΌλ μμκ° μν μ§ν©μ μ¬μ΄μ¦λ₯Ό ꡬνλ κ²μ κ·Έλ₯ size[find(a)] λ₯Ό κΊΌλ΄μ£Όλ©΄ λλ―λ‘ λλ΅ O(1) μ ν΄κ²°μ΄ κ°λ₯νλ€.
size λ°°μ΄μ λν΄μ μ‘°κΈ λ μμΈνκ² μ€λͺ νλ©΄ μλμ κ°λ€.
βΆ μ΄κΈ°μ size λ₯Ό λͺ¨λ 1 λ‘ μ΄κΈ°ννλ€. μλλ©΄ μ²μμλ λ€λ€ μΉκ΅¬κ° μκΈ° μμ λ°μ μμΌλκΉ! (μ΄κΈ° μ§ν©μ μμλ μ€μ§ μκΈ° μμ λΏ)
βΆ union (a, b) λΌλ μ°μ°μ΄ λ°μνλ€κ³ κ°μ ν΄λ³΄μ. λμΆ© λ¬ννκ² μκ°ν΄μ b λΌλ μ§ν©μ a μ§ν©μ ν©μ³μΌ νλ€λ μν©μΌλ‘ μ΄ν΄ν΄λ³΄λ©΄, size[b] λ₯Ό size[a] μ μΆμ μν€λ©΄ λ κ² μμ κΉ¨λ¬μ μ μλ€. κ·Έλ¬λκΉ μλμ κ°μ΄ union μ°μ°μ μμ ν΄μ£Όμ.
private static void union(int a, int b) {
a = find(a);
b = find(b);
if (a != b) {
if (a < b) {
parent[b] = a;
size[a] += size[b];
} else {
parent[a] = b;
size[b] += size[a];
}
}
}
μ΄μ κ°μ΄ ν΄μ£Όλ©΄ size λ°°μ΄μ κ°κ°μ μμκ° μν΄ μλ μ§ν©μ ν¬κΈ°λ₯Ό μ μ₯νκ² λλ€. μ΄μ κ°μ κ³Όμ μΌλ‘, νΉμ μμκ° μν μ§ν©μ μ¬μ΄μ¦λ₯Ό ꡬνλ κ³Όμ μ O(N) μμ O(1) λ‘ μ΅μ νν μ μκ³ , κ·Έλ¬λ©΄ μ΅μ’ μ μΈ μκ° λ³΅μ‘λκ° λλ΅ O(N) μ΄ λκΈ° λλ¬Έμ λ¬Έμ λ₯Ό ν΄κ²°ν μ μλ€!
μ 체 μ½λλ μλμ κ°λ€.
import java.io.*;
import java.util.*;
public class Main {
static int id = 1;
static Map<String, Integer> hm = new HashMap<>();
static int[] parent;
static int[] size;
public static void main(String[] args) throws IOException {
System.setIn(new FileInputStream("src/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer stk;
int tc = Integer.parseInt(br.readLine()); // ν
μ€νΈ μΌμ΄μ€
while (tc > 0) {
tc--;
int f = Integer.parseInt(br.readLine()); // μΉκ΅¬ κ΄κ³μ μ
size = new int[f * 2 + 1];
parent = new int[f * 2 + 1];
for (int i = 1; i <= f * 2; i++) {
parent[i] = i;
size[i] = 1;
}
for (int i = 0; i < f; i++) {
stk = new StringTokenizer(br.readLine());
String a = stk.nextToken();
String b = stk.nextToken();
edit(a);
edit(b);
int aidx = hm.get(a);
int bidx = hm.get(b);
if(aidx!=bidx)
union(aidx, bidx);
bw.write(size[find(aidx)]+"\n");
}
}
bw.flush();
bw.close();
}
private static void union(int a, int b) {
a = find(a);
b = find(b);
if (a != b) {
if (a < b) {
parent[b] = a;
size[a] += size[b];
} else {
parent[a] = b;
size[b] += size[a];
}
}
}
private static int find(int a) {
if (a == parent[a])
return a;
else return parent[a] = find(parent[a]);
}
private static void edit(String name) {
if (hm.containsKey(name)) {
hm.get(name);
} else {
hm.put(name, id++);
}
}
}
'π₯ Algorithm || λ¬Έμ νμ΄ > PS' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[λ°±μ€ 9251/1520/9370] μλ° (0) | 2023.02.20 |
---|---|
[λ°±μ€ 5568/2559/1504/11066] μλ° (0) | 2023.02.19 |
[λ°±μ€ 1436/10815/11054/16139/16928] μλ° (0) | 2023.02.16 |
[λ°±μ€ 7568/1904/1018/25682/2110] μλ° (1) | 2023.02.15 |
[λ°±μ€ 2798/2231/14889/2606] μλ° (0) | 2023.02.14 |