Wayfair
  • OA
    • Karat
      • 811. Subdomain Visit Count
      • Ads Conversion Rate
      • Recommend Movie
      • Longest Common Continuous Subarray
      • Course Overlap
      • Halfway courses
      • Find one rectangle
      • Find all rectangles
      • Find Multiple Shapes
      • word wrap
      • word processor
      • Basic Calculator
      • Basic Calculator with parenthesis
      • 带变量计算器
      • Valid Matrix
      • nonogram
      • Node with 0 or 1 parents
      • 两个节点是否有公共祖先
      • 最远祖先
      • invalid Badge Records
      • 一小时内access多次
      • canSchedule
      • spareTime
      • sparse vector
      • sparse vector 实现add,dot和cos
      • userlogs earliest and latest access time
      • resource Access with in 5 min
      • Find Word Path in Grid
      • Find legal moves
      • 找能去的所有0区域
      • 最短路径找treasure
  • VO
    • Coding
      • Valid Palindrome
      • Add String
      • Coupon
    • System design
    • BQ
    • OOD
  • SD
  • LeetCode Tag
  • VO Onsite
Powered by GitBook
On this page
  1. OA
  2. Karat

Find all rectangles

PreviousFind one rectangleNextFind Multiple Shapes

Last updated 3 years ago

for the same image, it is filled with 0s and 1s. It may have multiple rectangles filled with 0s. The rectangles are separated by 1s. Find all the rectangles.

public class Main {
    public List<List<Integer>> findMultipleRectangle(int[][] board)
    {
        List<List<Integer>> res = new ArrayList<>();

        for(int i = 0; i< board.length; i++)
        {
            for (int j = 0; j< board[0].length; j++)
            {
                if(board[i][j] == 0)
                {
                    List<Integer> rectangle = new ArrayList<Integer>();
                    rectangle.add(i);
                    rectangle.add(j);
                    board[i][j] = 1;

                    int topLeftX = i;
                    int topLeftY = j;
                    while(topLeftX < board.length && board[topLeftX][j] == 0)
                    {
                        board[topLeftX][j] = 1;
                        topLeftX++;
                    }
                    while(topLeftY < board[0].length && board[i][topLeftY] == 0)
                    {
                        board[i][topLeftY] = 1;
                        topLeftY++;
                    }
                    rectangle.add(topLeftX);
                    rectangle.add(topLeftY);
                    res.add(rectangle);
                }
            }
        }
        return rectangle;
    }
}

https://stackoverflow.com/questions/65411142/finding-rectangles-in-the-image
https://www.geeksforgeeks.org/find-rectangles-filled-0/