Let's make this one less mysterious. For 1039. Minimum Score Triangulation of Polygon, the solution in this repository is mainly a dynamic programming solution.
Guide
What?
Before optimizing anything, pin down what information is still useful after each move. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: dynamic programming.
The notes already sitting in the source point us in the right direction:
- recursion
- TLE
- 73 / 93 test cases passed.
Guide
When?
Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are minScoreTriangulation.
Guide
Why?
The solution works because it narrows the problem until every update has a clear reason to exist.
- The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Initialize the memory or helper structure.
- Process candidates in the order the invariant expects.
- Update the answer only when the current state is valid.
- Return the value that represents the fully processed input.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//recursion
02//TLE
03//73 / 93 test cases passed.
04class Solution {
05public:
06 int minScoreTriangulation(vector<int>& A, int i, int j) {
07 // if(j == 0) j = A.size()-1;
08
09 //we cannot find a k between i and j
10 if(j - i < 2) return 0;
11
12 int res = INT_MAX;
13
14 for(int k = i+1; k <= j-1; k++){
15 // cout << i << ", " << k << ", " << j << endl;
16 res = min(res,
17 minScoreTriangulation(A, i, k) + A[i]*A[k]*A[j] +
18 minScoreTriangulation(A, k, j));
19 }
20
21 return res;
22 }
23
24 int minScoreTriangulation(vector<int>& A) {
25 int n = A.size();
26 return minScoreTriangulation(A, 0, n-1);
27 }
28};
29
30//recursion + memo
31//https://leetcode.com/problems/minimum-score-triangulation-of-polygon/discuss/286753/C%2B%2B-with-picture
32//Runtime: 12 ms, faster than 28.53% of C++ online submissions for Minimum Score Triangulation of Polygon.
33//Memory Usage: 8.7 MB, less than 100.00% of C++ online submissions for Minimum Score Triangulation of Polygon.
34class Solution {
35public:
36 vector<vector<int>> memo;
37
38 int minScoreTriangulation(vector<int>& A, int i, int j) {
39 //we cannot find a k between i and j
40 if(j - i < 2) return 0;
41
42 if(memo[i][j] != INT_MAX){
43 return memo[i][j];
44 }
45
46 for(int k = i+1; k <= j-1; k++){
47 // cout << i << ", " << k << ", " << j << endl;
48 memo[i][j] = min(memo[i][j],
49 minScoreTriangulation(A, i, k) + A[i]*A[k]*A[j] +
50 minScoreTriangulation(A, k, j));
51 }
52
53 return memo[i][j];
54 }
55
56 int minScoreTriangulation(vector<int>& A) {
57 int n = A.size();
58 memo = vector<vector<int>>(n, vector<int>(n, INT_MAX));
59 return minScoreTriangulation(A, 0, n-1);
60 }
61};
62
63//DP
64//https://leetcode.com/problems/minimum-score-triangulation-of-polygon/discuss/286753/C%2B%2B-with-picture
65//Runtime: 8 ms, faster than 75.04% of C++ online submissions for Minimum Score Triangulation of Polygon.
66//Memory Usage: 8.5 MB, less than 100.00% of C++ online submissions for Minimum Score Triangulation of Polygon.
67//time: O(N^3), space: O(N^2)
68class Solution {
69public:
70 int minScoreTriangulation(vector<int>& A) {
71 int n = A.size();
72 vector<vector<int>> dp(n, vector<int>(n, 0));
73
74 for(int i = n-1; i >= 0; i--){
75 for(int j = i+1; j < n; j++){
76 for(int k = i+1; k <= j-1; k++){
77 // cout << i << ", " << k << ", " << j << endl;
78 /*
79 dp[0][2] = dp[0][1] + A[0]*A[1]*A[0] + dp[1][2],
80 at this time, dp[0][1] and dp[1][2] should be 0
81 */
82 dp[i][j] = min(dp[i][j] == 0 ? INT_MAX : dp[i][j],
83 dp[i][k] + A[i]*A[k]*A[j] + dp[k][j]);
84 }
85 }
86 }
87
88 return dp[0][n-1];
89 }
90};
Cost