Kth path
Practice
4.5 (2 votes)
Hard
Algorithms
Approved
Problem
93% Success 752 Attempts 50 Points 1s Time Limit 256MB Memory 1024 KB Max Code

You have been given a N x M matrix containing lowercase English alphabets ('a' - 'z'). Consider path in which you can either move one unit right of current cell or move unit down of current cell i.e from \((x,\;y)\) you can either move to \((x+1,\;y)\) or move to \((x,\;y+1)\).
Consider all the paths going from \((1,\;1)\) to \((N,\;M)\) in this matrix. Each such path can be described by sequence of labels ('a' - 'z') of all the cells visited on the way. There will be many such paths going from \((1,\;1)\) to \((N,\;M)\). You have to find out lexicographic \(K^{th}\) path in the matrix.

Lexicographic \(K^{th}\) path can be described as:
Take all the possible paths in a vector and sort them. Print the path at \(K^{th}\) position in the array.

INPUT:
First line will consist of two integers N and M. Next N lines will consist of M characters. Next line will consist of number K.

OUTPUT:
Output a string consisting of \(N+M-1\) letters denoting the lexicographic \(K^{th}\) path. If total number of paths in the matrix are less than K, print 1.

CONSTRAINTS:
\(1 \le N * M \le 10^6\)
\(1 \le K \le 10^{18}\)
All the characters in the matrix will be lowercase English alphabets ('a'-'z').

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:50
1 votes
Tags:
AlgorithmsOpenApprovedHard
Points:50
Tags:
GeometryDynamic Programming
Points:50
14 votes
Tags:
Dynamic ProgrammingHardAlgorithmsMathematicsOpenApproved