Skip to content

Commit c172197

Browse files
committed
Corrected
1 parent 9787e26 commit c172197

File tree

5 files changed

+78
-7
lines changed

5 files changed

+78
-7
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<dependency>
2323
<groupId>org.reflections</groupId>
2424
<artifactId>reflections</artifactId>
25-
<version>0.9.9</version>
25+
<version>0.9.11</version>
2626
</dependency>
2727
</dependencies>
2828
</project>

src/main/java/org/zjy/learn/code/amazon/FindSongs.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ public void run() {
1414
}
1515

1616
private int[] findSongs(int[] songs) {
17-
17+
return null;
1818
}
1919
}

src/main/java/org/zjy/learn/code/amazon/ReorderLogFiles.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
package org.zjy.learn.code.amazon;
2+
3+
import java.util.Arrays;
4+
15
public class ReorderLogFiles {
26
public String[] reorderLogFiles(String[] logs) {
37
Arrays.sort(logs, (a, b) -> {

src/main/java/org/zjy/learn/code/amazon/TwoSumClosest.java

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
import org.zjy.learn.util.Application;
44

55
import java.util.ArrayList;
6+
import java.util.Arrays;
67
import java.util.List;
78
import java.util.TreeMap;
89

9-
@Application(time = "06/17/2019 13:00")
10+
@Application(time = "06/17/2019 22:08")
1011
public class TwoSumClosest implements Runnable {
1112
/**
1213
* two sum closest
@@ -16,11 +17,78 @@ public class TwoSumClosest implements Runnable {
1617
*/
1718
@Override
1819
public void run() {
19-
List<Integer[]> result1 = twoSumClosest(new int[][]{{1, 3}, {2, 4}, {3, 1}, {4, 2}}, new int[][]{{5, 3}, {6, 4}}, 8);
20-
for (Integer[] pair : result1) {
21-
System.out.println(pair[0] + " " + pair[1]);
20+
// List<Integer[]> result1 = twoSumClosest(new int[][]{{1, 3}, {2, 4}, {3, 1}, {4, 2}}, new int[][]{{5, 3}, {6, 4}}, 8);
21+
// for (Integer[] pair : result1) {
22+
// System.out.println(pair[0] + " " + pair[1]);
23+
// }
24+
25+
int deviceCapacity1 = 20;
26+
List<List<Integer>> foreground1 = new ArrayList<>();
27+
foreground1.add(Arrays.asList(1, 8));
28+
foreground1.add(Arrays.asList(2, 7));
29+
foreground1.add(Arrays.asList(3, 14));
30+
List<List<Integer>> background1 = new ArrayList<>();
31+
background1.add(Arrays.asList(1, 5));
32+
background1.add(Arrays.asList(2, 10));
33+
background1.add(Arrays.asList(3, 14));
34+
// expect: 3 1
35+
36+
int deviceCapacity2 = 20;
37+
List<List<Integer>> foreground2 = new ArrayList<>();
38+
foreground2.add(Arrays.asList(1, 8));
39+
foreground2.add(Arrays.asList(2, 15));
40+
foreground2.add(Arrays.asList(3, 9));
41+
List<List<Integer>> background2 = new ArrayList<>();
42+
background2.add(Arrays.asList(1, 8));
43+
background2.add(Arrays.asList(2, 11));
44+
background2.add(Arrays.asList(3, 12));
45+
// expect:
46+
// 1 3
47+
// 3 2
48+
49+
System.out.println("1:");
50+
output(optimalUtilization(deviceCapacity1, foreground1, background1));
51+
System.out.println("2:");
52+
output(optimalUtilization(deviceCapacity2, foreground2, background2));
53+
}
54+
55+
private List<List<Integer>> optimalUtilization(
56+
int deviceCapacity,
57+
List<List<Integer>> foregroundAppList,
58+
List<List<Integer>> backgroundAppList)
59+
{
60+
// WRITE YOUR CODE HERE
61+
TreeMap<Integer, List<Integer>> tree = new TreeMap<>();
62+
for (List<Integer> pair : backgroundAppList) {
63+
List<Integer> list = tree.getOrDefault(pair.get(1), new ArrayList<>());
64+
list.add(pair.get(0));
65+
tree.put(pair.get(1), list);
66+
}
67+
TreeMap<Integer, List<List<Integer>>> result = new TreeMap<>();
68+
for (List<Integer> pair : foregroundAppList) {
69+
Integer floorKey = tree.floorKey(deviceCapacity - pair.get(1));
70+
if (floorKey != null) {
71+
int diff = Math.abs(deviceCapacity - pair.get(1) - floorKey);
72+
List<List<Integer>> list = result.getOrDefault(diff, new ArrayList<>());
73+
for (int id : tree.get(floorKey)) {
74+
List<Integer> match = new ArrayList<>();
75+
match.add(pair.get(0));
76+
match.add(id);
77+
list.add(match);
78+
}
79+
result.put(diff, list);
80+
}
2281
}
82+
return result.get(result.firstKey());
83+
}
2384

85+
private void output(List<List<Integer>> matrix) {
86+
for (List<Integer> row : matrix) {
87+
for (int col : row) {
88+
System.out.print(col + " ");
89+
}
90+
System.out.println();
91+
}
2492
}
2593

2694
/**

src/main/java/org/zjy/learn/util/Main.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.zjy.learn.util;
22

3-
43
import org.reflections.Reflections;
54

65
import java.lang.annotation.Annotation;

0 commit comments

Comments
 (0)