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的更多相关文章

  1. [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 ...

  2. 【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 ...

  3. 【LeetCode】1001. Grid Illumination 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 哈希 日期 题目地址:https://leetcod ...

  4. 【LeetCode】Grid Illumination(网格照明)

    这道题是LeetCode里的第1001道题. 题目要求: 在 N x N 的网格上,每个单元格 (x, y) 上都有一盏灯,其中 0 <= x < N 且 0 <= y < N ...

  5. [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 ...

  6. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  7. [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 ...

  8. leetcode hard

    # Title Solution Acceptance Difficulty Frequency     4 Median of Two Sorted Arrays       27.2% Hard ...

  9. 【Leetcode周赛】从contest-121开始。(一般是10个contest写一篇文章)

    Contest 121 (题号981-984)(2019年1月27日) 链接:https://leetcode.com/contest/weekly-contest-121 总结:2019年2月22日 ...

随机推荐

  1. elasticsearch 产生未分配分片的原因(es官网)

    Reasons for unassigned shard: These are the possible reasons for a shard to be in a unassigned state ...

  2. clearstatcache清除文件状态缓存

    当使用下列任何函数时stat(),lstat(),file_exists(),is_writable(),is_readable(),is_executable(),is_file(),is_dir( ...

  3. IT从业者疫情之下出路何在

    作为一个IT行业十年经历的从业人员,在北京大公司工作过,但因衡量着北京大都市的繁华下高消费和高房价,选择到二线城市发展和组建家庭,由此逃离北上广,结束了数年的北漂生涯.很荣幸到了二线城市顺利遇见属于自 ...

  4. python 临时文件

    1. TemporaryFile 临时文件 TemporaryFile 不在硬盘上的生成真正文件,而是写在内存中 from tempfile import TemporaryFile # , Name ...

  5. JMeter-WebService接口的测试

    前言 JMeter3.2版本之后就没有SOAP/XML-RPC Request插件了,那么该如何进行webservice接口的测试呢? 今天我们来一起学习一下怎么在3.2以后版本的JMeter进行we ...

  6. 记一次苹果APP从账号续费到发布成功的历程

    一.一波三折的续费      最近公司开发的苹果APP的SSL证书到期了,计划重新发布一下该APP,已替换即将到期的SSL证书.近几年随着钉钉.企业微信等在线办公软件超级平台的出现,各企业都会选择其中 ...

  7. @JsonFormat、@DateTimeFormat注解,读取数据库晚一天问题

    @DateTimeFormat(pattern = "yyyy-MM-dd") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss&qu ...

  8. 从输入URL到页面展示

    当我们输入 URL 并按回车后,浏览器会对 URL 进行检查,首先判断URL格式,比如是ftp http ed2k等等,我们这里假设这个URL是http://hellocassie.cn,那么浏览器会 ...

  9. 零基础HTML及CSS编码总结

    任务目的 针对设计稿样式进行合理的HTML架构,包括以下但不限于: * 掌握常用HTML标签的含义.用法 能够基于设计稿来合理规划HTML文档结构 理解语义化,合理地使用HTML标签来构建页面 掌握基 ...

  10. python自动化第一课 - python安装以及pycharm配置

    1.安装python 1.1打开python官网https://www.python.org/downloads/windows/进行下载Python 3.8.0 1.2下载完毕后进行安装,1勾选 A ...