Skip to content

Commit 41b326c

Browse files
committed
Implement min-cost-climbing-stairs
name: min-cost-climbing-stairs url: https://leetcode.com/problems/min-cost-climbing-stairs difficulty: 1 time: 98.58 ms time-rank: -1 % time-complexity: O(N) space: 13.5 MB space-rank: 100.0 % space-complexity: O(1) Fixes #301 Signed-off-by: Christopher Friedt <[email protected]>
1 parent a13fcc3 commit 41b326c

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

min-cost-climbing-stairs-test.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (c) 2021, Christopher Friedt
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*/
6+
7+
#include <gtest/gtest.h>
8+
9+
#include "min-cost-climbing-stairs.cpp"
10+
11+
using namespace std;
12+
13+
TEST(MinCostClimbingStairs, 10_15_20) {
14+
vector<int> cost = {10, 15, 20};
15+
EXPECT_EQ(15, Solution().minCostClimbingStairs(cost));
16+
}
17+
18+
TEST(MinCostClimbingStairs, 1_100_1_1_1_100_1_1_100_1) {
19+
vector<int> cost = {1, 100, 1, 1, 1, 100, 1, 1, 100, 1};
20+
EXPECT_EQ(6, Solution().minCostClimbingStairs(cost));
21+
}
22+
23+
TEST(MinCostClimbingStairs, 0_1_1_1_1) {
24+
vector<int> cost = {0, 1, 1, 1};
25+
EXPECT_EQ(1, Solution().minCostClimbingStairs(cost));
26+
}

min-cost-climbing-stairs.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright (c) 2021, Christopher Friedt
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*/
6+
7+
// clang-format off
8+
// name: min-cost-climbing-stairs
9+
// url: https://leetcode.com/problems/min-cost-climbing-stairs
10+
// difficulty: 1
11+
// clang-format on
12+
13+
#include <vector>
14+
15+
using namespace std;
16+
17+
class Solution {
18+
public:
19+
int minCostClimbingStairs(vector<int> &cost) {
20+
// clang-format off
21+
// C(i) = min(C(i - 1), C(i - 2)) + cost[i]
22+
// = cost[i], i < 2
23+
//
24+
// The exit criteria are to successfully arrive at either N-1 or N-2
25+
// since from N-2 it's possible to climb 2 stairs at once to get to N.
26+
//
27+
// Eg. v--- goal is 1 past the last array element
28+
// cost = [10,15,20]
29+
// i cost[i] C(i) C(i - 1) C(i - 2) comment
30+
// ----------------------------------------------
31+
// 0 10 10 0 0
32+
// 1 15 15 10 0 stop here, because i >= N - 2 && C(1) < cost[2] OR set C(2) = C(1)
33+
// 2 20 15* 15 10
34+
//
35+
// Eg.
36+
// cost = [1,100,1,1,1,100,1,1,100,1]
37+
// i cost[i] C(i) C(i - 1) C(i - 2) comment
38+
// ----------------------------------------------
39+
// 0 1 1 0 0
40+
// 1 100 100 1 0
41+
// 2 1 2 100 1
42+
// 3 1 3 2 100
43+
// 4 1 3 3 2
44+
// 5 100 103 3 3
45+
// 6 1 4 103 3
46+
// 7 1 5 4 103
47+
// 8 100 104 5 4
48+
// 9 1 6 104 5
49+
// Eg.
50+
// cost = [0,1,1,1]
51+
// i cost[i] C(i) C(i - 1) C(i - 2) comment
52+
// ----------------------------------------------
53+
// 0 0 0 0 0
54+
// 1 1 1 0 0
55+
// 2 1 1 1 0
56+
// 3 1 1* 1 1
57+
// clang-format on
58+
const int N = cost.size();
59+
for (int i = 2; i < N; ++i) {
60+
int t = cost[i] + min(cost[i - 1], cost[i - 2]);
61+
if (i == N - 1) {
62+
if (cost[i - 1] < t) {
63+
cost[i] = cost[i - 1];
64+
break;
65+
}
66+
}
67+
cost[i] = t;
68+
}
69+
70+
return cost[N - 1];
71+
}
72+
};

0 commit comments

Comments
 (0)