Skip to content

Commit 526b902

Browse files
committed
feat: add solutions to lc problem: No.1643
No.1643.Kth Smallest Instructions
1 parent 1f57e5e commit 526b902

File tree

7 files changed

+370
-4
lines changed

7 files changed

+370
-4
lines changed

solution/1600-1699/1641.Count Sorted Vowel Strings/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@
6464

6565
**方法二:动态规划 + 前缀和**
6666

67-
定义 $f[i][j]$ 表示当前已经选了 $i$ 个元音字母,且最后一个元音字母是 $j$ 的方案数。初始时 $f[0][j]=1$。答案是 $\sum_{j = 0}^4 f[n - 1][j]$。
67+
定义 $f[i][j]$ 表示当前已经选了 $i$ 个元音字母,且最后一个元音字母是 $j$ 的方案数。初始时 $f[1][j]=1$。答案是 $\sum_{j = 0}^4 f[n][j]$。
6868

69-
我们可以发现$f[i][j]$ 只与 $f[i - 1][j]$ 有关,因此我们可以使用滚动数组优化空间复杂度
69+
我们可以发现 $f[i][j]$ 只与 $f[i - 1][j]$ 有关,因此可以将二维数组压缩为一维数组,从而优化空间复杂度
7070

7171
时间复杂度 $O(n \times C)$,空间复杂度 $O(C)$。其中 $n$ 为题目给定的整数,而 $C$ 是元音字母的数量,本题中 $C = 5$。
7272

solution/1600-1699/1643.Kth Smallest Instructions/README.md

Lines changed: 135 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,156 @@
6969

7070
<!-- 这里可写通用的实现逻辑 -->
7171

72+
**方法一:组合计数**
73+
74+
根据题目描述我们可以知道,最终的路径是由 $destination[0]$ 个 `'V'` 和 $destination[1]$ 个 `'H'` 组成的,且按字典序排列后的第 $k$ 条最小指令。
75+
76+
我们首先考虑字典序的最高位,即最左边的字符。如果高位字符是 `'V'`,那么所有以 `'H'` 开头的路径的字典序都比它小,而以 `'H'` 开头的路径总数为 $x = C_{v+h-1}^{h-1}$。
77+
78+
如果 $k \lt x$,那么高位字符一定是 `'V'`,我们将 $k$ 减去 $x$,并将 $v$ 减 $1$,然后继续考虑下一位字符;否则,高位字符一定是 `'H'`,我们将 $h$ 减 $1$,然后继续考虑下一位字符。
79+
80+
注意,如果 $h = 0$,那么高位字符一定是 `'V'`,因为剩下的字符都是 `'V'`
81+
82+
问题可以转换为求解 $C_{n}^{k}$,我们可以通过公式 $C_{n}^{k} = C_{n-1}^{k-1} + C_{n-1}^{k}$ 递推求解。
83+
84+
时间复杂度 $O((h + v) \times h)$,空间复杂度 $O((h + v) \times h)$。其中 $h$ 和 $v$ 分别为 $destination[1]$ 和 $destination[0]$。
85+
7286
<!-- tabs:start -->
7387

7488
### **Python3**
7589

7690
<!-- 这里可写当前语言的特殊实现逻辑 -->
7791

7892
```python
79-
93+
class Solution:
94+
def kthSmallestPath(self, destination: List[int], k: int) -> str:
95+
v, h = destination
96+
ans = []
97+
for _ in range(h + v):
98+
if h == 0:
99+
ans.append("V")
100+
else:
101+
x = comb(h + v - 1, h - 1)
102+
if k > x:
103+
ans.append("V")
104+
v -= 1
105+
k -= x
106+
else:
107+
ans.append("H")
108+
h -= 1
109+
return "".join(ans)
80110
```
81111

82112
### **Java**
83113

84114
<!-- 这里可写当前语言的特殊实现逻辑 -->
85115

