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, ...
随机推荐
- LeetCode组合总和I~IV和背包问题小结
一.组合总和问题 最近在看leetcode的组合问题,一共四道,总结一下共通之处与不同之处. 原题链接: 组合总和 组合总和II 组合总和III 组合总和IV 对比如下,为了便于对比,将原题目的叙述方 ...
- ASP.NET Core分布式项目实战(业务介绍,架构设计,oAuth2,IdentityServer4)--学习笔记
任务4:第一章计划与目录 敏捷产品开发流程 原型预览与业务介绍 整体架构设计 API 接口设计 / swagger Identity Server 4 搭建登录 账号 API 实现 配置中心 任务5: ...
- Power BI 2 DAY
目录 Power BI零散知识点 M函数 Power BI零散知识点 纵向合并 = 主页-组合-追加查询-追加查询(修改数据源)-将查询追加为信查询(创建新数据源) 横向合并 = 主页-组合-合并查询 ...
- Codeforces Global Round 22 A-E
比赛链接 A 题解 知识点:贪心. 显然交错释放最好. 若两类数量不一样,那么较少的一组的一定都可以双倍,剩下的另一组就放进一个优先队列,从大到小和少的一组匹配可以双倍,剩下的直接加. 如果两类数量一 ...
- Shiro 框架的MD5加密算法实现原理
直接上代码:该代码可以直接用于项目中做MD5加密,加盐加密,多层散列加密 import java.io.UnsupportedEncodingException; import java.securi ...
- idea 报错: Unable to import maven project: See logs for details
错误再现: idea 工具日志: 1) No implementation for org.apache.maven.model.path.PathTranslator was bound. whil ...
- Android自动化测试框架uiautomator2详解
1 uiautomator2 简介 uiautomator2 是 一种 Android 自动化测试框架,提供了点击.长按.输入文本.滑动.拖拽.截屏等方法,能够模拟用户的各种动作.用户可以通过控件 ...
- Vue中data为何以函数形式返回
data为何以函数形式返回 在使用Vue构建组件化应用时,每个组件的data属性都是以函数形式返回的,这主要是在组件化实现的时候,每个实例可以维护一份被返回对象的独立的拷贝,而不是共享同一个对象的引用 ...
- 我们在SqlSugar开发框架中,用到的一些设计模式
我们在<SqlSugar开发框架>中,有时候都会根据一些需要引入一些设计模式,主要的目的是为了解决问题提供便利和代码重用等目的.而不是为用而用,我们的目的是解决问题,并在一定的场景下以水到 ...
- 腾讯云轻量级主机修改hostname后重启后又恢复原先的hostname
搭建k8s后,执行inspect后 显示hostname 不符合规范, 修改/etc/hostname 后重启, 发现又自动恢复到默认主机名: 查资料后发现腾讯云的配置里,如果重启会重置hostnam ...