일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- 아자아자 화이팅
- Hamming weight
- DP
- 자고 싶다
- SSAFY 10기 화이팅
- 수학
- BFS
- 우유가옆으로넘어지면아야
- LeetCode #릿코드 #좋은 하루 되세요 #Have a nice day
- I am Korean
- SSAFY 화이팅
- DFS
- SeongSeobDang
- 네트워크
- Have a nice day.
- 우유가 옆으로 넘어지면 아야
- 자료구조
- 텐션 업 10기 화이팅
- SSAFY IM/A
- 텐션 업 10기!
- 코로나 싫어요
- have a nice day
- HAVE A GOOD DAY
- Have a good day :)
- Java 환경 설정
- 우유아야
- SSAFY 테스트
- 모르고리즘
- amazon
- Today
- Total
Hope Everyone Is Happy
[Hard] 818. Race Car (Java) 본문
[Hard] 818. Race Car (Java)
J 크 2023. 12. 18. 20:41https://leetcode.com/problems/race-car/description/
Race Car - LeetCode
Can you solve this real interview question? Race Car - Your car starts at position 0 and speed +1 on an infinite number line. Your car can go into negative positions. Your car drives automatically according to a sequence of instructions 'A' (accelerate) an
leetcode.com
DP로 풀걸,,
※ Question Summary
▶ Your car starts at position 0 and speed +1 on an infinite number line (also can go into negative positions)
▶ There are only two modes to drive. 'A' (accelerate) and 'R' (reverse)
- 'A'
- position += speed
- speed *= 2
- 'R'
- If your speed is positive then speed = -1
- otherwise speed = 1
- Your postion stays the same
▶ Given a target position 'target', return the length of the shortest seqeunce of instructions to get there
For example, after commands "AAR",
your car goes to positions 0 --> 1 --> 3 --> 3, and
your speed goes to 1 --> 2 --> 4 --> -1.
◈ Constraints
- 1 <= target <= 10^4
◈ Input - 1
target = 3
◈ Output - 1
2
◈ Input - 2
Input: target = 6
◈ Output - 2
5
◎ Solution
▶ Create the class 'Car' which has the speed, position, count of sequence
▶ Override the equals, hashcode to enablle the use of hashSet
▶ We can use BFS since it can be respresented as a node
▶ Check if it has been visited using the hashset and whether the position exceeds twice the target value
class Car {
int m_nSpeed;
int m_nPosition;
int m_nCount;
Car(int nSpeed, int nPosition, int nCount) {
m_nSpeed = nSpeed;
m_nPosition = nPosition;
m_nCount = nCount;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Car car = (Car) o;
return m_nSpeed == car.m_nSpeed && m_nPosition == car.m_nPosition;
}
@Override
public int hashCode() {
return Objects.hash(m_nSpeed, m_nPosition);
}
}
class Solution {
public int racecar(int target) {
HashSet<Car> visit = new HashSet<>();
Queue<Car> BFS = new LinkedList<>();
BFS.add(new Car(1, 0, 0));
visit.add(new Car(1, 0, 0));
while(!BFS.isEmpty()) {
Car car = BFS.poll();
if(car.m_nPosition == target)
return car.m_nCount;
// accelerate
Car A = accelerate(car);
if(!visit.contains(A) && isValid(A, target)) {
visit.add(A);
BFS.add(new Car(A.m_nSpeed, A.m_nPosition, car.m_nCount+1));
}
// reverse
int nSpeed = reverse(car.m_nSpeed);
Car R = new Car(nSpeed, car.m_nPosition, car.m_nCount+1);
if(!visit.contains(R) && isValid(R, target)) {
visit.add(R);
BFS.add(R);
}
}
return 0;
}
public int reverse(int nSpeed) {
if(nSpeed >= 0)
return -1;
return 1;
}
public Car accelerate(Car car) {
int nPosition = car.m_nPosition + car.m_nSpeed;
int nSpeed = car.m_nSpeed * 2;
return new Car(nSpeed, nPosition, 0);
}
public boolean isValid(Car car, int target) {
if (Math.abs(car.m_nPosition) > 2 * target) {
return false;
}
return true;
}
}
I HOPE YOUR DAY GOES WELL :)
'※ 릿코드 ( LeetCode ) > [Java] 문제 풀이 ( Solve the problems)' 카테고리의 다른 글
[Hard] 1293. Shortest Path in a Grid with Obstacles Elimination (Java) (2) | 2023.12.17 |
---|---|
[Hard] 2534. Time Taken to Cross the Door(Java) (2) | 2023.12.15 |
[Hard] 489. Robot Room Cleaner (Java) (0) | 2023.12.11 |
[Medium] 419. Battleships in a Board (Java) (3) | 2023.12.05 |
[Medium] 366. Find Leaves of Binary Tree (Java) (0) | 2023.12.04 |