-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimumSidewayJumps.cpp
More file actions
25 lines (23 loc) · 909 Bytes
/
minimumSidewayJumps.cpp
File metadata and controls
25 lines (23 loc) · 909 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Source: https://leetcode.com/problems/minimum-sideway-jumps/
// Author: Miao Zhang
// Date: 2021-06-10
class Solution {
public:
int minSideJumps(vector<int>& obstacles) {
int n = obstacles.size();
vector<vector<int>> dp(n, vector<int>(3, INT_MAX / 2));
dp[0][1] = 0;
dp[0][0] = 1;
dp[0][2] = 1;
for(int i = 1; i < n; ++i)
{
if(obstacles[i] != 1) dp[i][0] = dp[i-1][0];
if(obstacles[i] != 2) dp[i][1] = dp[i-1][1];
if(obstacles[i] != 3) dp[i][2] = dp[i-1][2];
if(obstacles[i] != 1) dp[i][0] = min(dp[i][0], min(dp[i][1], dp[i][2]) + 1);
if(obstacles[i] != 2) dp[i][1] = min(dp[i][1], min(dp[i][0], dp[i][2]) + 1);
if(obstacles[i] != 3) dp[i][2] = min(dp[i][2], min(dp[i][0], dp[i][1]) + 1);
}
return min(dp[n-1][0], min(dp[n-1][1], dp[n-1][2]));
}
};