Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@
# [2375.Construct Smallest Number From DI String][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
You are given a **0-indexed** string `pattern` of length `n` consisting of the characters `'I'` meaning **increasing** and `'D'` meaning **decreasing**.

A **0-indexed** string `num` of length `n + 1` is created using the following conditions:

- `num` consists of the digits `'1'` to `'9'`, where each digit is used **at most** once.
- If `pattern[i] == 'I'`, then `num[i] < num[i + 1]`.
- If `pattern[i] == 'D'`, then `num[i] > num[i + 1]`.

Return the lexicographically **smallest** possible string `num` that meets the conditions.

**Example 1:**

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

## 题意
> ...
**Example 2:**

## 题解

### 思路1
> ...
Construct Smallest Number From DI String
```go
```

Input: pattern = "DDD"
Output: "4321"
Explanation:
Some possible values of num are "9876", "7321", and "8742".
It can be proven that "4321" is the smallest possible num that meets the conditions.
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(pattern string) string {
res := make([]byte, len(pattern)+1)
for i := range res {
res[i] = '0'
}

index := byte('1')
for i := 0; i < len(pattern); i++ {
if pattern[i] == 'D' {
continue
}
res[i] = index
index++
for pre := i - 1; pre >= 0 && res[pre] == '0'; pre-- {
res[pre] = index
index++
}
}

res[len(pattern)] = index
index++
for pre := len(pattern) - 1; pre >= 0 && res[pre] == '0'; pre-- {
res[pre] = index
index++
}
return string(res)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs string
expect string
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", "IIIDIDDD", "123549876"},
{"TestCase2", "DDD", "4321"},
}

// 开始测试
Expand All @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading