leetcode:范围求和 II(用点脑子)
作者:互联网
无脑模拟:
超出内存
func maxCount(m int, n int, ops [][]int) int {
//m[0][0]一定最大
//模拟
//初始化
if len(ops) == 0 {
return m * n
}
matrix := make([][]int, m)
for i := 0; i < m; i++ {
matrix[i] = make([]int, n)
}
//不断+1
for i := 0; i < len(ops); i++ {
a, b := ops[i][0], ops[i][1]
for r := 0; r < a; r++ {
for c := 0; c < b; c++ {
matrix[r][c] += 1
}
}
}
//寻找最大的元素
maxn := matrix[0][0]
count := 0
for r := 0; r < m; r++ {
for c := 0; c < n; c++ {
if matrix[r][c] == maxn {
count += 1
}
}
}
return count
}
use your noodles:
显然,最大的元素一定是在最小的行和列中,因为它们一直都有在被加啊!!!
func maxCount(m int, n int, ops [][]int) int {
mina, minb := m, n
for _, op := range ops {
mina = min(mina, op[0])
minb = min(minb, op[1])
}
return mina * minb
}
func min(a, b int) int {
if a > b {
return b
} else {
return a
}
}
总结:
打码前先想想有没有偷懒的办法!!
标签:用点,return,matrix,ops,int,mina,minb,II,leetcode 来源: https://blog.csdn.net/weixin_40986490/article/details/121197342