Skip to content

Commit 0a1bc74

Browse files
committed
Merge branch 'LC-55' into develop
2 parents f4d4f66 + 1dc3d6a commit 0a1bc74

File tree

4 files changed

+109
-1
lines changed

4 files changed

+109
-1
lines changed

LC-4/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Given two sorted arrays `nums1` and `nums2` of size `m` and `n` respectively, re
44

55
The overall run time complexity should be `O(log (m+n))`.
66

7+
> * Difficulty: **HARD**
8+
79
---
810
## Examples
911

@@ -44,7 +46,7 @@ Output: 2.00000
4446
- `0 <= m <= 1000`
4547
- `0 <= n <= 1000`
4648
- `1 <= m + n <= 2000`
47-
- `-106 <= nums1[i], nums2[i] <= 106`
49+
- `-10^6 <= nums1[i], nums2[i] <= 10^6`
4850

4951
---
5052
## Solutions

LC-55/LC-55-1.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <vector>
2+
3+
using namespace std;
4+
5+
class Solution {
6+
public:
7+
/**
8+
* Concepts: Dynamic Programming (DP)
9+
* 1. Create an array to store whether we can arrive this index
10+
* 2. Iterate entire array
11+
*/
12+
bool canJump(vector<int>& nums) {
13+
int numsLen = nums.size();
14+
if (numsLen <= 1) {
15+
return true;
16+
}
17+
18+
vector<bool> state(numsLen, false);
19+
state[0] = true;
20+
21+
for (int i = 1; i < numsLen; ++i) {
22+
for (int j = i -1; j >= 0; --j) {
23+
if (state[j] && j + nums[j] >= i) {
24+
state[i] = true;
25+
break;
26+
}
27+
}
28+
}
29+
return state[numsLen - 1];
30+
}
31+
};

LC-55/LC-55-2.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <vector>
2+
3+
using namespace std;
4+
5+
class Solution {
6+
public:
7+
/**
8+
* Concepts: Greedy Approach
9+
* 1. Create an array to store the maximum reach steps and get initial
10+
* 2. Iterate find the maximum reach steps:
11+
* - If the maxReach[i - 1] is lower than index i then return false
12+
* - Else get max(maxReach[i - 1], i + nums[i]) and if maxReach[i] can exceed (nums.size() - 1) then return true
13+
*/
14+
bool canJump(vector<int>& nums) {
15+
int numsLen = nums.size();
16+
if (numsLen <= 1) {
17+
return true;
18+
}
19+
20+
vector<int> maxReach(numsLen);
21+
maxReach[0] = nums[0];
22+
23+
for (int i = 1; i < numsLen; ++i) {
24+
if (maxReach[i - 1] < i) {
25+
return false;
26+
}
27+
maxReach[i] = max(maxReach[i - 1], i + nums[i]);
28+
if (maxReach[i] >= numsLen - 1) {
29+
return true;
30+
}
31+
}
32+
33+
return false;
34+
}
35+
};

LC-55/README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# LC-55 - Jump Game
2+
3+
You are given an integer array `nums`. You are initially positioned at the array's **first index**, and each element in the array represents your maximum jump length at that position.
4+
5+
Return `true` *if you can reach the last index, or `false` otherwise.*
6+
7+
> * Difficulty: **MEDIUM**
8+
9+
---
10+
## Examples
11+
12+
```
13+
Input: nums = [2,3,1,1,4]
14+
Output: true
15+
Explanation:
16+
Jump 1 step from index 0 to 1, then 3 steps to the last index.
17+
```
18+
19+
```
20+
Input: nums = [3,2,1,0,4]
21+
Output: false
22+
Explanation:
23+
You will always arrive at index 3 no matter what. Its maximum jump length is 0, which makes it impossible to reach the last index.
24+
```
25+
26+
---
27+
## Notes
28+
29+
- `1 <= nums.length <= 10^4`
30+
- `0 <= nums[i] <= 10^5`
31+
32+
---
33+
## Solutions
34+
35+
1. Dynamic Programming (DP)
36+
- Time complexity: $O(n^2)$
37+
- Space complexity: $O(n)$
38+
2. Greedy Approach
39+
- Time complexity: $O(n)$
40+
- Space complexity: $O(n)$

0 commit comments

Comments
 (0)