-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path768.java
More file actions
74 lines (63 loc) · 2.04 KB
/
768.java
File metadata and controls
74 lines (63 loc) · 2.04 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
__________________________________________________________________________________________________
sample 0 ms submission
class Solution {
public int maxChunksToSorted(int[] arr) {
if(arr==null || arr.length==0) return 0;
int[] data = new int[arr.length+1];
data[1]=arr[0];
int ans=1;
for(int i=1; i<arr.length; i++){
if(arr[i]>=data[ans]){
data[++ans]=arr[i]; continue;
}
int top= data[ans--];
while(ans>0 && arr[i]<data[ans]) ans--;
data[++ans]=top;
}
return ans;
}
}
__________________________________________________________________________________________________
sample 37732 kb submission
class Solution {
public int maxChunksToSorted(int[] arr) {
int chunkCount = 0;
int[] sortedArray = new int[arr.length];
for (int i=0; i<arr.length; i++) {
sortedArray[i] = arr[i];
}
Arrays.sort(sortedArray);
int newPositions[] = new int[arr.length];
for (int i=0; i<sortedArray.length; i++) {
for (int j=0; i<arr.length; j++) {
if (arr[j]==sortedArray[i]) {
newPositions[j] = i;
if (arr[j] == 0) {
arr[j] = Integer.MIN_VALUE;
} else {
arr[j] *= -1;
}
break;
}
}
}
for (int i=0; i<arr.length; i++) {
if (arr[i] == Integer.MIN_VALUE) {
arr[i] = 0;
} else {
arr[i] *= -1;
}
}
int max = Integer.MIN_VALUE;
for (int i=0; i<newPositions.length; i++) {
if (newPositions[i] > max) {
max = newPositions[i];
}
if (max == i) {
chunkCount++;
}
}
return chunkCount;
}
}
__________________________________________________________________________________________________