Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Race Car in C++
Suppose we have a car, that starts at position 0 and speed +1 on an infinite number line. The car runs automatically according to a sequence of instructions A: for accelerate and R − for reverse. When we get an instruction "A", our car does the following −
- position := position + speed, then speed = speed * 2.
When we get an instruction "R", our car does the following −
- if speed is positive then speed = -1,
- otherwise speed = 1.
For example, after executing the instructions "AAR", our car goes to positions 0->1->3->3, and speed goes to 1->2->4->-1.
Now suppose we have some target position; we have to find the length of the shortest sequence of instructions to get there.
So, if the input is like target = 6, then the output will be 5 as one of the possible sequence is AAARA, the position sequence will be 0->1->3->7->7->6
To solve this, we will follow these steps −
- Define one set visited
- Define one queue q
- insert { 0, 1 } into q
- for initialize level := 0, when not q is empty, update (increase level by 1), do −
- for initialize k := size of q, when k > 0, update (decrease k by 1), do −
- Define one pair curr := front element of q, delete element from q
- if first of curr is same as target, then −
- return level
- forward := first of curr + second of curr
- forwardSpeed := second of curr * 2
- key := convert forward to string concatenate " * " concatenate convert forwardSpeed to string
- if forward > 0 and |forward - target| < target and not key is not in visited, then −
- insert key into visited
- insert { forward, forwardSpeed } into q
- key := convert first of curr to string concatenate " * " concatenate 0 when second of curr > 0, otherwise -1
- if curr.first > 0 and |target - curr.first| < target and key is not in visited, then −
- insert key into visited
- insert { curr.first, (if curr.second > 0, then -1, otherwise 1 }) into q
- for initialize k := size of q, when k > 0, update (decrease k by 1), do −
- return -1
Let us see the following implementation to get better understanding −
Example
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int racecar(int target) {
unordered_set < string > visited;
queue < pair <int ,int> > q;
q. push({0, 1});
for(int level = 0; !q.empty(); level++){
for(int k = q.size(); k > 0; k-- ){
pair <int, int> curr = q.front();
q.pop();
if(curr.first == target) return level;
int forward = curr.first + curr.second;
int forwardSpeed = curr.second * 2;
string key = to_string(forward) + "*" + to_string(forwardSpeed);
if(forward > 0 && abs(forward - target) < target && !visited.count(key)){
visited.insert(key);
q.push({forward, forwardSpeed});
}
key = to_string(curr.first) + "*" + to_string(curr.second > 0 ? - 1: 1);
if(curr.first > 0 && abs(target - curr.first) < target && !visited.count(key)){
visited.insert(key);
q.push({curr.first, curr.second > 0 ? - 1: 1});
}
}
}
return -1;
}
};
main(){
Solution ob;
cout << (ob.racecar(6));
}
Input
6
Output
5