-
Notifications
You must be signed in to change notification settings - Fork 152
/
MaxCounters.java
59 lines (48 loc) · 1.97 KB
/
MaxCounters.java
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
package MaxCounters;
class Solution {
public int[] solution(int N, int[] A) {
// write your code in Java SE 8
// 1. key point: maintain the max value
int max = 0;
// 2. key point: maintain the current_min (very important!!!)
// so, we can move "the 2nd for-loop" outside "the 1st for-loop"
// by maintaining "min"
int min =0;
// new integer array
int[] my_array = new int[N];
/* no need to initialize (because the values are "0" by default)
for(int i=0; i<my_array.length; i++){
my_array[i] =0;
}
*/
for(int i=0; i<A.length; i++){
if( A[i] >= 1 && A[i] <= N){ // normal case
// important: check the "min" before "increasing by 1"
if(my_array[ A[i] -1] < min){
my_array[ A[i] -1] = min; // update it to "min"
}
my_array[ A[i] -1 ] ++; // increased by 1
if( my_array[ A[i] -1 ] > max){ // maintain max
max = my_array[ A[i] -1 ];
}
}
else if( A[i] == N+1){ // special case
/* cannot use for-loop (will take too much time)
for(int j=0; j<my_array.length; j++){
my_array[j] = max;
}
// instead, we maintain "min", so we can move the for-loop outside */
min = max; // all the values should be bigger than "max"
// therefore, "min = max"
}
}
// move the 2nd for-loop outside the 1st for-loop
// update some elements who have not been updated yet
for(int j=0; j<my_array.length; j++){
if(my_array[j] < min){
my_array[j] = min; // update it to "min"
}
}
return my_array;
}
}