Skip to content

Commit ff3ca93

Browse files
committed
add Programmers
1 parent 63a1ace commit ff3ca93

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

java/src/main/java/Programmers.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import java.util.*;
2+
import java.util.stream.Collectors;
3+
import java.util.stream.IntStream;
4+
5+
import static org.junit.Assert.assertArrayEquals;
6+
7+
/**
8+
* Created by jeremy on 03/13/2019.
9+
*/
10+
public class Programmers {
11+
public static int[] solution(int[] progresses, int[] speeds) {
12+
List<Integer> deploys = new ArrayList<>();
13+
Queue<Integer> works = IntStream.range(0, progresses.length).boxed().collect(Collectors.toCollection(LinkedList::new));
14+
15+
while (!works.isEmpty()) {
16+
int done = 0;
17+
// 1. check if work is done
18+
for (int i = 0; i < progresses.length; i++) {
19+
int progress = progresses[i];
20+
if (works.element() == i && progress >= 100) {
21+
done++;
22+
works.remove();
23+
}
24+
}
25+
if (done > 0) deploys.add(done);
26+
27+
// 2. work
28+
for (int i = 0; i < progresses.length; i++) {
29+
if (progresses[i] < 100) {
30+
// print(progresses);
31+
progresses[i] += speeds[i];
32+
}
33+
}
34+
}
35+
36+
return deploys.stream().mapToInt(i -> i).toArray();
37+
}
38+
39+
public static void print(int[] progresses) {
40+
Arrays.stream(progresses).boxed().forEach(p -> System.out.print(p + " "));
41+
System.out.println();
42+
}
43+
44+
public static void main(String[] args) {
45+
assertArrayEquals(new int [] { 2, 1 }, solution(new int [] { 93,30,55 }, new int[] { 1,30,5 }));
46+
}
47+
}

0 commit comments

Comments
 (0)