Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: solved day 20 medium #108

Merged
merged 1 commit into from
Apr 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion Medium/Day 20/solution.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,50 @@
// Write your code here.
// Write your code here.
//Code by Jagpreet153
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main()
{
int h, w;
cin >> h >> w;
vector<vector<int> > m(h, vector<int>(w)), dp(h, vector<int>(w, 0));
for(int i = 0; i < h; i++)
{
string s;
cin >> s;
for(int j = 0; j < w; j++) {
if(s[j] == '#') m[i][j] = 1;
else m[i][j] = 0;
}
}
if(m[0][0] == 1) dp[0][0] = 1;
else dp[0][0] = 0;

for(int i = 0; i < h; i++) for(int j = 0; j < w; j++)
{
if(i == 0 && j == 0)
continue;
if(i == 0)
{
if(m[i][j-1]==0 && m[i][j])
dp[i][j] = dp[i][j-1] + 1;
else
dp[i][j] = dp[i][j-1];
}
else if(j == 0)
{
if(m[i-1][j]==0 && m[i][j])
dp[i][j] = dp[i-1][j] + 1;
else
dp[i][j] = dp[i-1][j];
}
else
{
dp[i][j] = min(dp[i-1][j]+((m[i-1][j]==0 && m[i][j])?1:0), dp[i][j-1]+((m[i][j-1]==0 && m[i][j])?1:0));
}
}

cout << dp[h-1][w-1] << endl;
return 0;
}
Loading