-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAmazonPractice.java
More file actions
136 lines (113 loc) · 3.77 KB
/
AmazonPractice.java
File metadata and controls
136 lines (113 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import java.util.ArrayList;
import java.util.List;
public class AmazonPractice {
public static int[] sortArray(int[] arr) {
int i, max, location, j, temp, len = arr.length;
for (i = 0; i < len; i++) {
max = arr[i];
location = i;
for (j = i; j < len; j++) {
max = arr[j];
location = j;
}
temp = arr[i];
arr[i] = arr[location];
arr[location] = temp;
}
return arr;
}
public class ComponentNode {
public int value;
public ArrayList<ComponentNode> components;
public ComponentNode() {
components = new ArrayList<>();
}
public ComponentNode(int numOfLinesChanged) {
this.value = numOfLinesChanged;
this.components = new ArrayList<>();
}
}
class ComponentInfo {
public int totalVal;
public int children;
public ComponentNode component;
public ComponentInfo(int totalVal, int children, ComponentNode component) {
this.totalVal = totalVal;
this.children = children;
this.component = component;
}
}
private ComponentInfo targetNode;
private double maxAverage;
public ComponentNode getFastestComponent(ComponentNode rootComponent) {
if (rootComponent == null || rootComponent.components.size() == 0) return null;
targetNode = new ComponentInfo(0, 1, null);
maxAverage = Double.MIN_VALUE;
maxComponent(rootComponent);
return targetNode.component;
}
private ComponentInfo maxComponent(ComponentNode cur) {
if (cur.components.size() == 0) {
return new ComponentInfo(cur.value, 1, cur);
}
int curVal = 0;
int children = 0;
for (ComponentNode cNode : cur.components) {
ComponentInfo curInfo = maxComponent(cNode);
curVal += curInfo.totalVal;
children += curInfo.children;
}
ComponentInfo curInfo = new ComponentInfo(curVal, children, cur);
double curAverage = ((double)curVal) / children;
if (curAverage > maxAverage) {
targetNode = curInfo;
maxAverage = ((double)curInfo.totalVal)/ curInfo.children;
}
return curInfo;
}
/**
Interview prepare
*/
public static int sum(int[][] m) {
if (m == null) return 0;
int len = m.length;
int sum1 = 0, sum2 = 0;
for (int i = 0; i < len; i++) {
sum1 += m[i][i];
sum2 += m[i][len - i - 1];
}
return Math.abs(sum1 - sum2);
}
// New OA
//lc 240
static PairInt locationOfTargetValue(int rowCount, int columnCount, List<List<Integer>> matrix, int targetValue) {
if (rowCount <= 0 || columnCount <= 0 || matrix == null || matrix.get(0) == null) return new PairInt(-1, -1);
int i = 0, j = columnCount - 1;
while (i < rowCount && j >= 0) {
int curVal = matrix.get(i).get(j);
if (curVal == targetValue) {
return new PairInt(i, j);
}
if (curVal < targetValue) {
i--;
} else {
j++;
}
}
return new PairInt(-1, -1);
}
static class PairInt {
int first, second;
PairInt() {}
PairInt(int first, int second) {
this.first = first;
this.second = second;
}
}
public static void main(String[] args) {
// AmazonPractice a = new AmazonPractice();
// System.out.println(sum(new int[][] {{1, 2}, {2, 1}}));
// List<List<Integer>> matrix = new ArrayList
// PairInt res = locationOfTargetValue(3, 4, matrix, 3);
}
}