Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
shinmao
algorithm
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
algorithm
/
leetcode
/
leetcode312.cpp
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
49 lines (43 loc) · 1.82 KB
main
Breadcrumbs
algorithm
/
leetcode
/
leetcode312.cpp
Copy path
Top
File metadata and controls
Code
Blame
49 lines (43 loc) · 1.82 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* 首先讓子問題獨立:
* 如果我們將dp[i]定義為戳破前i個氣球的最大點數
* 很明顯的,戳破dp[i]和dp[i + 1]是會互相影響的
* 我們把問題轉化一下:
* 加上頭和尾的兩顆虛擬氣球
* 現在氣球的index變成0~n+1
* 我們要把整排氣球戳破,最後剩下0和n+1
*/
class Solution {
public:
int maxCoins(vector<int>& nums) {
int n = nums.size();
if(n == 1)
return nums[0];
vector<int> balls(n + 2, 1);
for(int i = 1; i <= n; i++)
balls[i] = nums[i - 1];
// dp[i][j]: maximum coin got after bursting balloons between ith and jth
// not include ith and jth (所以dp數組定義的是開區間)
// 狀態轉移方程很巧妙,指定最後一個戳破的氣球
// 如果最後一個戳破的氣球是kth
// dp[i][j] = dp[i][k] + dp[k][j] + balls[i]*balls[k]*balls[j]
vector<vector<int>> dp(n + 2, vector<int>(n + 2, 0));
// 上面的狀態轉移方程搞出來後
// dp依賴的必須是以前已經被計算出答案的狀態
// 這會決定我們的遍歷順序(畫個圖會發現要用對角線遍歷,這邊我們由下往上遍歷來取代)
// 首先我們j >= i
// base case for i == j
// 由於開區間,這代表區間內沒有任何一個氣球可以讓我們爆
for(int i = 0; i <= n + 1; i++)
dp[i][i] = 0;
// 由下往上遍歷
for(int i = n; i >= 0; i--) {
for(int j = i + 1; j <= n + 1; j++) {
for(int k = i + 1; k < j; k++) {
dp[i][j] = max(dp[i][j], dp[i][k] + dp[k][j] + balls[i]*balls[k]*balls[j]);
}
}
}
return dp[0][n + 1];
}
};
You can’t perform that action at this time.