int minCostClimbingStairs_opt1(vector<int>& cost) {
int sum1 = 0;
int i = 0;
while (i < cost.size()) {
// cout << cost[i] << endl;
sum1 += cost[i];
int a = cost[i+1]; // 1
int b = cost[i+2]; // 2
if (a >= b) {
i+=2;
continue;
} else {
i+=1;
continue;
}
}
return sum1;
}
int minCostClimbingStairs_opt2(vector<int>& cost) {
int sum1 = 0;
int i = 1;
while (i < cost.size()) {
// cout << cost[i] << endl;
sum1 += cost[i];
int a = cost[i+1]; // 1
int b = cost[i+2]; // 2
if (a >= b) {
i+=2;
continue;
} else {
i+=1;
continue;
}
}
return sum1;
}
int main() {
// 输入:cost = [1,100,1,1,1,100,1,1,100,1]
// 输出:6
int a[] = {1,100,1,1,1,100,1,1,100,1};
int b[] = {10,15,20};
vector<int>cost(a, a+10);
vector<int>cost1(b, b+3);
int ans1 = min(minCostClimbingStairs_opt1(cost),
minCostClimbingStairs_opt2(cost));
cout << ans1 << endl;
cout << "........." << endl;
int ans2 = min(minCostClimbingStairs_opt1(cost1),
minCostClimbingStairs_opt2(cost1));
cout << ans2 << endl;
cout << "............." << endl;
int c[] = {0,0,0,0};
vector<int>cc(c,c+4);
cout << min(minCostClimbingStairs_opt1(cc),
minCostClimbingStairs_opt2(cc)) << endl;
// cout << minCostClimbingStairs(cost1) << endl;
return 0;
}
aaaaaa
最新推荐文章于 2024-05-20 20:08:58 发布