86116
```java
117+
class Solution {
118+
public String kthSmallestPath(int[] destination, int k) {
119+
int v = destination[0], h = destination[1];
120+
int n = v + h;
121+
int[][] c = new int[n + 1][h + 1];
122+
c[0][0] = 1;
123+
for (int i = 1; i <= n; ++i) {
124+
c[i][0] = 1;
125+
for (int j = 1; j <= h; ++j) {
126+
c[i][j] = c[i - 1][j] + c[i - 1][j - 1];
127+
}
128+
}
129+
StringBuilder ans = new StringBuilder();
130+
for (int i = n; i > 0; --i) {
131+
if (h == 0) {
132+
ans.append('V');
133+
} else {
134+
int x = c[v + h - 1][h - 1];
135+
if (k > x) {
136+
ans.append('V');
137+
k -= x;
138+
--v;
139+
} else {
140+
ans.append('H');
141+
--h;
142+
}
143+
}
144+
}
145+
return ans.toString();
146+
}
147+
}
148+
```
149+
150+
### **C++**
151+
152+
```cpp
153+
class Solution {
154+
public:
155+
string kthSmallestPath(vector<int>& destination, int k) {
156+
int v = destination[0], h = destination[1];
157+
int n = v + h;
158+
int c[n + 1][h + 1];
159+
memset(c, 0, sizeof(c));
160+
c[0][0] = 1;
161+
for (int i = 1; i <= n; ++i) {
162+
c[i][0] = 1;
163+
for (int j = 1; j <= h; ++j) {
164+
c[i][j] = c[i - 1][j] + c[i - 1][j - 1];
165+
}
166+
}
167+
string ans;
168+
for (int i = 0; i < n; ++i) {
169+
if (h == 0) {
170+
ans.push_back('V');
171+
} else {
172+
int x = c[v + h - 1][h - 1];
173+
if (k > x) {
174+
ans.push_back('V');
175+
--v;
176+
k -= x;
177+
} else {
178+
ans.push_back('H');
179+
--h;
180+
}
181+
}
182+
}
183+
return ans;
184+
}
185+
};
186+
```
87187
188+
### **Go**
189+
190+
```go
191+
func kthSmallestPath(destination []int, k int) string {
192+
v, h := destination[0], destination[1]
193+
n := v + h
194+
c := make([][]int, n+1)
195+
for i := range c {
196+
c[i] = make([]int, h+1)
197+
c[i][0] = 1
198+
}
199+
for i := 1; i <= n; i++ {
200+
for j := 1; j <= h; j++ {
201+
c[i][j] = c[i-1][j] + c[i-1][j-1]
202+
}
203+
}
204+
ans := []byte{}
205+
for i := 0; i < n; i++ {
206+
if h == 0 {
207+
ans = append(ans, 'V')
208+
} else {
209+
x := c[v+h-1][h-1]
210+
if k > x {
211+
ans = append(ans, 'V')
212+
k -= x
213+
v--
214+
} else {
215+
ans = append(ans, 'H')
216+
h--
217+
}
218+
}
219+
}
220+
return string(ans)
221+
}
88222
```
89223

90224
### **...**

solution/1600-1699/1643.Kth Smallest Instructions/README_EN.md

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,133 @@
6565
### **Python3**
6666

6767
```python
68-
68+
class Solution:
69+
def kthSmallestPath(self, destination: List[int], k: int) -> str:
70+
v, h = destination
71+
ans = []
72+
for _ in range(h + v):
73+
if h == 0:
74+
ans.append("V")
75+
else:
76+
x = comb(h + v - 1, h - 1)
77+
if k > x:
78+
ans.append("V")
79+
v -= 1
80+
k -= x
81+
else:
82+
ans.append("H")
83+
h -= 1
84+
return "".join(ans)
6985
```
7086

7187
### **Java**
7288

