Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8086959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:36:51+00:00 2026-06-05T18:36:51+00:00

given an n*m matrix with the possible values of 1, 2 and null: .

  • 0

given an n*m matrix with the possible values of 1, 2 and null:

  . . . . . 1 . .
  . 1 . . . . . 1
  . . . 2 . . . .
  . . . . 2 . . .
  1 . . . . . 1 .
  . . . . . . . .
  . . 1 . . 2 . .
  2 . . . . . . 1

I am looking for all blocks B (containing all values between (x0,y0) and (x1,y1)) that:

  • contain at least one ‘1’
  • contain no ‘2’
  • are not a subset of a another block with the above properties

Example:

blocks

The red, green and blue area all contain an ‘1’, no ‘2’, and are not part of a larger area.
There are of course more than 3 such blocks in this picture. I want to find all these blocks.

what would be a fast way to find all these areas?

I have a working brute-force solution, iterating over all possible rectangles, checking if they fulfill the first two criteria; then iterating over all found rectangles, removing all rectangles that are contained in another rectangle; and I can speed that up by first removing consecutive identical rows and columns. But I am fairly certain that there is a much faster way.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T18:36:53+00:00Added an answer on June 5, 2026 at 6:36 pm

    I finally found a solution that works almost in linear time (there is a small factor depending on the number of found areas). I think this is the fastest possible solution.

    Inspired by this answer: https://stackoverflow.com/a/7353193/145999 (pictures also taken from there)

    First, I go trought the matrix by column, creating a new matrix M1 measuring the number of steps to the last ‘1’ and a matrix M2 measuring the number of steps to the last ‘2’
    M1 & M2

    imagine a ‘1’ or ‘2’ in any of the grey blocks in the above picture

    in the end I have M1 and M2 looking like this:

    enter image description here

    No go through M1 and M2 in reverse, by row:

    enter image description here

    I execute the following algorithm:

     foundAreas = new list()
    
     For each row y backwards:
         potentialAreas = new List()
         for each column x:
            if M2[x,y]>M2[x-1,y]:
                add to potentialAreas: 
                    new Area(left=x,height=M2[x,y],hasOne=M1[x,y],hasTop=false)
            if M2[x,y]<M2[x-1,y]:
                for each area in potentialAreas:
                     if area.hasTop and area.hasOne<area.height:
                            add to foundAreas:
                                 new Box(Area.left,y-area.height,x,y)
                if M2[x,y]==0: delete all potentialAreas
                else:
                     find the area in potentialAreas with height=M2[x,y] 
                     or the one with the closest bigger height: set its height to M2[x,y]
                     delete all potentialAreas with a bigger height
    
                for each area in potentialAreas:
                     if area.hasOne>M1[x,y]: area.hasOne=M1[x,y]
                     if M2[x,y+1]==0: area.hasTop=true
    

    now foundAreas contains all rectangles with the desired properties.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a matrix M and smaller matrices with different possible values, I seek to
Given an n-dimensional matrix of values: what is the most efficient way of retrieving
I'm trying to write a program in Java that when given an MxN matrix
Currently am developing a program that solves (if possible) any given labyrinth of dimensions
I have a function that returns a comparison matrix from a given list: def
I am messing around in a program that performs all rendering using a matrix
Given a positive integer n, I want to generate all possible n bit combinations
I have a matrix (5x10000) with the fifth line contains values ​​between 1 and
When given a square matrix, what would be the best way to find all
is it possible to find the View that is displayed at a given absolute

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.