Skip to content

Commit 55fc93d

Browse files
committed
New Problem Solution - "1824. Minimum Sideway Jumps"
1 parent 613ef3c commit 55fc93d

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ LeetCode
99

1010
| # | Title | Solution | Difficulty |
1111
|---| ----- | -------- | ---------- |
12+
|1824|[Minimum Sideway Jumps](https://leetcode.com/problems/minimum-sideway-jumps/) | [C++](./algorithms/cpp/minimumSidewayJumps/MinimumSidewayJumps.cpp)|Medium|
1213
|1823|[Find the Winner of the Circular Game](https://leetcode.com/problems/find-the-winner-of-the-circular-game/) | [C++](./algorithms/cpp/findTheWinnerOfTheCircularGame/FindTheWinnerOfTheCircularGame.cpp)|Medium|
1314
|1822|[Sign of the Product of an Array](https://leetcode.com/problems/sign-of-the-product-of-an-array/) | [C++](./algorithms/cpp/signOfTheProductOfAnArray/SignOfTheProductOfAnArray.cpp)|Easy|
1415
|1819|[Number of Different Subsequences GCDs](https://leetcode.com/problems/number-of-different-subsequences-gcds/) | [C++](./algorithms/cpp/numberOfDifferentSubsequencesGcds/NumberOfDifferentSubsequencesGcds.cpp)|Hard|
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// Source : https://leetcode.com/problems/minimum-sideway-jumps/
2+
// Author : Hao Chen
3+
// Date : 2021-04-11
4+
5+
/*****************************************************************************************************
6+
*
7+
* There is a 3 lane road of length n that consists of n + 1 points labeled from 0 to n. A frog starts
8+
* at point 0 in the second lane and wants to jump to point n. However, there could be obstacles along
9+
* the way.
10+
*
11+
* You are given an array obstacles of length n + 1 where each obstacles[i] (ranging from 0 to 3)
12+
* describes an obstacle on the lane obstacles[i] at point i. If obstacles[i] == 0, there are no
13+
* obstacles at point i. There will be at most one obstacle in the 3 lanes at each point.
14+
*
15+
* For example, if obstacles[2] == 1, then there is an obstacle on lane 1 at point 2.
16+
*
17+
* The frog can only travel from point i to point i + 1 on the same lane if there is not an obstacle
18+
* on the lane at point i + 1. To avoid obstacles, the frog can also perform a side jump to jump to
19+
* another lane (even if they are not adjacent) at the same point if there is no obstacle on the new
20+
* lane.
21+
*
22+
* For example, the frog can jump from lane 3 at point 3 to lane 1 at point 3.
23+
*
24+
* Return the minimum number of side jumps the frog needs to reach any lane at point n starting from
25+
* lane 2 at point 0.
26+
*
27+
* Note: There will be no obstacles on points 0 and n.
28+
*
29+
* Example 1:
30+
*
31+
* Input: obstacles = [0,1,2,3,0]
32+
* Output: 2
33+
* Explanation: The optimal solution is shown by the arrows above. There are 2 side jumps (red arrows).
34+
* Note that the frog can jump over obstacles only when making side jumps (as shown at point 2).
35+
*
36+
* Example 2:
37+
*
38+
* Input: obstacles = [0,1,1,3,3,0]
39+
* Output: 0
40+
* Explanation: There are no obstacles on lane 2. No side jumps are required.
41+
*
42+
* Example 3:
43+
*
44+
* Input: obstacles = [0,2,1,0,3,0]
45+
* Output: 2
46+
* Explanation: The optimal solution is shown by the arrows above. There are 2 side jumps.
47+
*
48+
* Constraints:
49+
*
50+
* obstacles.length == n + 1
51+
* 1 <= n <= 5 * 10^5
52+
* 0 <= obstacles[i] <= 3
53+
* obstacles[0] == obstacles[n] == 0
54+
******************************************************************************************************/
55+
56+
class Solution {
57+
private:
58+
int min (int x, int y) {
59+
return x < y ? x : y;
60+
}
61+
int min(int x, int y, int z) {
62+
return min(x, min(y,z));
63+
}
64+
void print(vector<vector<int>>& matrix) {
65+
int n = matrix.size();
66+
int m = matrix[0].size();
67+
68+
for(int i=0; i<m; i++) {
69+
for (int j=0; j<n-1; j++){
70+
if (matrix[j][i] == n) {
71+
cout << setw(2) << "X"<<",";
72+
} else {
73+
cout << setw(2) <<matrix[j][i] << ",";
74+
}
75+
}
76+
cout << matrix[n-1][i] << endl;
77+
}
78+
}
79+
public:
80+
int minSideJumps(vector<int>& obstacles) {
81+
int n = obstacles.size();
82+
vector<vector<int>> dp(n, vector(3,0));
83+
dp[0][0] = dp[0][2] = 1;
84+
85+
for(int i = 1; i < n; i++){
86+
87+
dp[i][0] = dp[i-1][0];
88+
dp[i][1] = dp[i-1][1];
89+
dp[i][2] = dp[i-1][2];
90+
if (obstacles[i] > 0 ) dp[i][obstacles[i]-1] = n;
91+
92+
if (obstacles[i]-1 != 0 ) dp[i][0] = min(dp[i-1][0], dp[i][1]+1, dp[i][2]+1);
93+
if (obstacles[i]-1 != 1 ) dp[i][1] = min(dp[i][0]+1, dp[i-1][1], dp[i][2]+1);
94+
if (obstacles[i]-1 != 2 ) dp[i][2] = min(dp[i][0]+1, dp[i][1]+1, dp[i-1][2]);
95+
96+
}
97+
//print(dp);
98+
//cout << endl;
99+
return min(dp[n-1][0], dp[n-1][1], dp[n-1][2]);
100+
}
101+
};

0 commit comments

Comments
 (0)