-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Lecture 116: Min Score Triangulation of Polygon
- Loading branch information
1 parent
f3bafc6
commit 8bc02de
Showing
5 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains 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
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); | ||
} | ||
}; |
This file contains 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
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); | ||
} | ||
}; |
This file contains 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
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); | ||
} | ||
}; |
This file contains 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
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 | |