-
Notifications
You must be signed in to change notification settings - Fork 21
/
Subarray Sum Closest.java
executable file
·261 lines (225 loc) · 7.59 KB
/
Subarray Sum Closest.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
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
260
261
M
1531118637
tags: Sort, PreSum, Subarray, PriorityQueue
time: O(nlogn)
space: O(n)
给一串数字, 找subarray的首尾index, 条件: subarray最接近0.
#### PreSum + index in class
- Can be a 2D array, or a `class Point` to store preSum + index
- Sort preSum: smaller (有可能负数) 靠前, 大数字靠后
- 比较preSum种相连接的两个节点, 找差值min; 因为最接近的两个preSum节点的差值肯定是最小
- min所在的两个节点的index, 就是result candidate: 这两个index可能再原nums里面相差很远
- time O(nlogn), sort
- space: O(n)
#### 为何没有用 map<preSum, index> ?
- 因为map虽然能存 preSum + index, 但是无法有效排序
- 所以用一个class来存这两个信息, 然后合理排序
```
/*
Given an integer array, find a subarray with sum closest to zero.
Return the indexes of the first number and last number.
Example
Given [-3, 1, 1, -3, 5], return [0, 2], [1, 3], [1, 1], [2, 2] or [0, 4]
Challenge
O(nlogn) time
Tags Expand
Subarray Sort
Thoughts:
Took a me a while to think through how to find the closest sum to 0.
Credits should be given to: http://rafal.io/posts/subsequence-closest-to-t.html
*/
// PriorityQueue
public class Solution {
class PreSumNode {
int val, index;
public PreSumNode(int val, int index) {
this.val = val;
this.index = index;
}
}
public int[] subarraySumClosest(int[] nums) {
int[] rst = new int[2];
if(nums == null || nums.length <= 1) return rst;
int n = nums.length;
PriorityQueue<PreSumNode> queue = buildPreSumNodes(nums);
int start = 0, end = 0, min = Integer.MAX_VALUE;
while (!queue.isEmpty()) {
PreSumNode p = queue.poll();
if (!queue.isEmpty()) {
int temp = queue.peek().val - p.val;
if (temp <= min) {
min = temp;
start = p.index;
end = queue.peek().index;
}
}
}
if (start < end) {
rst[0] = start + 1;
rst[1] = end;
} else {
rst[0] = end + 1;
rst[1] = start;
}
return rst;
}
private PriorityQueue<PreSumNode> buildPreSumNodes(int[] nums) {
PriorityQueue<PreSumNode> queue = new PriorityQueue<>(Comparator.comparing(p -> p.val));
int preSum = 0;
for (int i = 0; i < nums.length; i++) {
preSum += nums[i];
queue.offer(new PreSumNode(preSum, i));
}
return queue;
}
}
// Use a class point to track both preSum value and index
public class Solution {
class Point {
int val, index;
public Point(int val, int index) {
this.val = val;
this.index = index;
}
}
public int[] subarraySumClosest(int[] nums) {
int[] rst = new int[2];
if(nums == null || nums.length <= 1) return rst;
int n = nums.length;
Point[] points = buildPoints(nums);
int start = 0, end = 0, min = Integer.MAX_VALUE;
for (int i = 0; i < n - 1; i++) {
int temp = points[i + 1].val - points[i].val;
if (temp <= min) {
min = temp;
start = points[i].index;
end = points[i + 1].index;
}
}
if (start < end) {
rst[0] = start + 1;
rst[1] = end;
} else {
rst[0] = end + 1;
rst[1] = start;
}
return rst;
}
private Point[] buildPoints(int[] nums) {
int n = nums.length;
Point[] points = new Point[n];
points[0] = new Point(nums[0], 0);
for (int i = 1; i < n; i++) {
points[i] = new Point(points[i - 1].val + nums[i], i);
}
Arrays.sort(points, Comparator.comparing(p -> p.val));
return points;
}
}
// use 2D array, same concept, a bit messy
class CustomComparator implements Comparator<int[]> {
public int compare(int[] a, int[] b) {
return Integer.compare(a[0], b[0]);
}
}
public class Solution {
public ArrayList<Integer> subarraySumClosest(int[] nums) {
ArrayList<Integer> rst = new ArrayList<Integer>();
if(nums == null || nums.length == 0) {
return rst;
}
if (nums.length == 1) {
rst.add(0); rst.add(0);
return rst;
}
int[][] culmulate = new int[nums.length][2];
culmulate[0][0] = nums[0];
culmulate[0][1] = 0;
for (int i = 1; i < nums.length; i++) {
culmulate[i][0] = culmulate[i - 1][0] + nums[i];
culmulate[i][1] = i;
}
Arrays.sort(culmulate, new CustomComparator());
int min = Integer.MAX_VALUE;
int start = 0;
int end = 0;
for (int i = 0; i < nums.length - 1; i++) {
int temp = culmulate[i + 1][0] - culmulate[i][0];
if (temp <= min) {
min = temp;
start = culmulate[i][1];
end = culmulate[i + 1][1];
}
}
if (start < end) {
rst.add(start + 1);
rst.add(end);
} else {
rst.add(end + 1);
rst.add(start);
}
return rst;
}
}
//I also had to run a little java program locally to test/debug:
/*
import java.lang.*;
import java.util.*;
class CustomComparator implements Comparator<int[]> {
public int compare(int[] a, int[] b) {
return Integer.compare(a[0], b[0]);
}
}
public class test {
public ArrayList<Integer> subarraySumClosest(int[] nums) {
ArrayList<Integer> rst = new ArrayList<Integer>();
if(nums == null || nums.length == 0) {
return rst;
}
int[][] culmulate = new int[nums.length][2];
culmulate[0][0] = nums[0];
culmulate[0][1] = 0;
for (int i = 1; i < nums.length; i++) {
culmulate[i][0] = culmulate[i - 1][0] + nums[i];
culmulate[i][1] = i;
}
//TEST:
for(int i =0 ; i < nums.length; i++) {
System.out.println("test:" + culmulate[i][0] + " " + culmulate[i][1]);
}
Arrays.sort(culmulate, new CustomComparator());
for(int i =0 ; i < nums.length; i++) {
System.out.println("sorted:" + culmulate[i][0] + " " + culmulate[i][1]);
}
int min = Integer.MAX_VALUE;
int start = 0;
int end = 0;
for (int i = 0; i < nums.length - 1; i++) {
int temp = culmulate[i + 1][0] - culmulate[i][0];
System.out.println(culmulate[i + 1][0] + " minus " + culmulate[i][0] + " = " + temp);
if (temp <= min) {
min = temp;
start = culmulate[i][1];
end = culmulate[i + 1][1];
System.out.println("record:" + start + " " + end );
}
}
System.out.println("min:" + min);
if (start < end) {
rst.add(start + 1);
rst.add(end);
} else {
rst.add(end + 1);
rst.add(start);
}
return rst;
}
public static void main(String[] args){
int[] nums = {6,-4,-8,3,1,7};//{5,10,5,3,2,1,1,-2,-4,3};
test t = new test();
ArrayList<Integer> rst = t.subarraySumClosest(nums);
System.out.println(rst.get(0) + " " + rst.get(1));
}
}
*/
```