Skip to content

Commit a82587f

Browse files
committed
[Function add]
1. Add leetcode solutions with tag dp
1 parent 366005b commit a82587f

File tree

3 files changed

+167
-1
lines changed

3 files changed

+167
-1
lines changed

leetcode/300. Longest Increasing Subsequence.md

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,87 @@ class Solution {
5959
return result;
6060
}
6161
}
62-
```
62+
```
63+
64+
### Third time
65+
* Method 1: dp O(N^2)
66+
* dp[i]: the max increasing subarray up to current index.
67+
```Java
68+
class Solution {
69+
public int lengthOfLIS(int[] nums) {
70+
int len = nums.length;
71+
if(len == 0) return 0;
72+
int[] dp = new int[len];
73+
Arrays.fill(dp, 1);
74+
int max = 1;
75+
for(int i = 1; i < len; i++){
76+
for(int j = i; j >= 0; j--){
77+
if(nums[i] > nums[j]){
78+
dp[i] = Math.max(dp[i], dp[j] + 1);
79+
}
80+
max = Math.max(max, dp[i]);
81+
}
82+
}
83+
return max;
84+
}
85+
}
86+
```
87+
88+
* Method 2: BST O(NlgN)
89+
* we insert the values to tree in order and before inserting a node, we need to update the values.
90+
```Java
91+
class Solution {
92+
private static class Node{
93+
int val, count;
94+
Node left, right;
95+
public Node(int val, int count){
96+
this.val = val;
97+
this.count = count;
98+
}
99+
}
100+
private Node root;
101+
private int search(Node node, int val){
102+
if(node == null) return 0;
103+
int count = node.count;
104+
int leftMax = 0, rightMax = 0;
105+
if(val == node.val){
106+
return search(node.left, val);
107+
}else if(val < node.val){ // current node is at left side
108+
return search(node.left, val);
109+
}else{ // val > node.val
110+
leftMax = search(node.left, val);
111+
rightMax = search(node.right, val);
112+
return Math.max(count, Math.max(leftMax, rightMax));
113+
}
114+
}
115+
private void insert(Node node, int val, int count){
116+
int cur = node.val;
117+
if(cur == val){
118+
node.count = count;
119+
}else if(cur < val){
120+
if(node.right == null) node.right = new Node(val, count);
121+
else insert(node.right, val, count);
122+
}else{
123+
if(node.left == null) node.left = new Node(val, count);
124+
else insert(node.left, val, count);
125+
}
126+
}
127+
public int lengthOfLIS(int[] nums) {
128+
int len = nums.length;
129+
if(len == 0) return 0;
130+
else if(len == 1) return 1;
131+
this.root = new Node(nums[0], 1);
132+
int max = 1;
133+
for(int i = 1; i < len; i++){
134+
int count = search(root, nums[i]) + 1;
135+
insert(root, nums[i], count);
136+
max = Math.max(max, count);
137+
}
138+
return max;
139+
}
140+
}
141+
```
142+
143+
144+
145+
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
## 673. Number of Longest Increasing Subsequence
2+
3+
### Question
4+
Given an unsorted array of integers, find the number of longest increasing subsequence.
5+
6+
```
7+
Example 1:
8+
9+
Input: [1,3,5,4,7]
10+
Output: 2
11+
Explanation: The two longest increasing subsequence are [1, 3, 4, 7] and [1, 3, 5, 7].
12+
13+
Example 2:
14+
15+
Input: [2,2,2,2,2]
16+
Output: 5
17+
Explanation: The length of longest continuous increasing subsequence is 1, and there are 5 subsequences' length is 1, so output 5.
18+
```
19+
20+
Note: Length of the given array will be not exceed 2000 and the answer is guaranteed to be fit in 32-bit signed int.
21+
22+
### Solution
23+
* Method 1: dp
24+
* dp[i]: up to current index, the max number of increasing subset.
25+
* count[i]: the number of ways to keep the maximum dp number up to index i.
26+
* update the maximum: we update the count to the previous ones, since we only need to append current value to all previous ones.
27+
* if max is the same, we need to append current value to all previous ways.
28+
```Java
29+
class Solution {
30+
public int findNumberOfLIS(int[] nums) {
31+
int len = nums.length;
32+
if(len == 0) return 0;
33+
int[] dp = new int[len];
34+
int[] count = new int[len];
35+
Arrays.fill(dp, 1);
36+
Arrays.fill(count, 1);
37+
int max = 1;
38+
for(int i = 1; i < len; i++){
39+
for(int j = i - 1; j >= 0; j--){
40+
if(nums[i] > nums[j] && dp[j] + 1 > dp[i]){
41+
dp[i] = dp[j] + 1;
42+
count[i] = count[j];
43+
}else if(nums[i] > nums[j] && dp[j] + 1 == dp[i]){
44+
count[i] += count[j];
45+
}
46+
}
47+
max = Math.max(max, dp[i]);
48+
}
49+
int res = 0;
50+
for(int i = 0; i < len; i++){
51+
if(dp[i] == max){
52+
res += count[i];
53+
}
54+
}
55+
return res;
56+
}
57+
}
58+
```
59+
60+
### Reference
61+
1. [673. Number of Longest Increasing Subsequence 最长递增子序列的个数](https://blog.csdn.net/lanchunhui/article/details/51611970)

leetcode/72. Edit Distance.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,25 @@ class Solution {
9494
}
9595
}
9696
```
97+
98+
### Third time
99+
* Method 1: dp[i][j] This question is not difficult right now.
100+
* dp[i][j]: The minimum actions up to index i in String A and index j in String B.
101+
```Java
102+
class Solution {
103+
public int minDistance(String word1, String word2) {
104+
int len1 = word1.length(), len2 = word2.length();
105+
int[][] dp = new int[len1 + 1][len2 + 1];
106+
for(int i = 0; i <= len1; i++) dp[i][0] = i;
107+
for(int i = 0; i <= len2; i++) dp[0][i] = i;
108+
char[] arr1 = word1.toCharArray(), arr2 = word2.toCharArray();
109+
for(int i = 1; i <= len1; i++){
110+
for(int j = 1; j <= len2; j++){
111+
if(arr1[i - 1] == arr2[j - 1]) dp[i][j] = dp[i - 1][j - 1];
112+
else dp[i][j] = Math.min(dp[i - 1][j - 1], Math.min(dp[i - 1][j], dp[i][j - 1])) + 1;
113+
}
114+
}
115+
return dp[len1][len2];
116+
}
117+
}
118+
```

0 commit comments

Comments
 (0)