Last active
June 14, 2022 15:42
-
-
Save raihankhan/70887f1d66457cd9b9f7fa41d65a30f4 to your computer and use it in GitHub Desktop.
Leetcode daily solutions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Leetcode daily challenge - 14.06.22 | |
// https://leetcode.com/problems/delete-operation-for-two-strings | |
// Top Down DP approach - O(n*m) space and O(n^2) time complexity | |
class Solution { | |
public: | |
int minDistance(string word1, string word2) { | |
int n = word1.size(),m = word2.size(); | |
int dp[n+1][m+1]; | |
memset(dp , 0 , sizeof(dp)); | |
for(int i=1;i<=n;i++) { | |
for(int j=1;j<=m;j++) { | |
if(word1[i-1] == word2[j-1]) { | |
dp[i][j] = 1+dp[i-1][j-1]; | |
} else { | |
dp[i][j] = max(dp[i-1][j],dp[i][j-1]); | |
} | |
} | |
} | |
return n+m-(dp[n][m]<<1); | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Leetcode daily challenge - 13.06.22 | |
// https://leetcode.com/problems/triangle | |
// Bottom up DP approach - O(1) space and O(n^2) time complexity | |
class Solution { | |
public: | |
int minimumTotal(vector<vector<int>>& triangle) { | |
for(int i=triangle.size()-2;i>=0;i--) { | |
for(int j=0;j<=i;j++) { | |
triangle[i][j]+=min(triangle[i+1][j],triangle[i+1][j+1]); | |
} | |
} | |
return triangle[0][0]; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment