Skip to content

Commit 5662225

Browse files
author
rchen102
committed
DP
1 parent 293d32b commit 5662225

File tree

10 files changed

+349
-92
lines changed

10 files changed

+349
-92
lines changed

Java/leetcode 1143.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Solution: 基本 dp T: O(nm) S: O(nm)
2+
class Solution {
3+
public int longestCommonSubsequence(String text1, String text2) {
4+
if (text1 == null || text2 == null) return 0;
5+
int len1 = text1.length();
6+
int len2 = text2.length();
7+
if (len1 == 0 || len2 == 0) return 0;
8+
9+
int[][] dp = new int[len1+1][len2+1];
10+
for (int i = 0; i <= len1; i++) {
11+
dp[i][0] = 0;
12+
}
13+
for (int j = 0; j <= len2; j++) {
14+
dp[0][j] = 0;
15+
}
16+
// 状态转移
17+
for (int i = 1; i <= len1; i++) {
18+
for (int j = 1; j <= len2; j++) {
19+
if (text1.charAt(i-1) == text2.charAt(j-1)) {
20+
dp[i][j] = dp[i-1][j-1] + 1;
21+
}
22+
else {
23+
dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]);
24+
}
25+
}
26+
}
27+
return dp[len1][len2];
28+
}
29+
}

