diff --git a/leetcode/901-1000/0960.Delete-Columns-to-Make-Sorted-III/README.md b/leetcode/901-1000/0960.Delete-Columns-to-Make-Sorted-III/README.md index 21ecf387c..f6d5d004f 100644 --- a/leetcode/901-1000/0960.Delete-Columns-to-Make-Sorted-III/README.md +++ b/leetcode/901-1000/0960.Delete-Columns-to-Make-Sorted-III/README.md @@ -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. +``` ## 结语 diff --git a/leetcode/901-1000/0960.Delete-Columns-to-Make-Sorted-III/Solution.go b/leetcode/901-1000/0960.Delete-Columns-to-Make-Sorted-III/Solution.go index d115ccf5e..a52fc0926 100644 --- a/leetcode/901-1000/0960.Delete-Columns-to-Make-Sorted-III/Solution.go +++ b/leetcode/901-1000/0960.Delete-Columns-to-Make-Sorted-III/Solution.go @@ -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 } diff --git a/leetcode/901-1000/0960.Delete-Columns-to-Make-Sorted-III/Solution_test.go b/leetcode/901-1000/0960.Delete-Columns-to-Make-Sorted-III/Solution_test.go index 14ff50eb4..cc653ea7b 100644 --- a/leetcode/901-1000/0960.Delete-Columns-to-Make-Sorted-III/Solution_test.go +++ b/leetcode/901-1000/0960.Delete-Columns-to-Make-Sorted-III/Solution_test.go @@ -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}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }