File tree Expand file tree Collapse file tree 3 files changed +142
-0
lines changed Expand file tree Collapse file tree 3 files changed +142
-0
lines changed Original file line number Diff line number Diff line change @@ -31,6 +31,8 @@ This repo register my evolution in the Cracking The Coding Interview book as I g
31
31
# AWS Challenges
32
32
33
33
[ Amazon Review Score] ( https://github.com/brendonmiranda/CrackingTheCodingInterview/blob/main/src/main/java/aws/AmazonReviewScore.java )
34
+ [ Optimizing Box Weights] ( )
35
+ [ Number of Suitable Locations] ( )
34
36
35
37
# Chapter 2 | Linked Lists
36
38
Original file line number Diff line number Diff line change
1
+ package aws ;
2
+
3
+ public class AmazonNumberOfSuitableLocations {
4
+
5
+ /**
6
+ * // 2 * abs(x - center[i])
7
+ */
8
+ public static int numberOfSuitableLocationsSearch (int [] center , int d ) {
9
+
10
+ long low = -1000000000L ;
11
+ long high = 1000000000L ;
12
+
13
+ Long min = null ;
14
+ Long max = null ;
15
+
16
+ while (low <= high ) {
17
+
18
+ long x = low + (high - low ) / 2 ;
19
+
20
+ long dis = 0 ;
21
+ for (int c : center ) {
22
+ dis += 2 * (Math .abs (x - c ));
23
+ }
24
+
25
+ if (dis <= d ) {
26
+ min = x ;
27
+ high = x - 1 ;
28
+ } else {
29
+ low = x + 1 ;
30
+ }
31
+
32
+ }
33
+
34
+ if (min == null )
35
+ return 0 ;
36
+
37
+ low = -1000000000L ;
38
+ high = 1000000000L ;
39
+ while (low <= high ) {
40
+
41
+ long x = low + (high - low ) / 2 ;
42
+
43
+ long dis = 0 ;
44
+ for (int c : center ) {
45
+ dis += 2 * (Math .abs (x - c ));
46
+ }
47
+
48
+ if (dis <= d ) {
49
+ max = x ;
50
+ low = x + 1 ;
51
+ } else {
52
+ high = x - 1 ;
53
+ }
54
+
55
+ }
56
+
57
+ return Integer .parseInt (String .valueOf ((max - min ) + 1L ));
58
+ }
59
+
60
+ // quadratic expensive way of doing the same
61
+ public static int numberOfSuitableLocations (int [] center , int d ) {
62
+
63
+ int min = Integer .MIN_VALUE ;
64
+ int max = Integer .MAX_VALUE ;
65
+
66
+ int count = 0 ;
67
+ for (int x = min ; x < max ; x ++){
68
+ long buffer = 0 ;
69
+
70
+ for (int c = 0 ; c < center .length ; c ++) {
71
+ long distance = (2L * (x - center [c ]));
72
+ if (distance < 0 ) {
73
+ distance = -(distance ); // make absolute
74
+ }
75
+ buffer = buffer + distance ;
76
+ }
77
+ if (buffer <=d ){
78
+ count ++;
79
+ }
80
+
81
+ }
82
+
83
+ return count ;
84
+
85
+ }
86
+
87
+
88
+
89
+
90
+ }
Original file line number Diff line number Diff line change
1
+ package aws ;
2
+
3
+ import java .util .*;
4
+
5
+ public class AmazonPackItems {
6
+
7
+ /*
8
+ The intersection of A and B is null.
9
+ The union of A and B is equal to the original array.
10
+ The number of elements in subset A is minimal.
11
+ The sum of A's weights is greater than the sum of B's weights.*/
12
+ public static int [] packItems (int [] arr ) {
13
+
14
+ Arrays .sort (arr );
15
+
16
+ int max = arr .length / 2 ;
17
+ int arrSum = 0 ;
18
+ int bufferSum = 0 ;
19
+
20
+ final LinkedList <Integer > buffer = new LinkedList <>();
21
+ for (int i = 0 ; i < arr .length ; i ++) {
22
+ arrSum += arr [i ];
23
+ if (i >= max ) {
24
+ buffer .add (arr [i ]);
25
+ bufferSum += arr [i ];
26
+ }
27
+ }
28
+
29
+ // The sum of A's weights is greater than the sum of B's weights.
30
+ // Subset A must be minimal in size.
31
+ arrSum = arrSum - bufferSum ;
32
+ while ((bufferSum - buffer .get (0 )) > (arrSum )) {
33
+ // The union of A and B is equal to the original array.
34
+ Integer i = buffer .remove (0 );
35
+ arrSum += i ;
36
+ bufferSum -= i ;
37
+
38
+ }
39
+
40
+ int [] subA = new int [buffer .size ()];
41
+ for (int i = 0 ; i < buffer .size (); i ++) {
42
+ subA [i ] = buffer .get (i );
43
+ }
44
+
45
+ return subA ;
46
+
47
+ }
48
+
49
+
50
+ }
You can’t perform that action at this time.
0 commit comments