Skip to main content

MAY-21 2020 Challenge

Count Square Submatrices with All Ones


Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.

Example 1:
Input: matrix =
[
  [0,1,1,1],
  [1,1,1,1],
  [0,1,1,1]
]
Output: 15
Explanation: 
There are 10 squares of side 1.
There are 4 squares of side 2.
There is  1 square of side 3.
Total number of squares = 10 + 4 + 1 = 15.
Example 2:
Input: matrix = 
[
  [1,0,1],
  [1,1,0],
  [1,1,0]
]
Output: 7
Explanation: 
There are 6 squares of side 1.  
There is 1 square of side 2. 
Total number of squares = 6 + 1 = 7.

Constraints:


1 <= arr.length <= 300
1 <= arr[0].length <= 300
0 <= arr[i][j] <= 1

Solution in C++

class Solution {
public:
    int n,m,cnt=0;
    int mat[302][302],dp[302][302];
    int  dfs(int i,int j)
    {
        if(i>=n||j>=m)
            return 0;
        if(mat[i][j]==0)
            return 0;
        if(dp[i][j]!=-1)
            return dp[i][j];
        int p=dfs(i+1,j);
        int q=dfs(i,j+1);
        int r=dfs(i+1,j+1);
        return dp[i][j]=1+min(p,min(q,r));
    }
    int countSquares(vector<vector<int>>& matrix) {
        int ans=0;
        for(int i=0;i<matrix.size();i++)
        {
            
            for(int j=0;j<matrix[i].size();j++)
            {
               mat[i][j]=matrix[i][j];
            }
        }
        memset(dp,-1,sizeof(dp));
        n=matrix.size();
        m=matrix[0].size();
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(mat[i][j]==1)
                {
                   ans+= dfs(i,j);
                }}}
        return ans;
    }};


Leetcode May 21


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