Advertisement
Marvin48

Brick Wall

Apr 24th, 2021
651
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <math.h>
  4.  
  5. using namespace std;
  6.  
  7. class Solution {
  8. public:
  9.     int leastBricks(vector<vector<int>>& wall) {
  10.         int width = getWidth(wall[0]);
  11.         vector<int> dp (width * 2 - 1, 0);
  12.         float step = 0.5;
  13.        
  14.         for (vector<int> line : wall) {
  15.             float cursor = 0.0, curWidth = 0.0;
  16.             int dpPos = 0, brickPos = 0;
  17.            
  18.             while (cursor < width) {
  19.                 if (cursor == curWidth) {
  20.                     curWidth += line[brickPos++];
  21.                 }
  22.                 cursor += step;
  23.                 cursor < curWidth ?  dp[dpPos++]++ : dpPos++;
  24.             }
  25.         }
  26.        
  27.         int min = INT_MAX;
  28.         for (int val : dp)
  29.             min = fmin(min, val);
  30.        
  31.         return min;
  32.     }
  33.    
  34.     int getWidth(vector<int>& line) {
  35.         int width = 0;
  36.         for (int brick : line)
  37.             width += brick;
  38.         return width;
  39.     }
  40. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement