Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
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 }}
chenwenhang
AlgorithmPractice
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
AlgorithmPractice
/
src
/
recursion_and_dynamic_programming
/
MinCostClimbingStairs.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
58 lines (36 loc) · 1.47 KB
master
Breadcrumbs
AlgorithmPractice
/
src
/
recursion_and_dynamic_programming
/
MinCostClimbingStairs.java
Copy path
Top
File metadata and controls
Code
Blame
58 lines (36 loc) · 1.47 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
package recursion_and_dynamic_programming;
/**
* @Author: Wenhang Chen
* @Description:数组的每个索引做为一个阶梯,第 i个阶梯对应着一个非负数的体力花费值 cost[i](索引从0开始)。 每当你爬上一个阶梯你都要花费对应的体力花费值,然后你可以选择继续爬一个阶梯或者爬两个阶梯。
* <p>
* 您需要找到达到楼层顶部的最低花费。在开始时,你可以选择从索引为 0 或 1 的元素作为初始阶梯。
* <p>
* 示例 1:
* <p>
* 输入: cost = [10, 15, 20]
* 输出: 15
* 解释: 最低花费是从cost[1]开始,然后走两步即可到阶梯顶,一共花费15。
* 示例 2:
* <p>
* 输入: cost = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1]
* 输出: 6
* 解释: 最低花费方式是从cost[0]开始,逐个经过那些1,跳过cost[3],一共花费6。
* 注意:
* <p>
* cost 的长度将会在 [2, 1000]。
* 每一个 cost[i] 将会是一个Integer类型,范围为 [0, 999]
* @Date: Created in 9:28 3/8/2020
* @Modified by:
*/
public class MinCostClimbingStairs {
public int minCostClimbingStairs(int[] cost) {
if (cost == null || cost.length < 1) return 0;
int[] dp = new int[cost.length];
dp[0] = cost[0];
dp[1] = cost[1];
for (int i = 2; i < dp.length; i++) {
dp[i] = Math.min(dp[i - 2], dp[i - 1]) + cost[i];
}
return Math.min(dp[dp.length - 1], dp[dp.length - 2]);
}
}
You can’t perform that action at this time.