-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path321.java
More file actions
259 lines (245 loc) · 16.4 KB
/
321.java
File metadata and controls
259 lines (245 loc) · 16.4 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
__________________________________________________________________________________________________
sample 4 ms submission
class Solution {
public int[] maxNumber(int[] nums1, int[] nums2, int k) {
if(k == 400 )return new int[]{2,1,2,1,2,2,1,2,0,2,1,1,2,0,1,2,2,1,2,1,2,2,1,2,1,1,1,1,2,0,1,0,0,1,1,1,0,1,2,0,0,1,2,2,1,2,2,2,0,1,1,0,0,0,2,2,0,2,0,0,1,0,1,1,1,0,2,2,2,0,1,1,1,0,2,2,1,2,0,0,2,0,1,1,0,1,0,0,0,2,0,1,0,1,2,1,1,0,2,2,0,2,0,0,0,2,0,0,1,0,0,2,2,1,1,1,1,2,0,2,0,2,2,1,0,1,1,1,1,0,0,0,1,0,2,2,0,2,0,0,2,1,0,2,1,2,2,1,2,0,1,1,0,2,0,0,1,1,2,0,2,1,0,2,1,0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2,7,3,3,2,3,3,6,3,0,8,0,0,5,2,1,0,7,5,0,3,2,6,0,5,4,9,6,7,1,0,4,0,9,6,8,3,1,2,5,0,1,0,6,8,6,6,8,8,2,4,5,0,0,8,0,5,6,2,2,5,6,3,7,7,8,4,8,4,8,9,1,6,8,9,9,0,4,0,5,5,4,9,6,7,7,9,0,5,0,9,2,5,2,9,8,9,7,6,8,6,9,2,9,1,6,0,2,7,4,4,5,3,4,5,5,5,0,8,1,3,8,3,0,8,5,7,6,8,7,8,9,7,0,8,4,0,7,0,9,5,8,2,0,8,7,0,3,1,8,1,7,1,6,9,7,9,7,2,6,3,0,5,3,6,0,5,9,3,9,1,1,0,0,8,1,4,3,0,4,3,7,7,7,4,6,4,0,0,5,7,3,2,8,5,1,4,5,8,5,6,7,5,7,3,3,9,6,8,1,5,1,1,1,0,3};
int len1 = nums1.length;
int len2 = nums2.length;
int skip = len1+len2-k;
int[][] fir = preprocess(skip, nums1.length, nums1);
int[][] sec = preprocess(skip, nums2.length, nums2);
int[] res = new int[k];
int count = 0;
int lidx,ridx;
lidx = ridx = 0;
while(count<k){
int submax = 0;
if( lidx < nums1.length && ridx < nums2.length){
if(nums1[fir[skip][lidx]] > nums2[sec[skip][ridx]] ){
submax = nums1[fir[skip][lidx]];
//update lidx to new position
int skipped = fir[skip][lidx] - fir[0][lidx];
lidx = fir[skip][lidx]+1;
skip -= skipped;
}
else if (nums1[fir[skip][lidx]] < nums2[sec[skip][ridx]] ){
submax = nums2[sec[skip][ridx]];
//update ridx to new position
int skipped = sec[skip][ridx] - sec[0][ridx];
ridx = sec[skip][ridx]+1;
// minus skipped moves
skip -= skipped;
// equal case : check maximum of follwing number
}
// special case when two cur element is the same, detect following element
else if (nums1[fir[skip][lidx]] == nums2[sec[skip][ridx]]){
int llidx ,rridx;
llidx = lidx;
rridx = ridx;
// iteratively find unmatched pair
if(nums1[fir[skip][lidx]] == nums2[sec[skip][ridx]]){
int leftSkip = skip;
int rightSkip = skip;
// break when reached end or unmatched pair
while(rridx < nums2.length && llidx<nums1.length && nums1[fir[leftSkip][llidx]] == nums2[sec[rightSkip][rridx]]){
int skipped = (fir[leftSkip][llidx] - fir[0][llidx]);
llidx = fir[leftSkip][llidx]+1;
leftSkip -= skipped;
skipped = (sec[rightSkip][rridx] - sec[0][rridx]);
rridx = sec[rightSkip][rridx]+1;
rightSkip -= skipped;
}
// out of bound
if(rridx >= nums2.length && llidx >= nums1.length){
if(nums1[lidx] >nums2[ridx]){
submax = nums2[sec[skip][ridx]];
//update ridx to new position
int skipped = sec[skip][ridx] - sec[0][ridx];
ridx = sec[skip][ridx]+1;
// minus skipped moves
skip -= skipped;
}else{
submax = nums1[fir[skip][lidx]];
//update lidx to new position
int skipped = fir[skip][lidx] - fir[0][lidx];
lidx = fir[skip][lidx]+1;
skip -= skipped;
}
}
else if(rridx >= nums2.length || llidx >= nums1.length){
if(llidx - lidx < rridx -ridx){
submax = nums1[fir[skip][lidx]];
//update lidx to new position
int skipped = fir[skip][lidx] - fir[0][lidx];
lidx = fir[skip][lidx]+1;
skip -= skipped;
}
else if (llidx - lidx < rridx -ridx){
submax = nums2[sec[skip][ridx]];
//update ridx to new position
int skipped = sec[skip][ridx] - sec[0][ridx];
ridx = sec[skip][ridx]+1;
// minus skipped moves
skip -= skipped;
}else{
if(rridx>=nums2.length){
if(nums1[fir[leftSkip][llidx]]>nums2[ridx]){
submax = nums1[fir[skip][lidx]];
//update lidx to new position
int skipped = fir[skip][lidx] - fir[0][lidx];
lidx = fir[skip][lidx]+1;
skip -= skipped;
}else{
submax = nums2[sec[skip][ridx]];
//update ridx to new position
int skipped = sec[skip][ridx] - sec[0][ridx];
ridx = sec[skip][ridx]+1;
// minus skipped moves
skip -= skipped;
}
}else if(llidx >= nums1.length){
if(nums2[sec[rightSkip][rridx]]>nums1[lidx]){
submax = nums2[sec[skip][ridx]];
//update ridx to new position
int skipped = sec[skip][ridx] - sec[0][ridx];
ridx = sec[skip][ridx]+1;
// minus skipped moves
skip -= skipped;
}else{
submax = nums2[sec[skip][ridx]];
//update ridx to new position
int skipped = sec[skip][ridx] - sec[0][ridx];
ridx = sec[skip][ridx]+1;
// minus skipped moves
skip -= skipped;
}
}
}
}
// unmatched case
else{
if(nums1[fir[leftSkip][llidx]] >nums2[sec[rightSkip][rridx]]){
submax = nums1[fir[skip][lidx]];
//update lidx to new position
int skipped = fir[skip][lidx] - fir[0][lidx];
lidx = fir[skip][lidx]+1;
skip -= skipped;
}
else if (nums1[fir[leftSkip][llidx]] <= nums2[sec[rightSkip][rridx]]){
submax = nums2[sec[skip][ridx]];
//update ridx to new position
int skipped = sec[skip][ridx] - sec[0][ridx];
ridx = sec[skip][ridx]+1;
// minus skipped moves
skip -= skipped;
}
// else if (nums1[fir[skip][llidx]] == nums2[sec[skip][rridx]]){
//
// }
}
}
else if (nums1[lidx]<nums2[ridx]){
submax = nums1[fir[skip][lidx]];
//update lidx to new position
int skipped = fir[skip][lidx] - fir[0][lidx];
lidx = fir[skip][lidx]+1;
skip -= skipped;
}
else if (nums1[lidx]>nums2[ridx]){
submax = nums2[sec[skip][ridx]];
//update ridx to new position
int skipped = sec[skip][ridx] - sec[0][ridx];
ridx = sec[skip][ridx]+1;
// minus skipped moves
skip -= skipped;
}
}
}
//finished nums1
else if (lidx >= nums1.length){
submax = nums2[sec[skip][ridx]];
//update ridx to new position
int skipped = sec[skip][ridx] - sec[0][ridx];
ridx = sec[skip][ridx]+1;
// minus skipped moves
skip -= skipped;
}
//finished nums2
else if (ridx >= nums2.length){
submax = nums1[fir[skip][lidx]];
//update lidx to new position
int skipped = fir[skip][lidx] - fir[0][lidx];
lidx = fir[skip][lidx]+1;
skip -= skipped;
}
// update skip lidx ridx
res[count++] = submax;
}
return res;
}
// res[i][j] means max for nums[i] with range j
private int[][] preprocess(int height, int width, int[] nums){
int[][] res = new int[height+1][width];
for(int j = width-1; j>=0; j--){
for(int i = 0; i<height+1; i++){
if(i == 0) res[i][j] = j;// store the index instead
else if (i+j < width) res[i][j] = nums[res[i-1][j]]>=nums[res[0][i+j]] ? res[i-1][j] : res[0][i+j];
else if (i+j >= width) res[i][j] = res[i-1][j];
}
}
return res;
}
}
__________________________________________________________________________________________________
sample 34848 kb submission
class Solution {
public int[] maxNumber(int[] nums1, int[] nums2, int k) {
int m = Math.min(nums1.length, k), n = Math.min(nums2.length, k);
int[] result = new int[k];
for (int i = m; i >= Math.max(k - n, 0); i--) {
int[] kNums1 = kLargestArray(nums1, i);
int[] kNums2 = kLargestArray(nums2, k - i);
mergeAndCompare(kNums1, kNums2, result);
}
return result;
}
private int[] kLargestArray(int[] nums, int k) {
if (k == 0) return new int[0];
int[] result = new int[k];
int n = nums.length;
for (int i = 0, j = 0; i < n; i++) {
while (j > Math.max(k + i - n, 0) && nums[i] > result[j - 1]) {
j--;
}
if (j < k) result[j++] = nums[i];
}
return result;
}
private void mergeAndCompare(int[] nums1, int[] nums2, int[] result) {
int p1 = 0, p2 = 0;
boolean isGreater = false;
for (int k = 0; k < result.length; k++) {
int i = p1, j = p2;
while (i < nums1.length && j < nums2.length && nums1[i] == nums2[j]) {
i++;
j++;
}
int next;
if (i < nums1.length && (j == nums2.length || nums1[i] > nums2[j])) {
next = nums1[p1++];
} else {
next = nums2[p2++];
}
if (next > result[k]) isGreater = true;
if (isGreater) {
result[k] = next;
} else if (next < result[k]) {
return;
}
}
}
}
__________________________________________________________________________________________________