Skip to content

Commit 1918ead

Browse files
authored
Merge pull request #1125 from 0xff-dev/2375
Add solution and test-cases for problem 2375
2 parents 8a99f00 + 95e49c8 commit 1918ead

File tree

3 files changed

+56
-23
lines changed

3 files changed

+56
-23
lines changed

leetcode/2301-2400/2375.Construct-Smallest-Number-From-DI-String/README.md

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,38 @@
11
# [2375.Construct Smallest Number From DI String][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given a **0-indexed** string `pattern` of length `n` consisting of the characters `'I'` meaning **increasing** and `'D'` meaning **decreasing**.
5+
6+
A **0-indexed** string `num` of length `n + 1` is created using the following conditions:
7+
8+
- `num` consists of the digits `'1'` to `'9'`, where each digit is used **at most** once.
9+
- If `pattern[i] == 'I'`, then `num[i] < num[i + 1]`.
10+
- If `pattern[i] == 'D'`, then `num[i] > num[i + 1]`.
11+
12+
Return the lexicographically **smallest** possible string `num` that meets the conditions.
713

814
**Example 1:**
915

1016
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
17+
Input: pattern = "IIIDIDDD"
18+
Output: "123549876"
19+
Explanation:
20+
At indices 0, 1, 2, and 4 we must have that num[i] < num[i+1].
21+
At indices 3, 5, 6, and 7 we must have that num[i] > num[i+1].
22+
Some possible values of num are "245639871", "135749862", and "123849765".
23+
It can be proven that "123549876" is the smallest possible num that meets the conditions.
24+
Note that "123414321" is not possible because the digit '1' is used more than once.
1325
```
1426

15-
## 题意
16-
> ...
27+
**Example 2:**
1728

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Construct Smallest Number From DI String
23-
```go
2429
```
25-
30+
Input: pattern = "DDD"
31+
Output: "4321"
32+
Explanation:
33+
Some possible values of num are "9876", "7321", and "8742".
34+
It can be proven that "4321" is the smallest possible num that meets the conditions.
35+
```
2636

2737
## 结语
2838

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(pattern string) string {
4+
res := make([]byte, len(pattern)+1)
5+
for i := range res {
6+
res[i] = '0'
7+
}
8+
9+
index := byte('1')
10+
for i := 0; i < len(pattern); i++ {
11+
if pattern[i] == 'D' {
12+
continue
13+
}
14+
res[i] = index
15+
index++
16+
for pre := i - 1; pre >= 0 && res[pre] == '0'; pre-- {
17+
res[pre] = index
18+
index++
19+
}
20+
}
21+
22+
res[len(pattern)] = index
23+
index++
24+
for pre := len(pattern) - 1; pre >= 0 && res[pre] == '0'; pre-- {
25+
res[pre] = index
26+
index++
27+
}
28+
return string(res)
529
}

leetcode/2301-2400/2375.Construct-Smallest-Number-From-DI-String/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs string
14+
expect string
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", "IIIDIDDD", "123549876"},
17+
{"TestCase2", "DDD", "4321"},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)