3
3
import org .zjy .learn .util .Application ;
4
4
5
5
import java .util .ArrayList ;
6
+ import java .util .Arrays ;
6
7
import java .util .List ;
7
8
import java .util .TreeMap ;
8
9
9
- @ Application (time = "06/17/2019 13:00 " )
10
+ @ Application (time = "06/17/2019 22:08 " )
10
11
public class TwoSumClosest implements Runnable {
11
12
/**
12
13
* two sum closest
@@ -16,11 +17,78 @@ public class TwoSumClosest implements Runnable {
16
17
*/
17
18
@ Override
18
19
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
+ }
22
81
}
82
+ return result .get (result .firstKey ());
83
+ }
23
84
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
+ }
24
92
}
25
93
26
94
/**
0 commit comments