Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <math.h>
- using namespace std;
- class Solution {
- public:
- int leastBricks(vector<vector<int>>& wall) {
- int width = getWidth(wall[0]);
- vector<int> dp (width * 2 - 1, 0);
- float step = 0.5;
- for (vector<int> line : wall) {
- float cursor = 0.0, curWidth = 0.0;
- int dpPos = 0, brickPos = 0;
- while (cursor < width) {
- if (cursor == curWidth) {
- curWidth += line[brickPos++];
- }
- cursor += step;
- cursor < curWidth ? dp[dpPos++]++ : dpPos++;
- }
- }
- int min = INT_MAX;
- for (int val : dp)
- min = fmin(min, val);
- return min;
- }
- int getWidth(vector<int>& line) {
- int width = 0;
- for (int brick : line)
- width += brick;
- return width;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement