forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRace Car.cpp
48 lines (39 loc) · 1.24 KB
/
Race Car.cpp
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Runtime: 27 ms (Top 58.02%) | Memory: 9.5 MB (Top 46.80%)
struct Position {
long long int pos;
long long int speed;
long long int moves;
Position(int pos, int speed, int moves) {
this -> pos = pos;
this -> speed = speed;
this -> moves = moves;
}
};
class Solution {
public:
int racecar(int target) {
queue<Position> q;
Position p(0, 1, 0);
q.push(p);
set<pair<long long int, long long int>> s;
while(!q.empty()) {
Position u = q.front();
q.pop();
if(u.pos == target) return u.moves;
if(s.find({u.pos, u.speed}) != s.end()) continue;
else {
s.insert({u.pos, u.speed});
// only cases when you might need to move backwards
if((u.pos + u.speed > target && u.speed > 0) ||
(u.pos + u.speed < target && u.speed < 0)) {
long long int speed = u.speed > 0 ? -1 : 1;
Position bkwd(u.pos, speed, u.moves + 1);
q.push(bkwd);
}
Position fwd(u.pos + u.speed, 2 * u.speed, u.moves + 1);
q.push(fwd);
}
}
return -1;
}
};