Skip to content

Commit

Permalink
Lecture 116: Min Score Triangulation of Polygon
Browse files Browse the repository at this point in the history
  • Loading branch information
kishanrajput23 authored Nov 1, 2024
1 parent f3bafc6 commit 8bc02de
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 0 deletions.
Binary file added Lectures/Lecture_116/Lecture_116_Hands_On.pdf
Binary file not shown.
23 changes: 23 additions & 0 deletions Lectures/Lecture_116/Lecture_Codes/1039_1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// recusion --> gives TLE

class Solution {
public:
int solve(vector<int> &values, int i, int j) {
if (i+1 == j) {
return 0;
}

int ans = INT_MAX;

for (int k=i+1; k<j; k++) {
ans = min(ans, values[i]*values[j]*values[k] + solve(values, i, k) + solve(values, k, j));
}

return ans;
}

int minScoreTriangulation(vector<int>& values) {
int n = values.size();
return solve(values, 0, n-1);
}
};
30 changes: 30 additions & 0 deletions Lectures/Lecture_116/Lecture_Codes/1039_2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// recursion + memoization

class Solution {
public:
int solve(vector<int> &values, int i, int j, vector<vector<int>> &dp) {
if (i+1 == j) {
return 0;
}

if (dp[i][j] != -1) {
return dp[i][j];
}

int ans = INT_MAX;

for (int k=i+1; k<j; k++) {
ans = min(ans, values[i]*values[j]*values[k] + solve(values, i, k, dp) + solve(values, k, j, dp));
}

dp[i][j] = ans;

return dp[i][j];
}

int minScoreTriangulation(vector<int>& values) {
int n = values.size();
vector<vector<int>>dp(n, vector<int>(n, -1));
return solve(values, 0, n-1, dp);
}
};
29 changes: 29 additions & 0 deletions Lectures/Lecture_116/Lecture_Codes/1039_3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// tabulation method

class Solution {
public:
int solve(vector<int> &values) {
int n = values.size();

vector<vector<int>>dp(n, vector<int>(n, 0));

for (int i=n-1; i>=0; i--) {
for (int j=i+2; j<n; j++) {
int ans = INT_MAX;

for (int k=i+1; k<j; k++) {
ans = min(ans, values[i]*values[j]*values[k] + dp[i][k] + dp[k][j]);
}

dp[i][j] = ans;
}
}

return dp[0][n-1];
}

int minScoreTriangulation(vector<int>& values) {

return solve(values);
}
};
5 changes: 5 additions & 0 deletions Lectures/Lecture_116/Lecture_Codes/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Lecture Codes

| **Problem** | **Solution** | **Level** |
|:--------------|:--------------:|:-----------:|
| [1039. Minimum Score Triangulation of Polygon](https://leetcode.com/problems/minimum-score-triangulation-of-polygon/description/) | [Solution1](), [Solution2](), [Solution3]() | Medium |

0 comments on commit 8bc02de

Please sign in to comment.