Climbing Stairs
Solution in go
func climbStairs(n int) int {
    if n <= 0 {
        return 0
    }
    if n == 1 {
        return 1
    }
    if n == 2 {
        return 2
    }

    nMinusOne := 1
    nMinusTwo := 1

    for i := 2; i <= n; i++ {
        // Expanded form:
        // total := nMinusOne + nMinnusTwo
        // nMinusTwo = nMinusOne
        // nMinusOne = total
        nMinusTwo, nMinusOne = nMinusOne, nMinusOne + nMinusTwo
    }

    return nMinusOne
}
    TheDeveloperCafe © 2022-2024