Find the city by using teleporters
Last Updated :
04 Dec, 2023
Given N cities numbered 1 to N. You are initially in the 1st city. The distance between two cities i, j is |Ai – Aj|. The energy lost while traveling from one city to another is equal to the distance between those two cities. Additionally, In every city i ( 1 ≤ i ≤ N-1) there is a teleporter that can teleport you from the ith city to (i+1)th city without using any energy. Your initial energy is X and you can use the teleporters at most K times. Find the farthest city you can reach if you use these teleporters optimally.
Note: Ai < Ai+1 for every i such that 1 ≤ i ≤ N-1.
Examples:
Input: N = 4, X = 2, K = 2, A[] = {0, 1, 2, 3}
Output: 4
Explanation: You can use teleporters of cities 1 and 2 and then travel from the 3rd city to the 4th city.
Approach: This can be solved with the following idea:
Using the Priority queue, we can see which cities have greater distance between them. Replacing the highest distance by teleporting and reducing K. For more clarification read the steps.
Below are the steps involved:
- Declare a priority_queue p.
- Iterate in array A:
- See the distance between adjacent cities.
- If dis <= X, we can use energy to move to next city.
- If dis > X, we can now use teleport for maximum distance.
- Reduce k by 1, and top of queue to X.
- Check whether can we now move to next city or not.
- Where k value reduced to 0, return i + 1.
Below is the implementation of the code:
C++
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int solve( int N, vector< int >& A, int x, int k)
{
priority_queue< int > p;
int i;
for (i = 0; i < N - 1; i++) {
int y = A[i + 1] - A[i];
if (x >= y) {
x -= y;
p.push(y);
}
else {
while (!p.empty() && p.top() > y && x < y
&& k--) {
x += p.top();
p.pop();
}
if (x < y) {
if (k >= 1)
k--;
else
break ;
}
else {
x -= y;
p.push(y);
}
}
}
return i + 1;
}
int main()
{
int N = 4;
int x = 2;
int k = 2;
vector< int > A = { 0, 1, 2, 3 };
cout << solve(N, A, x, k);
return 0;
}
|
Java
import java.util.PriorityQueue;
import java.util.Vector;
public class CityTeleport {
static int solve( int N, Vector<Integer> A, int x, int k) {
PriorityQueue<Integer> p = new PriorityQueue<>();
int i;
for (i = 0 ; i < N - 1 ; i++) {
int y = A.get(i + 1 ) - A.get(i);
if (x >= y) {
x -= y;
p.add(y);
} else {
while (!p.isEmpty() && p.peek() > y && x < y && k > 0 ) {
x += p.poll();
k--;
}
if (x < y) {
if (k >= 1 )
k--;
else
break ;
} else {
x -= y;
p.add(y);
}
}
}
return i + 1 ;
}
public static void main(String[] args) {
int N = 4 ;
int x = 2 ;
int k = 2 ;
Vector<Integer> A = new Vector<>();
A.add( 0 );
A.add( 1 );
A.add( 2 );
A.add( 3 );
System.out.println(solve(N, A, x, k));
}
}
|
Python3
import heapq
def solve(N, A, x, k):
p = []
i = 0
while i < N - 1 :
y = A[i + 1 ] - A[i]
if x > = y:
x - = y
heapq.heappush(p, y)
else :
while p and p[ 0 ] > y and x < y and k > 0 :
x + = heapq.heappop(p)
k - = 1
if x < y:
if k > = 1 :
k - = 1
else :
break
else :
x - = y
heapq.heappush(p, y)
i + = 1
return i + 1
if __name__ = = "__main__" :
N = 4
x = 2
k = 2
A = [ 0 , 1 , 2 , 3 ]
print (solve(N, A, x, k))
|
C#
using System;
using System.Collections.Generic;
class Program
{
static int Solve( int N, int [] A, int x, int k)
{
var p = new SortedSet< int >();
int i = 0;
while (i < N - 1)
{
int y = A[i + 1] - A[i];
if (x >= y)
{
x -= y;
p.Add(y);
}
else
{
while (p.Count > 0 && p.Min > y && x < y && k > 0)
{
x += p.Min;
p.Remove(p.Min);
k--;
}
if (x < y)
{
if (k >= 1)
k--;
else
break ;
}
else
{
x -= y;
p.Add(y);
}
}
i++;
}
return i + 1;
}
static void Main( string [] args)
{
int N = 4;
int x = 2;
int k = 2;
int [] A = { 0, 1, 2, 3 };
Console.WriteLine(Solve(N, A, x, k));
}
}
|
Javascript
function solve(N, A, x, k) {
const p = [];
for (let i = 0; i < N - 1; i++) {
const y = A[i + 1] - A[i];
if (x >= y) {
x -= y;
p.push(y);
}
else {
while (p.length > 0 && p[0] > y && x < y && k > 0) {
x += p[0];
p.shift();
k--;
}
if (x < y) {
if (k >= 1) k--;
else break ;
}
else {
x -= y;
p.push(y);
}
}
}
return N;
}
const N = 4;
const x = 2;
let k = 2;
const A = [0, 1, 2, 3];
console.log(solve(N, A, x, k));
|
Time Complexity: O(N log N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...