// File: min_cost_climbing_stairs_dp.go
// Created Time: 2023-07-23
// Author: Reanon (793584285@qq.com)

package chapter_dynamic_programming

import "math"

/* 爬楼梯最小代价：动态规划 */
func minCostClimbingStairsDP(cost []int) int {
	n := len(cost) - 1
	if n == 1 || n == 2 {
		return cost[n]
	}
	// 初始化 dp 表，用于存储子问题的解
	dp := make([]int, n+1)
	// 初始状态：预设最小子问题的解
	dp[1] = cost[1]
	dp[2] = cost[2]
	// 状态转移：从较小子问题逐步求解较大子问题
	for i := 3; i <= n; i++ {
		dp[i] = int(math.Min(float64(dp[i-1]), float64(dp[i-2]+cost[i])))
	}
	return dp[n]
}

/* 爬楼梯最小代价：空间优化后的动态规划 */
func minCostClimbingStairsDPComp(cost []int) int {
	n := len(cost) - 1
	if n == 1 || n == 2 {
		return cost[n]
	}
	// 初始状态：预设最小子问题的解
	a, b := cost[1], cost[2]
	// 状态转移：从较小子问题逐步求解较大子问题
	for i := 3; i <= n; i++ {
		tmp := b
		b = int(math.Min(float64(a), float64(tmp+cost[i])))
		a = tmp
	}
	return b
}
