-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathSolution.java
More file actions
52 lines (45 loc) · 1.43 KB
/
Solution.java
File metadata and controls
52 lines (45 loc) · 1.43 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package Practice.Algorithms.Implementation.CutTheSticks;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class Solution {
private static int[] cutTheSticks(int[] arr) {
// Runs in O(n) time
Arrays.sort(arr);
List<Integer> list = new ArrayList<>();
int i = 0;
int lowIdx = 0;
int minStickLength = arr[0];
while (i < arr.length) {
if (arr[i] > minStickLength) {
list.add(arr.length - (lowIdx));
lowIdx = i ;
minStickLength += arr[i] - minStickLength;
}
i++;
// Add last
if (i == arr.length) {
list.add(arr.length - (lowIdx));
}
}
// Convert to int array
int[] result = new int[list.size()];
for (int j = 0; j < list.size(); j++) result[j] = list.get(j);
return result;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] arr = new int[n];
for(int arr_i = 0; arr_i < n; arr_i++){
arr[arr_i] = in.nextInt();
}
int[] result = cutTheSticks(arr);
for (int i = 0; i < result.length; i++) {
System.out.print(result[i] + (i != result.length - 1 ? "\n" : ""));
}
System.out.println("");
in.close();
}
}