Java/leetcode 169.java

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,42 @@
1-
//Solution1: HashMap Time: O(n) Space:0(n)
1+
//Solution: Boyer-Moore Majority Vote Algorithm Time:O(n) Space:O(1)
2+
class Solution {
3+
public int majorityElement(int[] nums) {
4+
if (nums == null || nums.length == 0) return 0;
5+
int major = nums[0];
6+
int times = 1;
7+
for (int i = 1; i < nums.length; i++) {
8+
int cur = nums[i];
9+
if (cur == major) times++;
10+
else {
11+
times--;
12+
if (times == 0) {
13+
times = 1;
14+
major = cur;
15+
}
16+
}
17+
}
18+
return major;
19+
}
20+
}
21+
22+
//Solution: Bit-Manipulation Time: O(n) Space: O(1)
23+
class Solution {
24+
public int majorityElement(int[] nums) {
25+
int res = 0, major = nums.length / 2;
26+
for(int i = 31; i >= 0; i--) {
27+
int pos = 0;
28+
for(int num : nums) {
29+
pos += (num >> i) & 1;
30+
}
31+
pos = pos > major ? 1 : 0;
32+
res |= (pos << i);
33+
}
34+
return res;
35+
}
36+
}
37+
38+
39+
//Solution: HashMap Time: O(n) Space:0(n)
240
class Solution {
341
public int majorityElement(int[] nums) {
442
Map<Integer, Integer> map = new HashMap<>();
@@ -16,7 +54,7 @@ public int majorityElement(int[] nums) {
1654
}
1755
}
1856

19-
//Solution2: Divide and Conquer Time: O(nlogn) Space: O(logn)
57+
//Solution: Divide and Conquer Time: O(nlogn) Space: O(logn)
2058
class Solution {
2159
public int majorityElement(int[] nums) {
2260
return findMajorityHelper(nums, 0, nums.length - 1);
@@ -38,36 +76,4 @@ private int findMajorityHelper(int[] nums, int start, int end) {
3876
}
3977
}
4078

41-
//Solution3: Boyer-Moore Majority Vote Algorithm Time:O(n) Space:O(1)
42-
class Solution {
43-
public int majorityElement(int[] nums) {
44-
int res = nums[0], count = 1;
45-
for(int i = 1; i < nums.length; i++) {
46-
if(count == 0) {
47-
res = nums[i];
48-
count++;
49-
}
50-
else if(res == nums[i])
51-
count++;
52-
else
53-
count--;
54-
}
55-
return res;
56-
}
57-
}
5879

59-
//Solution4: Bit-Manipulation Time: O(n) Space: O(1)
60-
class Solution {
61-
public int majorityElement(int[] nums) {
62-
int res = 0, major = nums.length / 2;
63-
for(int i = 31; i >= 0; i--) {
64-
int pos = 0;
65-
for(int num : nums) {
66-
pos += (num >> i) & 1;
67-
}
68-
pos = pos > major ? 1 : 0;
69-
res |= (pos << i);
70-
}
71-
return res;
72-
}
73-
}

Java/leetcode 300.java

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,4 @@
1-
// Solution1: binary search and dp T: O(nlogn) S: O(n)
2-
class Solution {
3-
public int lengthOfLIS(int[] nums) {
4-
int[] tails = new int[nums.length];
5-
int size = 0;
6-
for(int num : nums) {
7-
int low = 0, high = size;
8-
while(low != high) {
9-
int mid = (low + high) / 2;
10-
if(tails[mid] < num)
11-
low = mid + 1;
12-
else
13-
high = mid;
14-
}
15-
tails[low] = num;
16-
if(low == size) ++size;
17-
}
18-
return size;
19-
}
20-
}
21-
22-
//Solution more easy understand: T: O(n^2) S: O(n)
1+
// Solution1: Basic dp T: O(n^2) S: O(n)
232
class Solution {
243
public int lengthOfLIS(int[] nums) {
254
if(nums == null || nums.length == 0) return 0;
@@ -36,4 +15,33 @@ public int lengthOfLIS(int[] nums) {
3615
}
3716
return res;
3817
}
18+
}
19+
20+
// Solution2: 某扑克牌算法 T: O(nlogn) S: O(n)
21+
class Solution {
22+
public int lengthOfLIS(int[] nums) {
23+
if (nums == null || nums.length == 0) return 0;
24+
int[] tops = new int[nums.length];
25+
int piles = 0;
26+
for (int i = 0; i < nums.length; i++) {
27+
int card = nums[i];
28+
int find = binarySearchLeftBound(tops, piles, card);
29+
if (find == piles) {
30+
piles++;
31+
}
32+
tops[find] = card;
33+
}
34+
return piles;
35+
}
36+
37+
public int binarySearchLeftBound(int[] nums, int len, int target) {
38+
int lo = 0, hi = len - 1;
39+
while (lo <= hi) {
40+
int mid = lo + ((hi - lo) >> 1);
41+
if (nums[mid] < target) lo = mid + 1;
42+
else if (nums[mid] > target) hi = mid - 1;
43+
else hi = mid - 1;
44+
}
45+
return lo;
46+
}
3947
}

Java/leetcode 416.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Solution: 基本 dp T: O(nk) S: O(nk) k = sum(nums)
2+
class Solution {
3+
public boolean canPartition(int[] nums) {
4+
if (nums == null || nums.length == 0) return true;
5+
int len = nums.length;
6+
// 如果和为奇数,肯定分不了
7+
int sum = Arrays.stream(nums).sum();
8+
if (sum % 2 != 0) return false;
9+
sum = sum/2;
10+
11+
// 初始化 Base case
12+
boolean[][] dp = new boolean[len+1][sum+1];
13+
for (int i = 0; i <= len; i++) {
14+
dp[i][0] = true;
15+
}
16+
// 物品为 0 时,无物品可装,注意 dp[0][0] = true
17+
for (int j = 1; j <= sum; j++) {
18+
dp[0][j] = false;
19+
}
20+
// 状态转移
21+
for (int i = 1; i <= len; i++) {
22+
for (int j = 1; j <= sum; j++) {
23+
// 背包容量不足
24+
if (j < nums[i-1]) {
25+
dp[i][j] = dp[i-1][j];
26+
}
27+
else {
28+
dp[i][j] = dp[i-1][j] || dp[i-1][j - nums[i-1]];
29+
}
30+
}
31+
}
32+
return dp[len][sum];
33+
}
34+
}
35+
36+
// 优化:状态压缩
37+
class Solution {
38+
public boolean canPartition(int[] nums) {
39+
if (nums == null || nums.length == 0) return true;
40+
int len = nums.length;
41+
// 如果和为奇数,肯定分不了
42+
int sum = Arrays.stream(nums).sum();
43+
if (sum % 2 != 0) return false;
44+
sum = sum/2;
45+
46+
// 初始化 Base case
47+
boolean[] dp = new boolean[sum+1];
48+
dp[0] = true;
49+
50+
// 状态转移
51+
for (int i = 1; i <= len; i++) {
52+
for (int j = sum; j >= 1; j--) {
53+
if (j >= nums[i-1]) {
54+
dp[j] = dp[j] || dp[j-nums[i-1]];
55+
}
56+
}
57+
}
58+
return dp[sum];
59+
}
60+
}

Java/leetcode 5.java

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,61 @@
1-
// Solution1: T: O(n^2) S: O(1)
1+
// Solution1: 衍生法,少有的,暴力比 dp 更好 T: O(n^2) S: O(1)
22
class Solution {
33
public String longestPalindrome(String s) {
4-
int len = s.length();
5-
int max = 0;
6-
int idx = 0;
7-
for (int i = 0; i < len; i++) {
8-
int len1 = extend(s, i, i); // assume odd length, try to extend palindrome as possible
9-
int len2 = extend(s,i, i+1); // assume enven length
10-
if (Math.max(len1, len2) > max) {
11-
// Update
12-
max = Math.max(len1, len2);
13-
idx = len1 > len2 ? (i - len1/2) : (i - len2/2 + 1);
14-
}
4+
if (s == null || s.length() == 0) return s;
5+
int n = s.length();
6+
String res = "";
7+
for (int i = 0; i < n; i++) {
8+
String r1 = findPalindrome(s, i, i);
9+
String r2 = findPalindrome(s, i, i+1);
10+
if (r1.length() > res.length()) res = r1;
11+
if (r2.length() > res.length()) res = r2;
1512
}
16-
return s.substring(idx, idx + max);
13+
return res;
14+
1715
}
1816

19-
private int extend(String s, int i, int j) {
20-
int len = s.length();
21-
for (; i >= 0 && j < len; i--, j++) {
22-
if (s.charAt(i) != s.charAt(j)) break;
17+
public String findPalindrome(String str, int l, int r) {
18+
while (l >= 0 && r < str.length()) {
19+
if (str.charAt(l) == str.charAt(r)) {
20+
l--;
21+
r++;
22+
}
23+
else {
24+
break;
25+
}
2326
}
24-
return j - i + 1 -2; // Current 2 unmatched chars
27+
return str.substring(l+1, r);
2528
}
2629
}
2730

28-
// Solution2: Dynamic Programming T: O(n^2) S: O(n^2)
31+
// Solution: dp T: O(n^2) S: O(n^2)
2932
class Solution {
3033
public String longestPalindrome(String s) {
31-
int len = s.length();
32-
boolean[][] dp = new boolean[len][len];
34+
if (s == null || s.length() == 0) return s;
35+
int n = s.length();
36+
boolean[][] dp = new boolean[n][n];
37+
// base case
38+
for (int i = 0; i < n; i++) {
39+
dp[i][i] = true;
40+
}
41+
// 推导
3342
String res = "";
34-
for (int i = len - 1; i >= 0; i--) {
35-
for(int j = i; j < len; j++) {
36-
if (j - i <= 2) dp[i][j] = (s.charAt(i) == s.charAt(j));
37-
else dp[i][j] = (s.charAt(i) == s.charAt(j)) && dp[i+1][j-1];
38-
if (dp[i][j] && res.length() < j - i + 1) {
39-
res = s.substring(i, j + 1);
43+
int maxLen = 0;
44+
for (int i = n-1; i >= 0; i--) {
45+
for (int j = i+1; j < n; j++) {
46+
if (j - i == 1) {
47+
dp[i][j] = (s.charAt(i) == s.charAt(j));
48+
}
49+
else {
50+
dp[i][j] = (dp[i+1][j-1] && (s.charAt(i) == s.charAt(j)));
51+
}
52+
if (dp[i][j] && j - i + 1 > maxLen) {
53+
maxLen = j - i + 1;
54+
res = s.substring(i, j+1);
4055
}
4156
}
4257
}
43-
return res;
58+
if (maxLen != 0) return res;
59+
return String.valueOf(s.charAt(0));
4460
}
4561
}

Java/leetcode 516.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Solution: 基本 dp T: O(n^2) S: O(n^2)
2+
class Solution {
3+
public int longestPalindromeSubseq(String s) {
4+
if (s == null || s.length() == 0) return 0;
5+
int n = s.length();
6+
7+
int[][] dp = new int[n][n];
8+
// base case
9+
for (int i = 0; i < n; i++) {
10+
dp[i][i] = 1;
11+
}
12+
// 推导
13+
for (int i = n - 1; i >= 0; i--) {
14+
for (int j = i+1; j < n; j++) {
15+
if (s.charAt(i) == s.charAt(j)) {
16+
dp[i][j] = dp[i+1][j-1] + 2;
17+
}
18+
else {
19+
dp[i][j] = Math.max(dp[i+1][j], dp[i][j-1]);
20+
}
21+
}
22+
}
23+
return dp[0][n-1];
24+
}
25+
}

0 commit comments

Comments
 (0)