forked from sowon-dev/AlgorithmStudy_Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPriyankaandToys.java
More file actions
32 lines (26 loc) Β· 925 Bytes
/
PriyankaandToys.java
File metadata and controls
32 lines (26 loc) Β· 925 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package hackerrank;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class PriyankaandToys {
static int toys(int[] w) {
// 컨ν
μ΄λ 1λμ μ μ¬λ λͺ¨λ νλͺ©μ 무κ²λ μ΅μ λ¬΄κ² νλͺ©μ 무κ²μ 4λ₯Ό λν κ²λ³΄λ€ μκ±°λ κ°μμΌν¨.
// μ΄λ νμν κ°μ₯ μμ 컨ν
μ΄λ κ°―μλ₯Ό 리ν΄νλ λ¬Έμ μ΄λ€.
int minContainers = 1;
Arrays.sort(w);
// System.out.println(Arrays.toString(w));
int start = w[0] + 4;
for (int j : w) {
if ((j > start)) {
//컨ν
μ΄λ 1λμ μμνλ μ΅μ 무κ²λ₯Ό μμ νλ€.
start = j + 4;
minContainers++;
}
}
return minContainers;
}
public static void main(String[] args) {
System.out.println(toys(new int[]{1,2,3,4,5,10,11,12,13})+", ans: 2");
System.out.println(toys(new int[]{1,2,3,21,7,12,14,21})+", ans: 4");
}
}