일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- 프로그래머스
- 카페
- Android
- leetcode
- 림프절전이
- Jetpack Compose
- 안드로이드
- java
- 정렬 알고리즘
- MYSQL
- 백준알고리즘
- 폐CT
- 전이
- 갑상선암용인세브란스
- Android Compose
- 저요오드식
- firebase
- 갑상선암
- Compose
- 맛집
- 동위원소치료
- 방사성동위원소치료
- android auto
- 알고리즘
- 임파선전이
- 방사성 동위원소 치료
- 입원
- 객체
- 경력 개발자
- 수술
Archives
- Today
- Total
새우버거의 개발 블로그
[Java] LeetCode 874 : Walking Robot Simulation 본문
LeetCode # 874
Example 1:
Input:
commands = [4,-1,3], obstacles = []
Output:
25
Explanation: The robot starts at (0, 0): 1. Move north 4 units to (0, 4). 2. Turn right. 3. Move east 3 units to (3, 4). The furthest point away from the origin is (3, 4), which is 32 + 42 = 25 units away.
Solution #1 (11ms)
import java.awt.*;
import java.util.HashSet;
import java.util.Set;
class Solution {
int[] xStep = new int[]{0, 1, 0, -1};
int[] yStep = new int[]{1, 0, -1, 0};
int direction = 0, x = 0, y = 0;
int result = 0;
public int robotSim(int[] commands, int[][] obstacles) {
Set<Point> set = new HashSet<>();
for (int[] obs : obstacles) {
set.add(new Point(obs[0], obs[1]));
}
for (int command : commands) {
if (command == -1 || command == -2) {
direction = getDirection(command);
} else {
for (int j = 0; j < command; j++) {
int xTmp = x + xStep[direction];
int yTmp = y + yStep[direction];
Point point = new Point(xTmp, yTmp);
if (set.contains(point)) {
break;
}
x = xTmp;
y = yTmp;
}
}
result = Math.max(result, (x * x) + (y * y));
}
return result;
}
private int getDirection(int d) {
if (d == -2) direction -= 1;
else if (d == -1) direction += 1;
return (direction < 0 ? (direction + 4) : direction) % 4;
}
}
Result #1
More Algorithm!
👇👇
github.com/ggujangi/ggu.leet-code
ggujangi/ggu.leet-code
LeetCode, Java. Contribute to ggujangi/ggu.leet-code development by creating an account on GitHub.
github.com
출처 : leetCode
'알고리즘 > LeetCode' 카테고리의 다른 글
[Java] LeetCode 717 : 1-bit and 2-bit Characters (0) | 2021.04.21 |
---|---|
[Java] LeetCode 953 : Verifying an Alien Dictionary (0) | 2021.04.21 |
[Java] LeetCode 1518 : Water Bottles (0) | 2021.04.17 |
[Java] LeetCode 290 : Word Pattern (0) | 2021.04.17 |
[Java] LeetCode 914 : X of a Kind in a Deck of Cards (0) | 2021.04.17 |