Skip to content

Commit e196041

Browse files
committed
README + 518 + 723
1 parent cfebb42 commit e196041

File tree

5 files changed

+128
-0
lines changed

5 files changed

+128
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
Time spent : 10 min(self)
2+
3+
GIVEN: an unsorted array of integers
4+
5+
RETURNS: The length of longest continuous increasing subsequence(subarray)
6+
7+
**Example 1:**
8+
9+
```
10+
Input: [1,3,5,4,7]
11+
Output: 3
12+
Explanation: The longest continuous increasing subsequence is [1,3,5], its length is 3.
13+
Even though [1,3,5,7] is also an increasing subsequence, it's not a continuous one where 5 and 7 are separated by 4.
14+
15+
```
16+
17+
**Example 2:**
18+
19+
```
20+
Input: [2,2,2,2,2]
21+
Output: 1
22+
Explanation: The longest continuous increasing subsequence is [2], its length is 1.
23+
```
24+
25+
26+
27+
Sliding window
28+
29+
Every increasing subsequence is disjoint, and the boundary of each such subsequence occurs whenever nums[i - 1] >= nums[i]. When it does, it marks the start of a new increasing subsequence at nums[i], and we store such i in the variable anchor.
30+
31+
32+
33+
Time complexity: O(N)
34+
35+
Space complexity: O(1)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
public int findLengthOfLCIS(int[] nums) {
3+
if (nums.length == 0) {
4+
return 0;
5+
}
6+
int count = 1;
7+
int max = 0;
8+
for (int i = 1; i < nums.length; i++) {
9+
if (nums[i] > nums[i - 1]) {
10+
count++;
11+
}
12+
else {
13+
max = Math.max(count, max);
14+
count = 1;
15+
}
16+
}
17+
max = Math.max(count, max);
18+
return max;
19+
}
20+
}
21+
22+
class Solution {
23+
public int findLengthOfLCIS(int[] nums) {
24+
int ans = 0;
25+
int anchor = 0;
26+
for (int i = 0; i < nums.length; i++) {
27+
if (i > 0 && nums[i - 1] >= nums[i]) {
28+
anchor = i;
29+
}
30+
ans = Math.max(ans, i - anchor + 1);
31+
}
32+
return ans;
33+
}
34+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
Time spent : 15min(See hint)
2+
3+
GIVEN: coins of different denominations and a total amount of money
4+
5+
RETURNS: the number of combinations that make up that amount
6+
7+
**Note**:
8+
You may assume that you have an infinite number of each kind of coin.
9+
10+
**Example 1:**
11+
12+
```
13+
Input: amount = 5, coins = [1, 2, 5]
14+
Output: 4
15+
Explanation: there are four ways to make up the amount:
16+
5=5
17+
5=2+2+1
18+
5=2+1+1+1
19+
5=1+1+1+1+1
20+
21+
```
22+
23+
**Example 2:**
24+
25+
```
26+
Input: amount = 3, coins = [2]
27+
Output: 0
28+
Explanation: the amount of 3 cannot be made up just with coins of 2.
29+
30+
```
31+
32+
**Example 3:**
33+
34+
```
35+
Input: amount = 10, coins = [10]
36+
Output: 1
37+
```
38+
39+
40+
41+
For each coin, we can calculate how many combinations there is for every amount. When coin is 1, all the amount just have 1 combinations. For other coins, dp[i] = dp[i] + dp[i - coin], dp[i] means combinations for amount i.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public int change(int amount, int[] coins) {
3+
int[] dp = new int[amount + 1];
4+
dp[0] = 1;
5+
for (int coin : coins) {
6+
for (int i = 1; i <= amount; i++) {
7+
if (coin <= i) {
8+
dp[i] += dp[i - coin];
9+
}
10+
}
11+
}
12+
return dp[amount];
13+
}
14+
}

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,8 @@
294294
| 290 | 75 | [Sort Colors][75] | Java | Medium | 2018.2.28 | Sort | |
295295
| 291 | 435 | [Non-overlapping Intervals][435] | Java | Medium | 2018.2.28 | Greedy | |
296296
| 292 | 134 | [Gas Station][134] | Java | Medium | 2018.3.1 | Greedy | |
297+
| 293 | 518 | [Coin Change 2][518] | Java | Medium | 2018.3.1 | Dynamic Programming | |
298+
| 294 | 723 | [Longest Continuous Increasing Subsequence][723] | Java | Easy | 2018.3.1 | Array | |
297299

298300

299301
[1]:<https://leetcode.com/problems/two-sum/> "Two Sum"
@@ -516,6 +518,7 @@
516518
[503]:<https://leetcode.com/problems/next-greater-element-ii/> "Next Greater Element II"
517519
[504]:<https://leetcode.com/problems/base-7/> "Base 7"
518520
[516]:<https://leetcode.com/problems/longest-palindromic-subsequence/> "Longest Palindromic Subsequence"
521+
[518]:<https://leetcode.com/problems/coin-change-2/> "Coin Change 2"
519522
[520]:<https://leetcode.com/problems/detect-capital/> "Detect Capital"
520523
[521]:<https://leetcode.com/problems/longest-uncommon-subsequence-i/> "Longest Uncommon Subsequence I"
521524
[532]:<https://leetcode.com/problems/k-diff-pairs-in-an-array/> "K-diff pairs in an array"
@@ -575,6 +578,7 @@
575578
[712]:<https://leetcode.com/problems/minimum-ascii-delete-sum-for-two-strings/> "Minimum ASCII delete Sum for Two Strings"
576579
[717]:<https://leetcode.com/problems/1-bit-and-2-bit-characters/> "1-bit and 2-bit characters"
577580
[720]:<https://leetcode.com/problems/longest-word-in-dictionary/> "Longest Word in Dictionary"
581+
[723]:<https://leetcode.com/problems/longest-continuous-increasing-subsequence/> "Longest Continuous Increasing Subsequence"
578582
[725]:<https://leetcode.com/problems/split-linked-list-in-parts/> "Split Linked List in Parts"
579583
[728]:<https://leetcode.com/problems/self-dividing-numbers/> "Self Dividing Numbers"
580584
[733]:<https://leetcode.com/problems/flood-fill/> "Flood Fill"

0 commit comments

Comments
 (0)