Skip to content
Open
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
37 changes: 24 additions & 13 deletions leetcode/901-1000/0960.Delete-Columns-to-Make-Sorted-III/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,39 @@
# [960.Delete Columns to Make Sorted III][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 an array of `n` strings `strs`, all of the same length.

We may choose any deletion indices, and we delete all the characters in those indices for each string.

For example, if we have `strs = ["abcdef","uvwxyz"]` and deletion indices `{0, 2, 3}`, then the final array after deletions is `["bef", "vyz"]`.

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`.

**Example 1:**

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

## 题意
> ...

## 题解
**Example 2:**

### 思路1
> ...
Delete Columns to Make Sorted III
```go
```
Input: strs = ["edcba"]
Output: 4
Explanation: If we delete less than 4 columns, the only row will not be lexicographically sorted.
```

**Example 3:**

```
Input: strs = ["ghi","def","abc"]
Output: 0
Explanation: All rows are already lexicographically sorted.
```

## 结语

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

func Solution(x bool) bool {
return x
func Solution(A []string) int {
if len(A) == 0 {
return 0
}

numCols := len(A[0])
dp := make([]int, numCols)

for i := range dp {
dp[i] = 1
}

for i := numCols - 2; i >= 0; i-- {
for j := i + 1; j < numCols; j++ {
isSorted := true
for _, row := range A {
if row[i] > row[j] {
isSorted = false
break
}
}

if isSorted {
if dp[j]+1 > dp[i] {
dp[i] = dp[j] + 1
}
}
}
}

maxKept := 0
for _, val := range dp {
if val > maxKept {
maxKept = val
}
}

return numCols - maxKept
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs []string
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []string{"babca", "bbazb"}, 3},
{"TestCase2", []string{"edcba"}, 4},
{"TestCase3", []string{"ghi", "def", "abc"}, 0},
}

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

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

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