7389
```java
90+
class Solution {
91+
public String kthSmallestPath(int[] destination, int k) {
92+
int v = destination[0], h = destination[1];
93+
int n = v + h;
94+
int[][] c = new int[n + 1][h + 1];
95+
c[0][0] = 1;
96+
for (int i = 1; i <= n; ++i) {
97+
c[i][0] = 1;
98+
for (int j = 1; j <= h; ++j) {
99+
c[i][j] = c[i - 1][j] + c[i - 1][j - 1];
100+
}
101+
}
102+
StringBuilder ans = new StringBuilder();
103+
for (int i = n; i > 0; --i) {
104+
if (h == 0) {
105+
ans.append('V');
106+
} else {
107+
int x = c[v + h - 1][h - 1];
108+
if (k > x) {
109+
ans.append('V');
110+
k -= x;
111+
--v;
112+
} else {
113+
ans.append('H');
114+
--h;
115+
}
116+
}
117+
}
118+
return ans.toString();
119+
}
120+
}
121+
```
122+
123+
### **C++**
124+
125+
```cpp
126+
class Solution {
127+
public:
128+
string kthSmallestPath(vector<int>& destination, int k) {
129+
int v = destination[0], h = destination[1];
130+
int n = v + h;
131+
int c[n + 1][h + 1];
132+
memset(c, 0, sizeof(c));
133+
c[0][0] = 1;
134+
for (int i = 1; i <= n; ++i) {
135+
c[i][0] = 1;
136+
for (int j = 1; j <= h; ++j) {
137+
c[i][j] = c[i - 1][j] + c[i - 1][j - 1];
138+
}
139+
}
140+
string ans;
141+
for (int i = 0; i < n; ++i) {
142+
if (h == 0) {
143+
ans.push_back('V');
144+
} else {
145+
int x = c[v + h - 1][h - 1];
146+
if (k > x) {
147+
ans.push_back('V');
148+
--v;
149+
k -= x;
150+
} else {
151+
ans.push_back('H');
152+
--h;
153+
}
154+
}
155+
}
156+
return ans;
157+
}
158+
};
159+
```
74160
161+
### **Go**
162+
163+
```go
164+
func kthSmallestPath(destination []int, k int) string {
165+
v, h := destination[0], destination[1]
166+
n := v + h
167+
c := make([][]int, n+1)
168+
for i := range c {
169+
c[i] = make([]int, h+1)
170+
c[i][0] = 1
171+
}
172+
for i := 1; i <= n; i++ {
173+
for j := 1; j <= h; j++ {
174+
c[i][j] = c[i-1][j] + c[i-1][j-1]
175+
}
176+
}
177+
ans := []byte{}
178+
for i := 0; i < n; i++ {
179+
if h == 0 {
180+
ans = append(ans, 'V')
181+
} else {
182+
x := c[v+h-1][h-1]
183+
if k > x {
184+
ans = append(ans, 'V')
185+
k -= x
186+
v--
187+
} else {
188+
ans = append(ans, 'H')
189+
h--
190+
}
191+
}
192+
}
193+
return string(ans)
194+
}
75195
```
76196

77197
### **...**
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
public:
3+
string kthSmallestPath(vector<int>& destination, int k) {
4+
int v = destination[0], h = destination[1];
5+
int n = v + h;
6+
int c[n + 1][h + 1];
7+
memset(c, 0, sizeof(c));
8+
c[0][0] = 1;
9+
for (int i = 1; i <= n; ++i) {
10+
c[i][0] = 1;
11+
for (int j = 1; j <= h; ++j) {
12+
c[i][j] = c[i - 1][j] + c[i - 1][j - 1];
13+
}
14+
}
15+
string ans;
16+
for (int i = 0; i < n; ++i) {
17+
if (h == 0) {
18+
ans.push_back('V');
19+
} else {
20+
int x = c[v + h - 1][h - 1];
21+
if (k > x) {
22+
ans.push_back('V');
23+
--v;
24+
k -= x;
25+
} else {
26+
ans.push_back('H');
27+
--h;
28+
}
29+
}
30+
}
31+
return ans;
32+
}
33+
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
func kthSmallestPath(destination []int, k int) string {
2+
v, h := destination[0], destination[1]
3+
n := v + h
4+
c := make([][]int, n+1)
5+
for i := range c {
6+
c[i] = make([]int, h+1)
7+
c[i][0] = 1
8+
}
9+
for i := 1; i <= n; i++ {
10+
for j := 1; j <= h; j++ {
11+
c[i][j] = c[i-1][j] + c[i-1][j-1]
12+
}
13+
}
14+
ans := []byte{}
15+
for i := 0; i < n; i++ {
16+
if h == 0 {
17+
ans = append(ans, 'V')
18+
} else {
19+
x := c[v+h-1][h-1]
20+
if k > x {
21+
ans = append(ans, 'V')
22+
k -= x
23+
v--
24+
} else {
25+
ans = append(ans, 'H')
26+
h--
27+
}
28+
}
29+
}
30+
return string(ans)
31+
}

0 commit comments

Comments
 (0)