Advertisement

Min Cost Climbing Stairs - LeetCode 746 Solution

Min Cost Climbing Stairs - Complete Solution Guide

Min Cost Climbing Stairs is LeetCode problem 746, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are given an integer array cost where cost[i] is the cost of i th step on a staircase. Once you pay the cost, you can either climb one or two steps. You can either start from the step with index 0 , or the step with index 1 . Return the minimum cost to reach the top of the floor . Example 1: Input: cost = [10, 15 ,20] Output: 15 Explanation: You will start at index 1. - Pay 15 and climb two steps to reach the top. The total cost is 15. Example 2: Input: cost = [ 1 ,100, 1 ,1, 1 ,100, 1 , 1 ,

Detailed Explanation

The problem asks us to find the minimum cost to reach the top of a staircase, given an array `cost` where `cost[i]` represents the cost of the i-th step. We can start at either step 0 or step 1. After paying the cost of a step, we can climb one or two steps forward. The goal is to find the path that minimizes the total cost to reach the 'top', which can be thought of as reaching beyond the last step of the `cost` array.

Solution Approach

The provided solution uses dynamic programming to calculate the minimum cost to reach each step of the staircase. A DP array `dp` of size `n+1` is created, where `dp[i]` represents the minimum cost to reach the i-th step (or 'top' if i=n). The base cases are `dp[0] = 0` and `dp[1] = 0`, because we can start from step 0 or step 1 without paying any cost to *reach* those steps. The solution then iterates from step 2 up to step `n` (inclusive), calculating `dp[i]` as the minimum of: (1) the cost to reach the previous step (`dp[i-1]`) plus the cost of the previous step (`cost[i-1]`), and (2) the cost to reach the step two steps before (`dp[i-2]`) plus the cost of that step (`cost[i-2]`). Finally, the solution returns `dp[n]`, which represents the minimum cost to reach the top of the stairs.

Step-by-Step Algorithm

  1. Step 1: Initialize a DP array `dp` of size `n+1` with all elements set to 0, where `n` is the length of the `cost` array. `dp[i]` represents the minimum cost to *reach* step `i`.
  2. Step 2: Set `dp[0] = 0` and `dp[1] = 0`, as it costs nothing to reach the first and second starting points.
  3. Step 3: Iterate from `i = 2` to `n`: `dp[i] = min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2])`. This computes the minimum cost to reach the i-th step by either coming from the (i-1)-th step or the (i-2)-th step.
  4. Step 4: Return `dp[n]`, the minimum cost to reach the top of the staircase.

Key Insights

  • Insight 1: Dynamic Programming is well-suited because the optimal solution for reaching a step depends on the optimal solutions for the previous one or two steps.
  • Insight 2: The problem has overlapping subproblems. The minimum cost to reach step `i` is influenced by the minimum costs to reach steps `i-1` and `i-2`.
  • Insight 3: We can define dp[i] as the minimum cost to reach step `i`. The top of the staircase is considered the (n+1)-th step, where n is the size of `cost` array. Hence, we return dp[n].

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Array, Dynamic Programming.

Companies

Asked at: Accenture.