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日 ...
随机推荐
- 干了这碗蛋炒饭 继续APP性能提升
[前言] 什么是做功能,功能就是客户要一碗蛋炒饭,然后做了给他. 我想谁都明白,一家餐厅能活下去,是因为能把食材料理好,客户喜欢. 更准确的说,一家餐厅能活得下去,要考虑用户需求.食材,然后就是料理水 ...
- idea激活教程(永久)支持2019 3.1 亲测
此教程已支持最新2019.3版本 本教程适用Windows.Mac.Ubuntu等所有平台. 激活前准备工作 配置文件修改已经不在bin目录下直接修改,而是通过Idea修改 如果输入code一直弹出来 ...
- Java入门教程十一(异常处理)
在程序设计和运行的过程中,发生错误是不可避免的.尽管 Java 语言的设计从根本上提供了便于写出整洁.安全代码的方法,并且程序员也尽量地减少错误的产生,但是使程序被迫停止的错误的存在仍然不可避免.为此 ...
- 压力测试(三)-自定义变量和CSV可变参数实操
1.Jmeter用户自定义变量实战 简介:什么是用户自定义变量,怎样使用 为什么使用:很多变量在全局中都有使用,或者测试数据更改,可以在一处定义,四处使用 比如服务器地址 1.线程组->add ...
- linux入门系列17--邮件系统之Postfix和Dovecot
前文演示了通过Samba和NFS实现文件共享,本篇演示使用Postfix和Dovecot在局域网实现电子邮件收发系统. 电子邮件系统是我们日常生活和工作中非常重要的一个网络服务,在windows下收发 ...
- Aajx
# Ajax入门及基本开发 ## # Ajax的基本概念 >> 概念: 界面异步传输技术: 将几种技术和在一起进行开发的一种编程方式: >> 基本应用场景: > Goog ...
- Asp.Net Core Filter 深入浅出的那些事-AOP
一.前言 在分享ASP.NET Core Filter 使用之前,先来谈谈AOP,什么是AOP 呢? AOP全称Aspect Oriented Programming意为面向切面编程,也叫做面向方法编 ...
- http协议概览
这里我只是对一些知识进行简单的整理,方便自己理解记忆,还有很多不完善的地方,更多细节,需要查看书籍或者其他文章 http协议的发展过程 HTTP 是基于 TCP/IP 协议的应用层协议.它不涉及数据包 ...
- Yuchuan_Linux_C编程之五gdb调试
一.整体大纲 二.gdb调试 1. 启动gdb start -- 只执行一步 n -- next s -- step(单步) -- 可以进入到函数体内部 c - continue - ...
- go 下载qq音乐
//go下载qq音乐 package main import ( _ "fmt" jsoniter "github.com/json-iterator/go" ...