Grid Illumination
2019-07-07 16:53:31
问题描述:
问题求解:
本题和n后问题很类似,所以最初的时候就直接套了n后的板子,MLE。
public int[] gridIllumination(int N, int[][] lamps, int[][] queries) {
int[] rows = new int[N];
int[] cols = new int[N];
int[] diag1 = new int[2 * N - 1];
int[] diag2 = new int[2 * N - 1];
Set<String> set = new HashSet<>();
for (int[] lamp : lamps) {
int x = lamp[0];
int y = lamp[1];
rows[x]++;
cols[y]++;
diag1[x + y]++;
diag2[N - 1 - x + y]++;
set.add(String.valueOf(x) + "_" + String.valueOf(y));
}
int n = queries.length;
int[] res = new int[n];
for (int i = 0; i < n; i++) {
int[] q = queries[i];
int x = q[0];
int y = q[1];
if (rows[x] > 0 || cols[y] > 0 || diag1[x + y] > 0 || diag2[N - 1 - x + y] > 0)
res[i] = 1;
else res[i] = 0;
helper(x, y, set, rows, cols, diag1, diag2, N);
}
return res;
} private void helper(int x, int y, Set<String> set, int[] rows, int[] cols, int[] diag1, int[] diag2, int N) {
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
int pi = x + i;
int pj = y + j;
if (pi < 0 || pi >= N || pj < 0 || pj >= N) continue;
String cur = String.valueOf(pi) + "_" + String.valueOf(pj);
if (set.contains(cur)) {
set.remove(cur);
rows[pi]--;
cols[pj]--;
diag1[pi + pj]--;
diag2[N - 1 - pi + pj]--;
}
}
}
}
那么本题的核心就是如何降低空间复杂度了,如何做呢?
降低空间复杂度有两种常用的技巧:
1. 将数组转为HashMap,这样就可以在大量稀疏的数组中提取到有用的信息,避免无用的信息。
2. 如何保存已经点亮的位置,这里采用字符串的处理显然是低效的,最好的方法是采用位操作的方式,使用long将两个int拼接起来达到区分的效果。
public int[] gridIllumination(int N, int[][] lamps, int[][] queries) {
HashMap<Integer, Integer> rows, cols, diag1, diag2;
rows = new HashMap<Integer, Integer>();
cols = new HashMap<Integer, Integer>();
diag1 = new HashMap<Integer,Integer>();
diag2 = new HashMap<Integer, Integer>();
Set<Long> set = new HashSet<>();
for (int[] lamp : lamps) {
int x = lamp[0];
int y = lamp[1];
rows.put(x, rows.getOrDefault(x, 0) + 1);
cols.put(y, cols.getOrDefault(y, 0) + 1);
diag1.put(x + y, diag1.getOrDefault(x + y, 0) + 1);
diag2.put(N - 1 - x + y, diag2.getOrDefault(N - 1 - x + y, 0) + 1);
set.add((long)x << 32 | (long)y);
}
int n = queries.length;
int[] res = new int[n];
for (int i = 0; i < n; i++) {
int[] q = queries[i];
int x = q[0];
int y = q[1];
if (rows.getOrDefault(x, 0) > 0 || cols.getOrDefault(y, 0) > 0 || diag1.getOrDefault(x + y, 0) > 0 || diag2.getOrDefault(N - 1 - x + y, 0) > 0)
res[i] = 1;
else res[i] = 0;
helper(x, y, set, rows, cols, diag1, diag2, N);
}
return res;
} private void helper(int x, int y, Set<Long> set, HashMap<Integer, Integer> rows, HashMap<Integer, Integer> cols, HashMap<Integer, Integer> diag1, HashMap<Integer, Integer> diag2, int N) {
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
int pi = x + i;
int pj = y + j;
if (pi < 0 || pi >= N || pj < 0 || pj >= N) continue;
long cur = (long)pi << 32 | pj;
if (set.contains(cur)) {
set.remove(cur);
rows.put(pi, rows.get(pi) - 1);
cols.put(pj, cols.get(pj) - 1);
diag1.put(pi + pj, diag1.get(pi + pj) - 1);
diag2.put(N - 1 - pi + pj, diag2.get(N - 1 - pi + pj) - 1);
}
}
}
}
Grid Illumination的更多相关文章
- [Swift]LeetCode1001. 网格照明 | Grid Illumination
On a N x N grid of cells, each cell (x, y) with 0 <= x < N and 0 <= y < N has a lamp. In ...
- 【leetcode】1001. Grid Illumination
题目如下: On a N x N grid of cells, each cell (x, y) with 0 <= x < N and 0 <= y < N has a la ...
- 【LeetCode】1001. Grid Illumination 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 哈希 日期 题目地址:https://leetcod ...
- 【LeetCode】Grid Illumination(网格照明)
这道题是LeetCode里的第1001道题. 题目要求: 在 N x N 的网格上,每个单元格 (x, y) 上都有一盏灯,其中 0 <= x < N 且 0 <= y < N ...
- [LeetCode] N-Queens N皇后问题
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- [LeetCode] 51. N-Queens N皇后问题
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens ...
- leetcode hard
# Title Solution Acceptance Difficulty Frequency 4 Median of Two Sorted Arrays 27.2% Hard ...
- 【Leetcode周赛】从contest-121开始。(一般是10个contest写一篇文章)
Contest 121 (题号981-984)(2019年1月27日) 链接:https://leetcode.com/contest/weekly-contest-121 总结:2019年2月22日 ...
随机推荐
- Oops 的栈信息分析
MTK MT55 F3600 平台 现象:播放MP4文件不断快退或者快进系统重启. 关键log: Kernel panic - not syncing: x_msg_q_receive(): not ...
- IP不是万能药 为何有蜘蛛侠等大片的索尼要放弃电影
为何有蜘蛛侠等大片的索尼要放弃电影"> 近年来,国内狂炒"IP"这一概念,比如动漫.网络文学.小说.游戏等,甚至围绕IP制造出内容矩阵.从一个IP延伸至多个领域 ...
- Flutter跨平台框架的使用-iOS最新版
科技引领我们前行 [前言] 1:先简单的介绍下Flutter,它是一款跨平台应用SDK,高性能跨平台实现方案(暂时讨论iOS和Android), 它不同于RN,少了像RN的JS中间桥接层,所以它的性能 ...
- Mongodb 对于Sort排序能够支持的最大内存限制查看和修改
MongoDB Server对于Sort排序能够支持的最大内存限制查看: > use admin switched to db admin >db.runCommand({ getPara ...
- ORACLE数据库实现主键自增
ORACLE数据库是甲骨文公司的一款关系数据库管理系统. 实现主键自动增长需要四个步骤: 去看 创建表格 去看 创建自增序列 去看 创建触发器 去看 插入测试 1.创建表格(必须有主键) -- 创建学 ...
- czC#02
1.out参数 out参数要求在方法的内部必须为其赋值 using System; using System.Text; namespace Demo { class Program { //返回一个 ...
- 图解MySQL索引(上)—MySQL有中“8种”索引?
关于MySQL索引相关的内容,一直是一个让人头疼的问题,尤其是对于初学者来说.笔者曾在很长一段时间内深陷其中,无法分清"覆盖索引,辅助索引,唯一索引,Hash索引,B-Tree索引--&qu ...
- Web最佳实践阅读总结(2)
代码符合标准 标准的页面会保证正确的渲染 页面容易被搜索引擎搜索,提高搜索排名(SEO) 提高网站的易用性 网页更好维护和扩展(Validator,HTML Validator 属于Firefox插件 ...
- 学习经典算法—JavaScript篇(一)排序算法
前端攻城狮--学习常用的排序算法 一.冒泡排序 优点: 所有排序中最简单的,易于理解: 缺点: 时间复杂度O(n^2),平均来说是最差的一种排序方式: 因为在默认情况下,对于已经排好序的部分,此排序任 ...
- go结构体继承组合和匿名字段
1.结构体方法 go不是纯粹的面向对象的,在go里面函数是一等公民,但是go也有结构体实现类似java一样类的功能来提供抽象.结构体的方法分为值方法和指针方法,前者在方法中做的改变不会改变调用的实例对 ...