Skip to content

Commit 5ae2f96

Browse files
committed
Update the solutions
1 parent 9431af9 commit 5ae2f96

File tree

3 files changed

+201
-0
lines changed

3 files changed

+201
-0
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package org.algo.base;
2+
3+
/***
4+
* 53. Maximum Subarray
5+
*/
6+
public class MaxSubSum {
7+
/***
8+
* O(N³)
9+
* Cubic maximum contiguous subsequence sum algorithm.
10+
*/
11+
public int maxSubSumCubic(int[] a) {
12+
int maxSum = 0;
13+
14+
for (int i = 0; i < a.length; i++) {
15+
for (int j = i; j < a.length; j++) {
16+
int thisSum = 0;
17+
for (int k = i; k <= j; k++) {
18+
thisSum += a[k];
19+
}
20+
21+
if (thisSum > maxSum) maxSum = thisSum;
22+
}
23+
}
24+
25+
return maxSum;
26+
}
27+
28+
/***
29+
* O(N²)
30+
* Quadratic maximum contiguous subsequence sum algorithm.
31+
*/
32+
public int maxSubSumQuad(int[] a) {
33+
int maxSum = 0;
34+
35+
for (int i = 0; i < a.length; i++) {
36+
int thisSum = 0;
37+
for (int j = i; j < a.length; j++) {
38+
thisSum += a[j];
39+
40+
if (thisSum > maxSum) maxSum = thisSum;
41+
}
42+
}
43+
44+
return maxSum;
45+
}
46+
47+
// O(N*lgN)
48+
public int maxSubSumLog(int[] a) {
49+
return maxSubSumRec(a, 0, a.length - 1);
50+
}
51+
52+
/***
53+
* O(N) Linear-time
54+
*
55+
* Dynamic Programming:
56+
* dp[i] : max sum subarray ending with index i
57+
* dp[i] = max(dp[i-1] + a[i], a[i])
58+
*/
59+
public int maxSubArray(int[] a) {
60+
int maxValue = a[0];
61+
int n = a.length;
62+
int[] dp = new int[n + 1];
63+
dp[0] = a[0];
64+
for (int i = 1; i < n; i++) {
65+
dp[i] = Math.max(dp[i - 1] + a[i], a[i]);
66+
maxValue = Math.max(dp[i], maxValue);
67+
}
68+
return maxValue;
69+
}
70+
71+
/***
72+
* O(N) Linear-time maximum contiguous subsequence sum algorithm.
73+
*/
74+
public int maxSubSumLinear(int[] a) {
75+
int max = 0, maxSoFar = 0;
76+
for (int i = 0; i < a.length; i++) {
77+
maxSoFar += a[i];
78+
if (maxSoFar > max) max = maxSoFar;
79+
else if (maxSoFar < 0) maxSoFar = 0;
80+
}
81+
return max;
82+
}
83+
84+
/***
85+
* Recursive maximum contiguous subsequence sum algorithm.
86+
* Finds maximum sum in subarray spanning a[left..right].
87+
* Does not attempt to maintain actual best sequence.
88+
*/
89+
private int maxSubSumRec(int[] a, int l, int r) {
90+
if (l >= r) return Math.max(0, a[l]);
91+
92+
int m = l + (r - l) / 2;
93+
int maxLeft = maxSubSumRec(a, l, m);
94+
int maxRight = maxSubSumRec(a, m + 1, r);
95+
96+
int leftSideMax = 0, leftSumMax = 0;
97+
for (int i = m; i >= l; i--) {
98+
leftSumMax += a[i];
99+
if (leftSumMax > leftSideMax) {
100+
leftSideMax = leftSumMax;
101+
}
102+
}
103+
104+
int rightSideMax = 0, rightSumMax = 0;
105+
for (int i = m + 1; i <= r; i++) {
106+
rightSumMax += a[i];
107+
if (rightSumMax > rightSideMax) {
108+
rightSideMax = rightSumMax;
109+
}
110+
}
111+
return Math.max(Math.max(maxLeft, maxRight), leftSideMax + rightSideMax);
112+
}
113+
114+
public static void main(String[] args) {
115+
MaxSubSum maxSubSum = new MaxSubSum();
116+
System.out.println(
117+
"Result : " + maxSubSum.maxSubArray(new int[] {4, -3, 5, -2, -1, 2, 6, -2}));
118+
}
119+
}

src/main/java/org/sean/array/IpChecker.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,67 @@
22

33
import java.util.ArrayList;
44
import java.util.Collections;
5+
import java.util.LinkedList;
56
import java.util.List;
67

78
/***
89
* 93. Restore IP Addresses
910
*/
1011
public class IpChecker {
12+
// region DFS
13+
private final List<String> out = new ArrayList<>();
14+
private boolean isValid(String segment) {
15+
if (segment.isEmpty() || segment.length() > 1 && segment.startsWith("0")) return false;
16+
int num = Integer.parseInt(segment);
17+
return num < 256;
18+
}
19+
20+
private void restore(String s, int startPos, LinkedList<String> address) {
21+
int size = address.size();
22+
int len = s.length();
23+
if (size == 3) {
24+
if (startPos >= len - 3) {
25+
String segment = s.substring(startPos);
26+
if (isValid(segment)) {
27+
StringBuilder stringBuilder = new StringBuilder();
28+
for (String value : address) {
29+
stringBuilder.append(value);
30+
stringBuilder.append('.');
31+
}
32+
stringBuilder.append(segment);
33+
34+
out.add(stringBuilder.toString());
35+
}
36+
}
37+
return;
38+
}
39+
40+
for (int i = 0; i < 3; i++) {
41+
if (startPos + i >= len) continue;
42+
43+
String addr = s.substring(startPos, startPos + i + 1);
44+
if (!isValid(addr)) {
45+
continue;
46+
}
47+
address.add(addr);
48+
restore(s, startPos + i + 1, address);
49+
address.removeLast();
50+
}
51+
}
52+
1153
public List<String> restoreIpAddresses(String s) {
54+
int length = s.length();
55+
if (length < 4 || length > 12) return Collections.emptyList();
56+
57+
out.clear();
58+
restore(s, 0, new LinkedList<>());
59+
60+
return out;
61+
}
62+
// endregion
63+
64+
// region iterative
65+
public List<String> restoreIpAddresses0(String s) {
1266
int length = s.length();
1367
if (length < 4 || length > 12)
1468
return Collections.emptyList();
@@ -58,4 +112,5 @@ public List<String> restoreIpAddresses(String s) {
58112

59113
return out;
60114
}
115+
//endregion
61116
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.sean.backtracking;
2+
3+
/***
4+
* 494. Target Sum
5+
*/
6+
public class TargetSum {
7+
public int findTargetSumWays(int[] nums, int S) {
8+
if (nums == null || nums.length == 0) return 0;
9+
10+
find(nums, 0, 0, S);
11+
return counter;
12+
}
13+
14+
int counter = 0;
15+
16+
void find(int[] sums, int i, int sum, int target) {
17+
int len = sums.length;
18+
if (i == len) {
19+
if (sum == target) ++counter;
20+
return;
21+
}
22+
int elem = sums[i];
23+
24+
find(sums, i + 1, sum + elem, target);
25+
find(sums, i + 1, sum - elem, target);
26+
}
27+
}

0 commit comments

Comments
 (0)