当前位置:网站首页>LeetCode brush diary: LCP 03. Machine's adventure
LeetCode brush diary: LCP 03. Machine's adventure
2022-08-02 01:55:00 【light [email protected]】
力扣团队买了一个可编程机器人,机器人初始位置在原点(0, 0).小伙伴事先给机器人输入一串指令command,机器人就会无限循环这条指令的步骤进行移动.指令有两种:
U: 向y轴正方向移动一格
R: 向x轴正方向移动一格.
不幸的是,在 xy 平面上还有一些障碍物,他们的坐标用obstacles表示.机器人一旦碰到障碍物就会被损毁.
给定终点坐标(x, y),返回机器人能否完好地到达终点.如果能,返回true;否则返回false.
示例 1:
输入:command = "URR", obstacles = [], x = 3, y = 2
输出:true
解释:U(0, 1) -> R(1, 1) -> R(2, 1) -> U(2, 2) -> R(3, 2).
示例 2:
输入:command = "URR", obstacles = [[2, 2]], x = 3, y = 2
输出:false
解释:机器人在到达终点前会碰到(2, 2)的障碍物.
示例 3:
输入:command = "URR", obstacles = [[4, 2]], x = 3, y = 2
输出:true
解释:到达终点后,再碰到障碍物也不影响返回结果.
限制:
2 <= command的长度 <= 1000
command由U,R构成,且至少有一个U,至少有一个R
0 <= x <= 1e9, 0 <= y <= 1e9
0 <= obstacles的长度 <= 1000
obstacles[i]不为原点或者终点
思路:模拟(超时)
class Solution {
public boolean robot(String command, int[][] obstacles, int x, int y) {
int len = command.length();
int a = 0, b = 0;
int index = 0;
while (len != 0){
if(index == len){
index = 0;
}
char str = command.charAt(index++);
if(str == 'U'){
b ++;
}else if(str == 'R'){
a ++;
}
if(a == x && b == y){
return true;
}else if(a > x || b > y){
return false;
}
for (int i = 0; i < obstacles.length; i++) {
if(obstacles[i][0] == a && obstacles[i][1] == b){
return false;
}
}
}
return false;
}
}
优化
class Solution {
public boolean robot(String command, int[][] obstacles, int x, int y) {
int len = command.length();
int a = 0, b = 0;
// First judge whether you can finish one round first.
for (int i = 0; i < len; i++) {
char str = command.charAt(i);
if(str == 'U'){
b ++;
}else{
a ++;
}
}
boolean res = reach(command, x, y, a, b);
if (!res){
return false;
}
for (int[] val: obstacles) {
if(val[0] > x || val[1] > y){
continue;
}
if(reach(command, val[0], val[1], a, b)){
return false;
}
}
return true;
}
public boolean reach(String command,int tx, int ty, int x, int y){
// See how many more passes it will take to get to the end.
int min = Math.min(tx/x, ty/y);
int nx = x*min, ny = y*min;
if(nx == tx && ny == ty){
return true;
}
for (int i = 0; i < command.length(); i++) {
char str = command.charAt(i);
if(str == 'U'){
ny ++;
}else{
nx ++;
}
if (nx > tx || ny > ty) return false;
if (nx == tx && ny == ty) return true;
}
return true;
}
}
版权声明
本文为[light [email protected]~no trace]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/214/202208020144011297.html
边栏推荐
- 6-24 exploit-vnc password cracking
- YGG Guild Development Plan Season 1 Summary
- Constructor instance method inheritance of typescript37-class (extends)
- 『网易实习』周记(三)
- 6-24漏洞利用-vnc密码破解
- 软件测试 接口自动化测试 pytest框架封装 requests库 封装统一请求和多个基础路径处理 接口关联封装 测试用例写在yaml文件中 数据热加载(动态参数) 断言
- Basic use of typescript34-class
- 乱七八糟的网站
- kubernetes之服务发现
- 当关注「互联网+」模式的时候,通常仅仅只是在关注「互联网+」模式本身
猜你喜欢
Some insights from 5 years of automated testing experience: UI automation must overcome these 10 pits
Navicat data shows incomplete resolution
3个月测试员自述:4个影响我职业生涯的重要技能
检查IP或端口是否被封
3.Bean的作用域与生命周期
【轮式里程计】
oracle查询扫描全表和走索引
成都openGauss用户组招募啦!
6-24 exploit-vnc password cracking
HSDC is related to Independent Spanning Tree
随机推荐
AOF重写
密码学的基础:X.690和对应的BER CER DER编码
The Paddle Open Source Community Quarterly Report is here, everything you want to know is here
Hiring a WordPress Developer: 4 Practical Ways
LeetCode Brushing Diary: 74. Searching 2D Matrix
volatile原理解析
typescript30-any类型
《自然语言处理实战入门》 基于知识图谱的问答机器人
Rust P2P网络应用实战-1 P2P网络核心概念及Ping程序
¶Backtop 回到顶部 不生效
6-24漏洞利用-vnc密码破解
手写一个博客平台~第三天
大话西游创建角色失败解决
Pcie the inbound and outbound
Chengdu openGauss user group recruit!
Constructor instance method inheritance of typescript37-class (extends)
kubernetes之服务发现
安全(1)
MySQL——增删查改操作
After graduating from three books, I was rejected by Tencent 14 times, and finally successfully joined Alibaba