Minimum number of stops from given path
There are many points in two-dimensional space which need to be visited in a specific sequence. Path from one point to other is always chosen as shortest path and path segments are always aligned with grid lines. Now we are given the path which is chosen for visiting the points, we need to tell the minimum number of points that must be needed to generate given path. Examples:
In above diagram, we can see that there
must be at least 3 points to get above
path, which are denoted by A, B and C
We can solve this problem by observing the pattern of movement when visiting the stops. If we want to take the shortest path from one point to another point then we will move in either one or max two directions i.e. it is always possible to reach the other point following maximum two directions and if more than two directions are used then that path won’t be shortest, for example, path LLURD can be replaced with LLL only, so to find minimum number of stops in the path, we will loop over the characters of the path and maintain a map of directions taken till now.
If at any index we found both ‘L’ as well as ‘R’ or we found both ‘U’ as well as ‘D’ then there must be a stop at current index, so we will increase the stop count by one and we will clear the map for next segment. Total time complexity of the solution will be O(N)
Implementation:
CPP
#include <bits/stdc++.h>
using namespace std;
int numberOfPointInPath(string path)
{
int N = path.length();
map< char , int > dirMap;
int points = 1;
for ( int i = 0; i < N; i++) {
char curDir = path[i];
dirMap[curDir] = 1;
if ((dirMap[ 'L' ] && dirMap[ 'R' ]) ||
(dirMap[ 'U' ] && dirMap[ 'D' ])) {
dirMap.clear();
points++;
dirMap[curDir] = 1;
}
}
return (points + 1);
}
int main()
{
string path = "LLUUULLDD" ;
cout << numberOfPointInPath(path) << endl;
return 0;
}
|
Java
import java.util.*;
public class GFG
{
public static int numberOfPointInPath(String path)
{
int N = path.length();
TreeMap<Character, Integer> dirMap
= new TreeMap<Character, Integer>();
int points = 1 ;
dirMap.put( 'L' , 0 );
dirMap.put( 'R' , 0 );
dirMap.put( 'D' , 0 );
dirMap.put( 'U' , 0 );
for ( int i = 0 ; i < N; i++) {
char curDir = path.charAt(i);
dirMap.put(curDir, 1 );
if ((dirMap.get( 'L' ) == 1
&& dirMap.get( 'R' ) == 1 )
|| (dirMap.get( 'U' ) == 1
&& dirMap.get( 'D' ) == 1 )) {
dirMap.put( 'L' , 0 );
dirMap.put( 'R' , 0 );
dirMap.put( 'D' , 0 );
dirMap.put( 'U' , 0 );
points++;
dirMap.put(curDir, 1 );
}
}
return (points + 1 );
}
public static void main(String[] args)
{
String path = "LLUUULLDD" ;
System.out.print(numberOfPointInPath(path));
System.out.print( "\n" );
}
}
|
Python3
def numberOfPointInPath(path):
N = len (path)
dirMap = {}
points = 1
for i in range (N):
curDir = path[i]
dirMap[curDir] = 1
if ( 'L' in dirMap and 'R' in dirMap) or ( 'U' in dirMap and 'D' in dirMap):
dirMap.clear()
points = points + 1
dirMap[curDir] = 1
return points + 1
path = "LLUUULLDD"
print (numberOfPointInPath(path))
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
public static int NumberOfPointInPath( string path)
{
int N = path.Length;
Dictionary< char , int > dirMap = new Dictionary< char , int >();
dirMap[ 'L' ] = 0;
dirMap[ 'R' ] = 0;
dirMap[ 'D' ] = 0;
dirMap[ 'U' ] = 0;
int points = 1;
for ( int i = 0; i < N; i++)
{
char curDir = path[i];
dirMap[curDir] = 1;
if ((dirMap[ 'L' ] == 1 && dirMap[ 'R' ] == 1)
|| (dirMap[ 'U' ] == 1 && dirMap[ 'D' ] == 1))
{
dirMap[ 'L' ] = 0;
dirMap[ 'R' ] = 0;
dirMap[ 'D' ] = 0;
dirMap[ 'U' ] = 0;
points++;
dirMap[curDir] = 1;
}
}
return (points + 1);
}
public static void Main()
{
string path = "LLUUULLDD" ;
Console.Write(NumberOfPointInPath(path));
Console.Write( "\n" );
}
}
|
Javascript
<script>
function numberOfPointInPath(path) {
let N = path.length;
let dirMap = new Map();
let points = 1;
for (let i = 0; i < N; i++) {
let curDir = path[i];
dirMap.set(curDir, 1);
if ((dirMap.has( 'L' ) && dirMap.has( 'R' )) || (dirMap.has( 'U' ) && dirMap.has( 'D' ))) {
dirMap.clear();
points++;
dirMap.set(curDir, 1);
}
}
return points + 1;
}
let path = "LLUUULLDD" ;
console.log(numberOfPointInPath(path));
</script>
|
Time Complexity: O(n*logn).
Auxiliary Space: O(n)
Last Updated :
02 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...