add new solutions
This commit is contained in:
parent
f297e11859
commit
4720cbefc4
8 changed files with 290 additions and 0 deletions
28
solutions/30/q3070/solution.go
Normal file
28
solutions/30/q3070/solution.go
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
// Package q3070 implements a solution for https://leetcode.com/problems/count-submatrices-with-top-left-element-and-sum-less-than-k/
|
||||
package q3070
|
||||
|
||||
func countSubmatrices(grid [][]int, k int) int {
|
||||
cnt := 0
|
||||
for r := range grid {
|
||||
for c := 1; c < len(grid[0]); c++ {
|
||||
grid[r][c] += grid[r][c-1]
|
||||
}
|
||||
}
|
||||
|
||||
for r := range grid {
|
||||
for c := range grid[0] {
|
||||
if r > 0 {
|
||||
grid[r][c] += grid[r-1][c]
|
||||
}
|
||||
|
||||
if grid[r][c] <= k {
|
||||
cnt++
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return cnt
|
||||
}
|
||||
|
||||
var _ = countSubmatrices
|
||||
Loading…
Add table
Add a link
Reference in a new issue