Skip to main content

Climbing Stairs - Leetcode 70 - Python - Dynamic Programming

Climbing Stairs


You are climbing a staircase. It takes n steps to reach the top.


Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?


 


Example 1:


Input: n = 2

Output: 2

Explanation: There are two ways to climb to the top.

1. 1 step + 1 step

2. 2 steps

Example 2:


Input: n = 3

Output: 3

Explanation: There are three ways to climb to the top.

1. 1 step + 1 step + 1 step

2. 1 step + 2 steps

3. 2 steps + 1 step





class Solution:

    def climbStairs(self, n: int) -> int:

        f,s = 1, 1

        for i in range(n-1):

            t = f

            f = f + s

            s = t

        return f



Explaination :





 

Comments

Popular posts from this blog

Leetcode 417. Pacific Atlantic Water Flow. Python (Dfs Graph)

417 .  Pacific Atlantic Water Flow There is an  m x n  rectangular island that borders both the  Pacific Ocean  and  Atlantic Ocean . The  Pacific Ocean  touches the island's left and top edges, and the  Atlantic Ocean  touches the island's right and bottom edges. The island is partitioned into a grid of square cells. You are given an  m x n  integer matrix  heights  where  heights[r][c]  represents the  height above sea level  of the cell at coordinate  (r, c) . The island receives a lot of rain, and the rain water can flow to neighboring cells directly north, south, east, and west if the neighboring cell's height is  less than or equal to  the current cell's height. Water can flow from any cell adjacent to an ocean into the ocean. Return  a  2D list  of grid coordinates  result  where  result[i] = [r i , c i ]  denotes that rain water can flow fro...

Leetcode 424. Longest Repeating Character Replacement. Python (Sliding Window)

  424 .  Longest Repeating Character Replacement You are given a string  s  and an integer  k . You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most  k  times. Return  the length of the longest substring containing the same letter you can get after performing the above operations .   Example 1: Input: s = "ABAB", k = 2 Output: 4 Explanation: Replace the two 'A's with two 'B's or vice versa. Example 2: Input: s = "AABABBA", k = 1 Output: 4 Explanation: Replace the one 'A' in the middle with 'B' and form "AABBBBA". The substring "BBBB" has the longest repeating letters, which is 4.   Constraints: 1 <= s.length <= 10 5 s  consists of only uppercase English letters. 0 <= k <= s.length Solution :  class Solution: def characterReplacement(self, s: str, k: int) -> int: hm = {} ans = 0 ...

Leetcode 190. Reverse Bits. Python. Bit Manipulation

  190 .  Reverse Bits Reverse bits of a given 32 bits unsigned integer. Note: Note that in some languages, such as Java, there is no unsigned integer type. In this case, both input and output will be given as a signed integer type. They should not affect your implementation, as the integer's internal binary representation is the same, whether it is signed or unsigned. In Java, the compiler represents the signed integers using  2's complement notation . Therefore, in  Example 2  above, the input represents the signed integer  -3  and the output represents the signed integer  -1073741825 .   Example 1: Input: n = 00000010100101000001111010011100 Output: 964176192 (00111001011110000010100101000000) Explanation: The input binary string 00000010100101000001111010011100 represents the unsigned integer 43261596, so return 964176192 which its binary representation is 00111001011110000010100101000000 . Example 2: Input: n = 11111111111111111111...