forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to lc problem: No.0312. Burst Balloons
- Loading branch information
Showing
6 changed files
with
256 additions
and
36 deletions.
There are no files selected for viewing
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
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
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,21 @@ | ||
class Solution { | ||
public: | ||
int maxCoins(vector<int>& nums) { | ||
nums.insert(nums.begin(), 1); | ||
nums.push_back(1); | ||
int n = nums.size(); | ||
vector<vector<int>> dp(n, vector<int>(n)); | ||
for (int l = 2; l < n; ++l) | ||
{ | ||
for (int i = 0; i + l < n; ++i) | ||
{ | ||
int j = i + l; | ||
for (int k = i + 1; k < j; ++k) | ||
{ | ||
dp[i][j] = max(dp[i][j], dp[i][k] + dp[k][j] + nums[i] * nums[k] * nums[j]); | ||
} | ||
} | ||
} | ||
return dp[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,28 @@ | ||
func maxCoins(nums []int) int { | ||
vals := make([]int, len(nums)+2) | ||
for i := 0; i < len(nums); i++ { | ||
vals[i+1] = nums[i] | ||
} | ||
n := len(vals) | ||
vals[0], vals[n-1] = 1, 1 | ||
dp := make([][]int, n) | ||
for i := 0; i < n; i++ { | ||
dp[i] = make([]int, n) | ||
} | ||
for l := 2; l < n; l++ { | ||
for i := 0; i+l < n; i++ { | ||
j := i + l | ||
for k := i + 1; k < j; k++ { | ||
dp[i][j] = max(dp[i][j], dp[i][k]+dp[k][j]+vals[i]*vals[k]*vals[j]) | ||
} | ||
} | ||
} | ||
return dp[0][n-1] | ||
} | ||
|
||
func max(a, b int) int { | ||
if a > b { | ||
return a | ||
} | ||
return b | ||
} |
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 |
---|---|---|
@@ -1,38 +1,19 @@ | ||
class Solution { | ||
|
||
public int maxCoins(int[] nums) { | ||
if (nums == null || nums.length == 0) { | ||
return 0; | ||
} | ||
int n = nums.length; | ||
int[][] f = new int[n + 2][n + 2]; | ||
for (int i= 0; i < n + 2; ++i) { | ||
for (int j = 0; j < n + 2; ++j) { | ||
f[i][j] = -1; | ||
int[] vals = new int[nums.length + 2]; | ||
vals[0] = 1; | ||
vals[vals.length - 1] = 1; | ||
System.arraycopy(nums, 0, vals, 1, nums.length); | ||
int n = vals.length; | ||
int[][] dp = new int[n][n]; | ||
for (int l = 2; l < n; ++l) { | ||
for (int i = 0; i + l < n; ++i) { | ||
int j = i + l; | ||
for (int k = i + 1; k < j; ++k) { | ||
dp[i][j] = Math.max(dp[i][j], dp[i][k] + dp[k][j] + vals[i] * vals[k] * vals[j]); | ||
} | ||
} | ||
} | ||
int[] bak = new int[n + 2]; | ||
bak[0] = bak[n + 1] = 1; | ||
for (int i = 1; i < n + 1; ++i) { | ||
bak[i] = nums[i - 1]; | ||
} | ||
return dp(bak, f, 0, n + 1); | ||
} | ||
|
||
private int dp(int[] nums, int[][] f, int x, int y) { | ||
if (f[x][y] != -1) { | ||
return f[x][y]; | ||
} | ||
|
||
f[x][y] = 0; | ||
|
||
//枚举最后一个戳破的气球的位置 | ||
for (int i = x + 1; i < y; ++i) { | ||
f[x][y] = Math.max(f[x][y], nums[i] * nums[x] * nums[y] + dp(nums,f, x, i) + dp(nums, f, i, y)); | ||
} | ||
return f[x][y]; | ||
|
||
return dp[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,11 @@ | ||
class Solution: | ||
def maxCoins(self, nums: List[int]) -> int: | ||
nums = [1] + nums + [1] | ||
n = len(nums) | ||
dp = [[0] * n for _ in range(n)] | ||
for l in range(2, n): | ||
for i in range(n - l): | ||
j = i + l | ||
for k in range(i + 1, j): | ||
dp[i][j] = max(dp[i][j], dp[i][k] + dp[k][j] + nums[i] * nums[k] * nums[j]) | ||
return dp[0][-1] |