-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathHackerank.java
89 lines (83 loc) · 2.95 KB
/
Hackerank.java
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import java.util.*;
public class Hackerank {
public static void main(String[] args) {
List<Integer> sourceList = new ArrayList<Integer>();
sourceList.add(1);
sourceList.add(13);
sourceList.add(3);
sourceList.add(8);
sourceList.add(14);
sourceList.add(9);
sourceList.add(4);
sourceList.add(4);
int[] height = {1,8,6,2,5,4,8,3,7};
List<Integer> result = cutTheSticks(sourceList);
for (Integer integer : result) {
System.out.println(integer);
}
// System.out.println(appendAndDelete(s, t, k));
}
// public static String appendAndDelete(String s, String t, int k) {
// // Write your code here
// int count = 0;
// for (int i = 0; i < s.length(); i++) {
// char currentOfS = getChar(s, i);
// char currentOfT = getChar(t, i);
// if (currentOfS == currentOfT) continue;
// else {
// count = (s.length() - i) + (t.length() - i);
// break;
// }
// }
// if (k >= count) return "Yes";
// else return "No";
// }
// public static char getChar(String s, int index) {
// try {
// char c = s.charAt(index);
// return c;
// } catch (Exception e) {
// return '\0';
// }
// }
// public static int equalizeArray(List<Integer> arr) {
// HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
// int max = 0;
// for (int i = 0; i < arr.size(); i++) {
// map.put(arr.get(i), map .getOrDefault(arr.get(i), 0) + 1);
// max = Math.max(max, map.getOrDefault(arr.get(i), 0));
// }
// int count = 0;
// for (Map.Entry<Integer,Integer> entry : map.entrySet()) {
// if (entry.getValue() == max) {
// count = arr.size() - max;
// }
// }
// return count;
// }
public static List<Integer> cutTheSticks(List<Integer> arr) {
// Write your code here
Set<Integer> output = new LinkedHashSet<Integer>();
int minStick = 1;
for(int i = 0 ; i < arr.size(); i++) {
minStick = Math.min(minStick, arr.get(i));
}
while (arr.size() > 0) {
int count = 0;
for(int i = 0 ; i < arr.size(); i++) {
int currentStick = arr.get(i);
if(minStick == currentStick) {
arr.remove(i);
i = i - 1;
count++;
}
else {
arr.set(i, arr.get(i) - minStick);
count++;
}
}
output.add(count);
}
return new ArrayList<>(output);
}
}