forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKth Smallest Instructions.cpp
44 lines (35 loc) · 1.22 KB
/
Kth Smallest Instructions.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
class Solution {
public:
string kthSmallestPath(vector<int>& destination, int k) {
int n=destination[0]+1,m=destination[1]+1;
//this code is straight forward it calculate and stores no. ways to go to destination from i,j
vector<vector<int>> ways(n,vector<int> (m));
for(int i=n-1;i>=0;i--){
for(int j=m-1;j>=0;j--){
if(i==n-1||j==m-1){
ways[i][j]=1; continue;
}
ways[i][j]=ways[i][j+1]+ways[i+1][j];
}
}
// with the help of above table ans can be generated
//suppose we are at (i, j) we need to decide whether to go Horizontal or Vertical
string ans="";
int i=0,j=0;
while(i<n-1&&j<m-1){
// if k is less than no of ways to go from selecting horizontal path
if(k<=ways[i][j+1]){
ans=ans+'H';j++;
}
// else we chose vertical for greater k
else{
k=k-ways[i][j+1];
i++;
ans=ans+'V';
}
}
while(j++<m-1) ans=ans+'H';
while(i++<n-1) ans=ans+'V';
return ans;
}
};