554.brick-wall.go 326 B

12345678910111213141516171819202122
  1. func leastBricks(wall [][]int) int {
  2. cnt := 0
  3. freq := make(map[int]int)
  4. for i := range wall {
  5. left := 0
  6. for _, w := range wall[i] {
  7. if left != 0 {
  8. freq[left]++
  9. cnt = maxInt(cnt, freq[left])
  10. }
  11. left += w
  12. }
  13. }
  14. return len(wall) - cnt
  15. }
  16. func maxInt(x, y int) int {
  17. if x < y {
  18. return y
  19. }
  20. return x
  21. }