Skip to content

Commit 0a58c3d

Browse files
committed
Add KthSmallestInstructions problem's solution in C.
1 parent 97f4fd2 commit 0a58c3d

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,6 @@ Together, we grow faster 🚀
3737
|6|[ZigzagConversion](https://leetcode.com/problems/zigzag-conversion/)|[C](./Solutions/ZigzagConversion/sol.c)|Medium|
3838
|9|[PalindromeNumber](https://leetcode.com/problems/palindrome-number/)|[C](./Solutions/PalindromeNumber/sol.c)|Easy|
3939
|96|[UniqueBinarySearchTrees](https://leetcode.com/problems/unique-binary-search-trees/)|[C](./Solutions/UniqueBinarySearchTrees/C/sol.c)|Medium|
40+
|1643|[KthSmallestInstructions](https://leetcode.com/problems/kth-smallest-instructions/)|[C](./Solutions/KthSmallestInstructions/C/sol.c)|<span style="color:red">Hard</span>|
4041

4142
Made with ❤️ by Marius
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# K-th Smallest Lexicographical Path
2+
3+
## Problem Description
4+
5+
You are given a grid with dimensions defined by a destination point `[row, column]`. Starting at the origin `(0, 0)`, you can only move **right ('H')** or **down ('V')**. Your task is to find the **k-th lexicographically smallest path** that leads to the destination.
6+
7+
## Example
8+
9+
Input:
10+
```c
11+
destination = [2, 3] // 2 vertical moves, 3 horizontal moves
12+
k = 4
13+
```
14+
15+
Output:
16+
```
17+
"HVHHV"
18+
```
19+
20+
## Total Number of Paths
21+
22+
To reach a point `(v, h)`, where:
23+
- `v` is the number of vertical steps (`V`),
24+
- `h` is the number of horizontal steps (`H`),
25+
26+
you need to construct a string of length `v + h` that consists of exactly:
27+
- `v` occurrences of `'V'`,
28+
- `h` occurrences of `'H'`.
29+
30+
The total number of such unique paths is given by the combination formula:
31+
32+
\[
33+
C(v + h, h) = \frac{(v + h)!}{v! \cdot h!}
34+
\]
35+
36+
This represents the number of ways to arrange `h` H's among `v + h` total steps.
37+
38+
---
39+
40+
## Approach: Combinatorial Logic
41+
42+
We want to construct the k-th lexicographical string, step by step. At every step, we decide whether to place an `'H'` or a `'V'` next.
43+
44+
### Lexicographical Order
45+
46+
- `'H'` comes before `'V'` in lexicographical order.
47+
- So, in order to build the smallest string, we prefer `'H'` whenever possible.
48+
49+
### Recursive Logic
50+
51+
At each position in the result string:
52+
1. We try placing `'H'`.
53+
- If we do this, the remaining number of valid paths is `C(v + h - 1, h - 1)`.
54+
- If `k` is **less than or equal to** this value, then we know the desired path starts with `'H'`.
55+
2. Otherwise, we place `'V'`, reduce `k` accordingly (since we skipped over the paths that started with `'H'`), and continue.
56+
57+
This process repeats until all steps are placed.
58+
59+
---
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
long long comb(int n, int k) {
2+
long long res = 1;
3+
for (int i = 1; i <= k; i++) {
4+
res = res * (n - i + 1) / i;
5+
}
6+
return res;
7+
}
8+
9+
10+
char* kthSmallestPath(int* destination, int destinationSize, int k) {
11+
int v = destination[0]; // număr de V
12+
int h = destination[1]; // număr de H
13+
int total = v + h;
14+
char *path = malloc(total + 1);
15+
path[total] = '\0';
16+
17+
for (int i = 0; i < total; i++) {
18+
if (h == 0) {
19+
path[i] = 'V';
20+
v--;
21+
} else if (v == 0) {
22+
path[i] = 'H';
23+
h--;
24+
} else {
25+
long long countH = comb(h + v - 1, h - 1);
26+
if (k <= countH) {
27+
path[i] = 'H';
28+
h--;
29+
} else {
30+
path[i] = 'V';
31+
v--;
32+
k -= countH;
33+
}
34+
}
35+
}
36+
37+
return path;
38+
}

0 commit comments

Comments
 (0)