Skip to content

Commit 01fc99a

Browse files
authored
Merge pull request #1406 from 0xff-dev/960
Add solution and test-cases for problem 960
2 parents 3476e56 + 6a1b9a2 commit 01fc99a

File tree

3 files changed

+69
-22
lines changed

3 files changed

+69
-22
lines changed

leetcode/901-1000/0960.Delete-Columns-to-Make-Sorted-III/README.md

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,39 @@
11
# [960.Delete Columns to Make Sorted III][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 an array of `n` strings `strs`, all of the same length.
5+
6+
We may choose any deletion indices, and we delete all the characters in those indices for each string.
7+
8+
For example, if we have `strs = ["abcdef","uvwxyz"]` and deletion indices `{0, 2, 3}`, then the final array after deletions is `["bef", "vyz"]`.
9+
10+
Suppose we chose a set of deletion indices answer such that after deletions, the final array has **every string (row) in lexicographic** order. (i.e., `(strs[0][0] <= strs[0][1] <= ... <= strs[0][strs[0].length - 1])`, and `(strs[1][0] <= strs[1][1] <= ... <= strs[1][strs[1].length - 1])`, and so on). Return the minimum possible value of `answer.length`.
711

812
**Example 1:**
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
15+
Input: strs = ["babca","bbazb"]
16+
Output: 3
17+
Explanation: After deleting columns 0, 1, and 4, the final array is strs = ["bc", "az"].
18+
Both these rows are individually in lexicographic order (ie. strs[0][0] <= strs[0][1] and strs[1][0] <= strs[1][1]).
19+
Note that strs[0] > strs[1] - the array strs is not necessarily in lexicographic order.
1320
```
1421

15-
## 题意
16-
> ...
17-
18-
## 题解
22+
**Example 2:**
1923

20-
### 思路1
21-
> ...
22-
Delete Columns to Make Sorted III
23-
```go
2424
```
25+
Input: strs = ["edcba"]
26+
Output: 4
27+
Explanation: If we delete less than 4 columns, the only row will not be lexicographically sorted.
28+
```
29+
30+
**Example 3:**
2531

32+
```
33+
Input: strs = ["ghi","def","abc"]
34+
Output: 0
35+
Explanation: All rows are already lexicographically sorted.
36+
```
2637

2738
## 结语
2839

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(A []string) int {
4+
if len(A) == 0 {
5+
return 0
6+
}
7+
8+
numCols := len(A[0])
9+
dp := make([]int, numCols)
10+
11+
for i := range dp {
12+
dp[i] = 1
13+
}
14+
15+
for i := numCols - 2; i >= 0; i-- {
16+
for j := i + 1; j < numCols; j++ {
17+
isSorted := true
18+
for _, row := range A {
19+
if row[i] > row[j] {
20+
isSorted = false
21+
break
22+
}
23+
}
24+
25+
if isSorted {
26+
if dp[j]+1 > dp[i] {
27+
dp[i] = dp[j] + 1
28+
}
29+
}
30+
}
31+
}
32+
33+
maxKept := 0
34+
for _, val := range dp {
35+
if val > maxKept {
36+
maxKept = val
37+
}
38+
}
39+
40+
return numCols - maxKept
541
}

leetcode/901-1000/0960.Delete-Columns-to-Make-Sorted-III/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []string
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []string{"babca", "bbazb"}, 3},
17+
{"TestCase2", []string{"edcba"}, 4},
18+
{"TestCase3", []string{"ghi", "def", "abc"}, 0},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)