golang力扣leetcode 1001.网格照明

简介: golang力扣leetcode 1001.网格照明

1001.网格照明

1001.网格照明

题解

行 (横坐标 x )

列 (纵坐标 y )

撇 ( 记录 x-y )

捺 ( 记录 x+y )

其实就是模拟题,很简单,不知道为什么算hard

代码

package main
func gridIllumination(n int, lamps [][]int, queries [][]int) []int {
  type pair struct {
    x, y int
  }
  points := make(map[pair]bool)
  row := make(map[int]int)
  col := make(map[int]int)
  diagonal := make(map[int]int)
  antiDiagonal := make(map[int]int)
  for _, lamp := range lamps {
    r, c := lamp[0], lamp[1]
    p := pair{r, c}
    if points[p] {
      continue
    }
    points[p] = true
    row[r]++
    col[c]++
    diagonal[r-c]++
    antiDiagonal[r+c]++
  }
  ans := make([]int, len(queries))
  for i, quire := range queries {
    r, c := quire[0], quire[1]
    if row[r] > 0 || col[c] > 0 || diagonal[r-c] > 0 || antiDiagonal[r+c] > 0 {
      ans[i] = 1
    }
    for x := r - 1; x <= r+1; x++ {
      for y := c - 1; y <= c+1; y++ {
        if x < 0 || x >= n || y < 0 || y >= n || !points[pair{x, y}] {
          continue
        }
        delete(points, pair{x, y})
        row[x]--
        col[y]--
        diagonal[x-y]--
        antiDiagonal[x+y]--
      }
    }
  }
  return ans
}
目录
相关文章
|
17小时前
|
Java
【LeetCode力扣】面试题 17.14. 最小K个数(top-k问题)
【LeetCode力扣】面试题 17.14. 最小K个数(top-k问题)
10 1
|
17小时前
|
存储 算法
【LeetCode力扣】单调栈解决Next Greater Number(下一个更大值)问题
【LeetCode力扣】单调栈解决Next Greater Number(下一个更大值)问题
6 0
|
17小时前
|
Go 容器 SQL
【Golang Leetcode】总目录(Day1~100)
【Golang Leetcode】总目录(Day1~100)
477 1
【Golang Leetcode】总目录(Day1~100)
|
17小时前
|
算法 测试技术 C#
【动态规划】【广度优先搜索】LeetCode:2617 网格图中最少访问的格子数
【动态规划】【广度优先搜索】LeetCode:2617 网格图中最少访问的格子数
|
17小时前
leetcode-6110:网格图中递增路径的数目
leetcode-6110:网格图中递增路径的数目
23 1
|
17小时前
|
Go
golang力扣leetcode 494.目标和
golang力扣leetcode 494.目标和
32 0
|
17小时前
|
Go
golang力扣leetcode 剑指Offer II 114. 外星文字典
golang力扣leetcode 剑指Offer II 114. 外星文字典
21 0
|
17小时前
|
算法 C++
【刷题】Leetcode 1609.奇偶树
这道题是我目前做过最难的题,虽然没有一遍做出来,但是参考大佬的代码,慢慢啃的感觉的真的很好。刷题继续!!!!!!
8 0
|
17小时前
|
算法 索引
【刷题】滑动窗口精通 — Leetcode 30. 串联所有单词的子串 | Leetcode 76. 最小覆盖子串
经过这两道题目的书写,相信大家一定深刻认识到了滑动窗口的使用方法!!! 下面请大家继续刷题吧!!!
11 0