Leetcode 招商银行-03. 点燃木棒
用广度优先,暴力搜索。代码如下
import java.util.*;
class Solution {
public static void main(String[] args) {
Solution solution = new Solution();
System.out.println(solution.lightSticks(1,2,new int[]{3}));
}
public int[] lightSticks(int height, int width, int[] indices) {
int pointsNum = (height + 1) * (width + 1);
boolean[][] relation = new boolean[pointsNum][4];
for(int i = 0;i<pointsNum;i++){
int x = i / (width+1);
int y = i % (width+1);
if(x>0) relation[i][0] = true;
if(y>0) relation[i][1] = true;
if(x<height) relation[i][2] = true;
if(y<width) relation[i][3] = true;
}
for(int removed:indices){
int mod = 2*width + 1;
int num = removed / mod;
int pos = removed % mod;
int x1,y1;
int x2,y2;
if( pos < width){
x1 = num ;
x2 = num;
y1 = pos;
y2 = pos+1;
int u = x1*(width+1)+y1;
int v = x2*(width+1)+y2;
relation[u][3] = false;
relation[v][1] = false;
}
else{
pos = pos - width;
x1 = num;
y1 = pos;
x2 = x1 + 1;
y2 = pos;
int u = x1*(width+1)+y1;
int v = x2*(width+1)+y2;
relation[u][2] = false;
relation[v][0] = false;
}
}
int[] costs = new int[pointsNum];
for (int i = 0; i < pointsNum; i++) {
int cost = bfs(i,relation,pointsNum,width,height);
costs[i] = cost;
}
int min = Integer.MAX_VALUE;
for(int cost:costs){
if(cost < min){
min = cost;
}
}
if( min == Integer.MAX_VALUE){
return new int[0];
}
int size = 0;
for(int cost:costs){
if(cost == min){
size++;
}
}
int[] ans = new int[size];
for(int i=0,index=0;i<pointsNum;i++){
int cost = costs[i];
if(cost == min){
ans[index++]=i;
}
}
return ans;
}
public int bfs(int start,boolean[][] originRelation,int num,int width,int height){
boolean[][] relation = new boolean[num][4];
for(int i = 0;i<num;i++){
for(int j = 0;j<4;j++){
relation[i][j] = originRelation[i][j];
}
}
Queue<Integer> queue = new LinkedList<Integer>();
queue.add(start);
int[] travedNodes = new int[num];
Arrays.fill(travedNodes,Integer.MAX_VALUE);
travedNodes[start] = 0;
while( !queue.isEmpty()) {
int u = queue.poll();
int x = u / (width+1);
int y = u % (width+1) ;
//如果可以向上燃:当前坐标不是第一行,并且存在边,并且没有遍历过
if(relation[u][0] ){
travedNodes[u-width-1] = travedNodes[u] + 1;
queue.add(u-width-1);
relation[u-width-1][2] = false;
relation[u][0] = false;
}
//如果可以向左
if(relation[u][1] ){
travedNodes[u-1] = travedNodes[u] + 1;
queue.add(u-1);
relation[u][1] = false;
relation[u-1][3] = false;
}
//如果可以向下
if(relation[u][2]){
travedNodes[u+width+1] = travedNodes[u] + 1;
queue.add(u+width+1);
relation[u][2] = false;
relation[u+width+1][0] = false;
}
//如果可以向右
if( relation[u][3]){
travedNodes[u+1] = travedNodes[u] + 1;
queue.add(u+1);
relation[u][3] = false;
relation[u+1][1] = false;
}
}
for(int i = 0;i<num;i++){
for(int j = 0;j<4;j++){
if(relation[i][j]){
return Integer.MAX_VALUE;
}
}
}
int max = 0;
for(int i=0;i<num;i++){
if( travedNodes[i] != Integer.MAX_VALUE && travedNodes[i] > max){
max = travedNodes[i];
}
}
return max;
}
}
Leetcode 招商银行-03. 点燃木棒的更多相关文章
- 【LeetCode字符串#03】图解翻转字符串中的单词,以及对于for使用的说明
翻转字符串中的单词 力扣题目链接(opens new window) 给定一个字符串,逐个翻转字符串中的每个单词. 示例 1: 输入: "the sky is blue" 输出: ...
- LeetCode Algorithm 05_Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- Leetcode:面试题 04.03. 特定深度节点链表
Leetcode:面试题 04.03. 特定深度节点链表 Leetcode:面试题 04.03. 特定深度节点链表 先贴一下自己写过一个模板,按层数遍历: https://www.cnblogs.co ...
- (python)leetcode刷题笔记03 Longest Substring Without Repeating Characters
3. Longest Substring Without Repeating Characters Given a string, find the length of the longest sub ...
- LeetCode 面试题 02.03. 删除中间节点
题目链接:https://leetcode-cn.com/problems/delete-middle-node-lcci/ 实现一种算法,删除单向链表中间的某个节点(除了第一个和最后一个节点,不一定 ...
- [LeetCode] Additive Number 加法数
Additive number is a positive integer whose digits can form additive sequence. A valid additive sequ ...
- [LeetCode] Number of Digit One 数字1的个数
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...
- [LeetCode] Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- Leetcode: LFU Cache && Summary of various Sets: HashSet, TreeSet, LinkedHashSet
Design and implement a data structure for Least Frequently Used (LFU) cache. It should support the f ...
- Leetcode 7 Reverse Integer 数论
题意:将整数倒置,该题简单但是需要注意数据的范围,难得的好题. 如果出现1000000003或者-2000000003,倒置后的数超过int的范围,因此返回0,出现这种情况可以使用long long, ...
随机推荐
- 素数打表,洛谷P1217 [USACO1.5]回文质数 Prime Palindromes
这道题的最后一个样例TLE(超时)了,判断素数的条件是 i*i<n 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include ...
- flash8.ocx或其附件之一不能正确注册
运行书中自带光盘中的程序,在该程序的readme说明中,提到这类错误,解决方式是: 因为是免安装程序,需要运行"setup"文件夹下的setup.exe文件,安装控件.在安装完成后 ...
- Delphi 安装ICS
下载完成后解压到你的指写目录! 1.在library里加入ICS->Delphi->Vc32目录. 2.从File->Open中打开ICS->Delphi->Vc32-& ...
- Linux 环境下使用 sqlplus 访问远程 Oracle 数据库
自己最近需要在 Oracle 生产环境检查一些数据,但是目前大多数的生产环境,出于安全考虑,不会提供图形界面让你使用类似 Navicat 工具让你直接访问数据库,网上找了很多资料,大部分都比较过时或者 ...
- spring前导知识-Tomcat、Maven等配置
spring前导知识: 版本注意: 该博客所用的版本: tomcat version 9 (注意10有未知错误(个人测试)) Maven version3.6.3 (注意3.6.2未知错误) serv ...
- Nginx+uwsgi+ssl配置https
Nginx+uwsgi+ssl配置https 使用原始django,太过于笨重和杂多nginx是一个轻量级的web服务器,在处理静态资源和高并发有优势uwsgi是一个基于python的高效率的协议,处 ...
- MQTT-QoS与协议流程
QoS的报文收发流程 QoS 0 最多交付一次,消息有可能丢失,最低的QoS等级,没有任何的机制,不需要等待确认和重传,只要保证消息发送,也可能到达不了接收端 QoS0消息发送流程: 发送端调用API ...
- 2024年,提升Windows开发和使用体验的实践经验 - RIME输入法
前言 上一篇文章介绍了 Windows 下的包管理器,本文继续介绍输入法. 事实上 Windows 的输入法生态比 Linux/Mac 丰富很多,不过很多国产输入法存在窃取隐私.植入广告.乱安装流氓软 ...
- Git将某个文件合并到指定分支
企业开发中,经常会单独拉分支去做自己的需求开发,但是某些时候一些公共的配置我们需要从主线pull,这时候整个分支merge显然不合适 1.切换至待合并文件的分支 git checkout <br ...
- 中国特色AI创业:在OpenAI阴影下的探索与挑战
在中国特色背景下,AI创业领域的一些荒诞现象以及对AI创业者.投资人的心态和影响.同时,OpenAI的强大影响力和国内AI领域的创业现状. 首先,小红书上关于中国版Sora项目的荒诞段子,揭示了部分人 ...