Skip to content

Commit 8fba0cc

Browse files
committed
Runtime: 27 ms (Top 58.02%) | Memory: 9.5 MB (Top 46.80%)
1 parent dada6e9 commit 8fba0cc

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed
Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
// Runtime: 27 ms (Top 58.02%) | Memory: 9.5 MB (Top 46.80%)
12
struct Position {
23
long long int pos;
34
long long int speed;
45
long long int moves;
5-
6+
67
Position(int pos, int speed, int moves) {
78
this -> pos = pos;
89
this -> speed = speed;
@@ -16,32 +17,32 @@ class Solution {
1617
queue<Position> q;
1718
Position p(0, 1, 0);
1819
q.push(p);
19-
20+
2021
set<pair<long long int, long long int>> s;
21-
22+
2223
while(!q.empty()) {
2324
Position u = q.front();
2425
q.pop();
25-
26+
2627
if(u.pos == target) return u.moves;
27-
28+
2829
if(s.find({u.pos, u.speed}) != s.end()) continue;
2930
else {
3031
s.insert({u.pos, u.speed});
31-
32-
// only cases when you might need to move backwards
33-
if((u.pos + u.speed > target && u.speed > 0) ||
32+
33+
// only cases when you might need to move backwards
34+
if((u.pos + u.speed > target && u.speed > 0) ||
3435
(u.pos + u.speed < target && u.speed < 0)) {
3536
long long int speed = u.speed > 0 ? -1 : 1;
3637
Position bkwd(u.pos, speed, u.moves + 1);
3738
q.push(bkwd);
3839
}
39-
40+
4041
Position fwd(u.pos + u.speed, 2 * u.speed, u.moves + 1);
4142
q.push(fwd);
4243
}
4344
}
44-
45+
4546
return -1;
4647
}
47-
};
48+
};

0 commit comments

Comments
 (0)