Skip to content

Commit b259c0a

Browse files
committed
no message
1 parent 0634204 commit b259c0a

1 file changed

Lines changed: 147 additions & 0 deletions

File tree

Java/Subarray Sum Closest.java

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/*
2+
Given an integer array, find a subarray with sum closest to zero. Return the indexes of the first number and last number.
3+
4+
Example
5+
Given [-3, 1, 1, -3, 5], return [0, 2], [1, 3], [1, 1], [2, 2] or [0, 4]
6+
7+
Challenge
8+
O(nlogn) time
9+
10+
Tags Expand
11+
Subarray Sort
12+
13+
Thoughts:
14+
Took a me a while to think through how to find the closest sum to 0.
15+
Credits should be given to: http://rafal.io/posts/subsequence-closest-to-t.html
16+
*/
17+
18+
19+
class CustomComparator implements Comparator<int[]> {
20+
public int compare(int[] a, int[] b) {
21+
return Integer.compare(a[0], b[0]);
22+
}
23+
}
24+
25+
public class Solution {
26+
27+
/**
28+
* @param nums: A list of integers
29+
* @return: A list of integers includes the index of the first number
30+
* and the index of the last number
31+
*/
32+
public ArrayList<Integer> subarraySumClosest(int[] nums) {
33+
ArrayList<Integer> rst = new ArrayList<Integer>();
34+
if(nums == null || nums.length == 0) {
35+
return rst;
36+
}
37+
if (nums.length == 1) {
38+
rst.add(0); rst.add(0);
39+
return rst;
40+
}
41+
int[][] culmulate = new int[nums.length][2];
42+
culmulate[0][0] = nums[0];
43+
culmulate[0][1] = 0;
44+
for (int i = 1; i < nums.length; i++) {
45+
culmulate[i][0] = culmulate[i - 1][0] + nums[i];
46+
culmulate[i][1] = i;
47+
}
48+
49+
Arrays.sort(culmulate, new CustomComparator());
50+
int min = Integer.MAX_VALUE;
51+
int start = 0;
52+
int end = 0;
53+
for (int i = 0; i < nums.length - 1; i++) {
54+
int temp = culmulate[i + 1][0] - culmulate[i][0];
55+
if (temp <= min) {
56+
min = temp;
57+
start = culmulate[i][1];
58+
end = culmulate[i + 1][1];
59+
}
60+
}
61+
if (start < end) {
62+
rst.add(start + 1);
63+
rst.add(end);
64+
} else {
65+
rst.add(end + 1);
66+
rst.add(start);
67+
}
68+
return rst;
69+
}
70+
}
71+
72+
73+
74+
75+
76+
//I also had to run a little java program locally to test/debug:
77+
/*
78+
79+
import java.lang.*;
80+
import java.util.*;
81+
82+
class CustomComparator implements Comparator<int[]> {
83+
public int compare(int[] a, int[] b) {
84+
return Integer.compare(a[0], b[0]);
85+
}
86+
}
87+
88+
public class test {
89+
public ArrayList<Integer> subarraySumClosest(int[] nums) {
90+
ArrayList<Integer> rst = new ArrayList<Integer>();
91+
if(nums == null || nums.length == 0) {
92+
return rst;
93+
}
94+
int[][] culmulate = new int[nums.length][2];
95+
culmulate[0][0] = nums[0];
96+
culmulate[0][1] = 0;
97+
for (int i = 1; i < nums.length; i++) {
98+
culmulate[i][0] = culmulate[i - 1][0] + nums[i];
99+
culmulate[i][1] = i;
100+
}
101+
//TEST:
102+
for(int i =0 ; i < nums.length; i++) {
103+
System.out.println("test:" + culmulate[i][0] + " " + culmulate[i][1]);
104+
}
105+
Arrays.sort(culmulate, new CustomComparator());
106+
for(int i =0 ; i < nums.length; i++) {
107+
System.out.println("sorted:" + culmulate[i][0] + " " + culmulate[i][1]);
108+
}
109+
110+
int min = Integer.MAX_VALUE;
111+
int start = 0;
112+
int end = 0;
113+
for (int i = 0; i < nums.length - 1; i++) {
114+
int temp = culmulate[i + 1][0] - culmulate[i][0];
115+
System.out.println(culmulate[i + 1][0] + " minus " + culmulate[i][0] + " = " + temp);
116+
if (temp <= min) {
117+
min = temp;
118+
start = culmulate[i][1];
119+
end = culmulate[i + 1][1];
120+
System.out.println("record:" + start + " " + end );
121+
}
122+
}
123+
System.out.println("min:" + min);
124+
if (start < end) {
125+
rst.add(start + 1);
126+
rst.add(end);
127+
} else {
128+
rst.add(end + 1);
129+
rst.add(start);
130+
}
131+
return rst;
132+
}
133+
134+
public static void main(String[] args){
135+
136+
int[] nums = {6,-4,-8,3,1,7};//{5,10,5,3,2,1,1,-2,-4,3};
137+
test t = new test();
138+
ArrayList<Integer> rst = t.subarraySumClosest(nums);
139+
System.out.println(rst.get(0) + " " + rst.get(1));
140+
}
141+
}
142+
143+
*/
144+
145+
146+
147+

0 commit comments

Comments
 (0)