Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0838927

Browse files
committed
Merge pull request #394 from 0xff-dev/944
Add solution and test-cases for problem 944
2 parents ca04225 + 462d733 commit 0838927

File tree

3 files changed

+61
-22
lines changed

3 files changed

+61
-22
lines changed

‎leetcode/901-1000/0944.Delete-Columns-to-Make-Sorted/README.md

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,54 @@
11
# [944.Delete Columns to Make Sorted][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.
75

8-
**Example 1:**
6+
The strings can be arranged such that there is one on each line, making a grid. For example, `strs = ["abc", "bce", "cae"]` can be arranged as:
97

108
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
9+
abc
10+
bce
11+
cae
1312
```
1413

15-
## 题意
16-
> ...
14+
You want to **delete** the columns that are **not sorted lexicographically**. In the above example (0-indexed), columns 0 (`'a'`, `'b'`, `'c'`) and 2 (`'c'`, `'e'`, `'e'`) are sorted while column 1 (`'b'`, `'c'`, `'a'`) is not, so you would delete column 1.
15+
16+
Return the number of columns that you will delete.
17+
18+
**Example 1:**
19+
20+
```
21+
Input: strs = ["cba","daf","ghi"]
22+
Output: 1
23+
Explanation: The grid looks as follows:
24+
cba
25+
daf
26+
ghi
27+
Columns 0 and 2 are sorted, but column 1 is not, so you only need to delete 1 column.
28+
```
1729

18-
## 题解
30+
**Example 2:**
1931

20-
### 思路1
21-
> ...
22-
Delete Columns to Make Sorted
23-
```go
32+
```
33+
Input: strs = ["a","b"]
34+
Output: 0
35+
Explanation: The grid looks as follows:
36+
a
37+
b
38+
Column 0 is the only column and is sorted, so you will not delete any columns.
2439
```
2540

41+
**Example 3:**
42+
43+
```
44+
Input: strs = ["zyx","wvu","tsr"]
45+
Output: 3
46+
Explanation: The grid looks as follows:
47+
zyx
48+
wvu
49+
tsr
50+
All 3 columns are not sorted, so you will delete all 3.
51+
```
2652

2753
## 结语
2854

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(strs []string) int {
4+
ans := 0
5+
rows := len(strs)
6+
cols := len(strs[0])
7+
for col := 0; col < cols; col++ {
8+
c := strs[0][col]
9+
for row := 1; row < rows; row++ {
10+
if strs[row][col] < c {
11+
ans++
12+
break
13+
}
14+
c = strs[row][col]
15+
}
16+
}
17+
return ans
518
}

‎leetcode/901-1000/0944.Delete-Columns-to-Make-Sorted/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{"cba", "daf", "ghi"}, 1},
17+
{"TestCase2", []string{"a", "b"}, 0},
18+
{"TestCase3", []string{"zyx", "wvu", "tsr"}, 3},
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)

AltStyle によって変換されたページ (->オリジナル) /