Skip to main content

Unique Paths - Leetcode 62 - Python

There is a robot on an m x n grid. The robot is initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in time.


Given the two integers m and n, return the number of possible unique paths that the robot can take to reach the bottom-right corner.


The test cases are generated so that the answer will be less than or equal to 2 * 109. 



class Solution:
    def uniquePaths(self, m: int, n: int) -> int:
        r = [1]*n
        
        for i in range(m-1):
            nr = [1] * n
            for j in range(n-2, -1, -1):
                nr[j] = nr[j+1] + r[j]
            r = nr
			
        return r[0]


Explaination :






Comments

Popular posts from this blog

May-6 2020 Challenge

  6.   Majority Element Given an array of size  n , find the majority element. The majority element is the element that appears  more than   ⌊ n/2 ⌋  times. You may assume that the array is non-empty and the majority element always exist in the array. Example 1: Input: [3,2,3] Output: 3 Example 2: Input: [2,2,1,1,1,2,2] Output: 2 Solution in Java  class Solution {     public int majorityElement(int[] num) {         int m = num[0], cnt= 1;     for (int i = 1; i < num.length; i++) {         if (cnt == 0) {             m= num[i];             cnt = 1;         } else if (num[i] == m) {             cnt++;         } else              cnt--;    }      return m;...

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...

Leetcode 58. Meeting Rooms II. Python (Easiest Explaination)

  58.  Meeting Rooms II Description Given an array of meeting time intervals consisting of start and end times  [[s1,e1],[s2,e2],...] (si < ei) , find the minimum number of conference rooms required.) (0,8),(8,10) is not conflict at 8 Example Example1 Input : intervals = [(0,30),(5,10),(15,20)] Output : 2 Explanation : We need two meeting rooms room1 : ( 0 , 30 ) room2 : ( 5 , 10 ),( 15 , 20 ) Example2 Input: intervals = [( 2 , 7 )] Output: 1 Explanation: Only need one meeting room Solution : """ Definition of Interval: class Interval(object):     def __init__(self, start, end):         self.start = start         self.end = end """ class Solution:     """     @param intervals: an array of meeting time intervals     @return: the minimum number of conference rooms required     """     def min_meeting_rooms( self , intervals: List[Interval]) -> int :   ...