33import org .zjy .learn .util .Application ;
44
55import java .util .ArrayList ;
6+ import java .util .Arrays ;
67import java .util .List ;
78import java .util .TreeMap ;
89
9- @ Application (time = "06/17/2019 13:00 " )
10+ @ Application (time = "06/17/2019 22:08 " )
1011public 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 /**
0 commit comments