Skip to main content

699. Falling Squares - LeetCode Solution

699. Falling Squares
On an infinite number line (x-axis), we drop given squares in the order they are given.
The i-th square dropped (positions[i] = (left, side_length)) is a square with the left-most point being positions[i][0] and sidelength positions[i][1].
The square is dropped with the bottom edge parallel to the number line, and from a higher height than all currently landed squares. We wait for each square to stick before dropping the next.
The squares are infinitely sticky on their bottom edge, and will remain fixed to any positive length surface they touch (either the number line or another square). Squares dropped adjacent to each other will not stick together prematurely.
 
Return a list ans of heights. Each height ans[i] represents the current highest height of any square we have dropped, after dropping squares represented by positions[0], positions[1], ..., positions[i].
Example 1:
Input: [[1, 2], [2, 3], [6, 1]]
Output: [2, 5, 5]
Explanation:
After the first drop of positions[0] = [1, 2]: _aa _aa ------- The maximum height of any square is 2.
After the second drop of positions[1] = [2, 3]: __aaa __aaa __aaa _aa__ _aa__ -------------- The maximum height of any square is 5. The larger square stays on top of the smaller square despite where its center of gravity is because squares are infinitely sticky on their bottom edge.
After the third drop of positions[1] = [6, 1]: __aaa __aaa __aaa _aa _aa___a -------------- The maximum height of any square is still 5. Thus, we return an answer of [2, 5, 5].
 
 
Example 2:
Input: [[100, 100], [200, 100]]
Output: [100, 100]
Explanation: Adjacent squares don't get stuck prematurely - only their bottom edge can stick to surfaces.
 
Note:
  • 1 <= positions.length <= 1000.
  • 1 <= positions[i][0] <= 10^8.
  • 1 <= positions[i][1] <= 10^6.

Intuition : 
  • convert the points to form : {height,start,end}
  • iterate over points one by one in the given order and keep adding these points to a map
  • Initially, the map is empty, so simply add the first point 
  • Now after this every time there are four cases to cover:
  • The purple block is the most recently added block with max-height
  • The magenta block is the new block to be added

  • In any of these cases: we update the height of the new block as new block + old block
  • And then add this block to the map

  • And at the end of each traversal
  • Max height can be easily obtained from the end element of the map
  • Note: case 4 is automatically covered by case 3 and 2


CODE:
vector<int> fallingSquares(vector<vector<int>>& pos) {
      map<vector<int>,int> points;
      vector<int> ans;   
      for(auto& v : pos){
          vector<int> temp = {v[1],v[0],v[0]+v[1]};
          if(points.size()!=0){
              for(auto it=points.rbegin();it!=points.rend();it++){
                 if(
                     (temp[1]>= it->first[1] && temp[2]<=it->first[2])||
                     (temp[1]<=it->first[1] && temp[2]>it->first[1])||
                     (temp[1]<it->first[2] && temp[2]>=it->first[2])
                   ){
                     temp[0] += it->first[0];
                     break;
                 }
              }
          }
          points[temp]++;
          ans.push_back(points.rbegin()->first[0]);
      }
        return ans;
    }

Comments

Popular posts from this blog

All About Flutter

F L U T T E R What is Flutter? Flutter is an open-source mobile SDK developer can use to build native-looking Android and iOS applications from the same code base. Flutter has been around since 2015 when Google introduced it and remained in the beta stage before its official launch in December 2018. Since then, the buzz around Flutter has been growing stronger. Widgets The central idea behind Flutter is the use of widgets. It’s by combining different widgets that developers can build the entire UI. Each of these widgets defines a structural element (like a button or menu), a stylistic element (a font or color scheme), a layout aspect (like padding), and many others. Flutter also provides developers with reactive-style views. To avoid performance issues deriving from using a compiled programming language to serve as the JavaScript bridge, Flutter uses Dart. It compiles Dart   ahead of time (AOT)   into the native code for multiple platforms. Flutter also provides developers wit...

Are you smart?

S ocrates once said " I know one thing, that I know nothing " Hello humans, Put on a helmet, because after reading this your mind may explode 😨 (just kidding). I will be sharing with you some famous paradoxes that will hit your brain hard. Let's begin  1. Do you have a brother?   Consider a family, where there is a mother, father, and two children. One of the children is a boy, then what are the chances of the second one being a girl. Is it 1/2, because the other children can be either a boy or a girl. But, wait a minute...   There are four possible combinations of two children ( Boy, Boy), (Boy, Girl),(Girl, Boy) and (Girl, Girl). Since one of the children is a boy, so the combination can not be (Girl, Girl), this means we are left with 3 possible combinations. This gives us the chances of the other children being a boy as 1/3. Uhhh... this is confusing is it 1/2 or 1/3. You tell me🤪 2. Crocodility Once upon a time, a mother and a son visited a zoo. Accidentally, the ...