[554] C language LeetCode brush. Brick wall (M)

In front of you there is a wall square, brick wall consisting of a plurality of lines of bricks. These bricks of the same height but different widths. You now want to draw a top-down, a minimum vertical line through the bricks.

Brick wall is represented by a list of lines. Each row is a list of integers representative of the width of each block from left to right.

If you just draw a line through from the edge of the brick, it is not through this brick. You need to find out how to draw this line in order to make the bricks through the minimum number, and returns the number of bricks through.

One of the two vertical edges of objects that you can not along the wall, this is clearly not through a brick.

 

Example:

Input: [[1,2,2,1],
      [3,1,2],
      [3,2],
      [2,4],
      [3,1,2],
      [1,3,1, 1]]

Output: 2

prompt:

Each row of the block and the width should be equal and can not exceed INT_MAX.
The number of bricks per row in the range [1,10,000], the height of the wall is in the range [1,10,000], the total number of bricks does not exceed 20,000.

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/brick-wall
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

A hash of this question, the statistical number of different widths, the width of sum + = wall [i] [j]; number hashbrick [sum] ++ ;, recording max, is returned wallSize - max.

#define INTMAX 20000000

int leastBricks(int** wall, int wallSize, int* wallColSize){
    int i, j;
    int sum = 0;
    int max = 0;
    int hashbrick[INTMAX] = {0};
    
    for (i = 0; i < wallSize; i++) {
        sum = 0;
        for (j = 0; j < wallColSize[i] - 1; j++) {
            sum += wall[i][j];
            hashbrick[sum]++;
            
            if (max < hashbrick[sum]) {
                max = hashbrick[sum];
            }
        }
    }

    return wallSize - max;
}

 

Published 149 original articles · won praise 17 · views 110 000 +

Guess you like

Origin blog.csdn.net/jin615567975/article/details/104417394