NC388. 砖墙的垂线
描述
示例1
输入:
[[1,2,2,1],[2,4],[3,1,2],[6],[3,3]]
输出:
2
C 解法, 执行用时: 55ms, 内存消耗: 7556KB, 提交时间: 2022-07-18
int brickwall(int** wall, int wallRowLen, int* wallColLen ) { int h = 0; for (int j = 0; j < wallColLen[0]; j++) { h += wall[0][j]; } int a[h + 1]; memset(a, 0, sizeof(a)); for (int i = 0; i < wallRowLen; i++) { h = 0; for (int j = 0; j < wallColLen[i]; j++) { for (int k = 1; k < wall[i][j]; k++) { a[h+k] += 1; } h += wall[i][j]; } } int z = wallRowLen; for (int i = 1; i < h; i++) { if (a[i] < z) { z = a[i]; } } return z; }
C 解法, 执行用时: 57ms, 内存消耗: 7436KB, 提交时间: 2022-06-28
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param wall int整型二维数组 * @param wallRowLen int wall数组行数 * @param wallColLen int* wall数组列数 * @return int整型 * * C语言声明定义全局变量请加上static,防止重复定义 */ int brickwall(int** wall, int wallRowLen, int* wallColLen ) { int width = 0; for (int j = 0; j < wallColLen[0]; j++) { width += wall[0][j]; } int hash[width + 1]; memset(hash, 0, sizeof(hash)); for (int i = 0; i < wallRowLen; i++) { width = 0; for (int j = 0; j < wallColLen[i]; j++) { for (int k = 1; k < wall[i][j]; k++) { hash[width+k] += 1; } width += wall[i][j]; } } int minCount = wallRowLen; for (int i = 1; i < width; i++) { if (hash[i] < minCount) { minCount = hash[i]; } } return minCount; }
C++ 解法, 执行用时: 57ms, 内存消耗: 7664KB, 提交时间: 2022-07-18
class Solution { public: int brickwall(vector<vector<int> >& wall) { int a = 0; for (auto f : wall[0]) { a += f; } vector<int> v(a + 1, 0); for (int i = 0; i < wall.size(); i ++) { int b = 0; for (int j = 0; j < wall[i].size(); j ++) { b += wall[i][j]; v[b]++; } } int h = wall.size(); int z = INT_MAX; for (int i = 1; i <= a - 1; i ++) { z = min(z, h - v[i]); } return z; } };
C++ 解法, 执行用时: 58ms, 内存消耗: 7668KB, 提交时间: 2022-08-06
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param wall int整型vector<vector<>> * @return int整型 */ int brickwall(vector<vector<int> >& wall) { // write code here int a=0; for(auto f:wall[0]) { a+=f; } vector<int> v(a+1,0); for(int i=0;i<wall.size();i++) { int b=0; for(int j=0;j<wall[i].size();j++) { b+=wall[i][j]; v[b]++; } } int h=wall.size(); int z=INT_MAX; for(int i=1;i<=a-1;i++) { z=min(z,h-v[i]); } return z; } };
C++ 解法, 执行用时: 58ms, 内存消耗: 7668KB, 提交时间: 2022-07-03
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param wall int整型vector<vector<>> * @return int整型 */ int brickwall(vector<vector<int> >& wall) { // write code here int n = 0; for(auto subvecn : wall[0]){ n += subvecn; } vector<int> wallline(n + 1, 0); for(int i = 0; i < wall.size(); i ++){ int len = 0; for(int j = 0; j < wall[i].size(); j ++){ len += wall[i][j]; wallline[len]++; } } int layer = wall.size(); int res = INT_MAX; for(int i = 1; i <= n - 1; i ++){ res = min(res, layer - wallline[i]); } return res; } };