diff --git a/leetcode/901-1000/0955.Delete-Columns-to-Make-Sorted-II/README.md b/leetcode/901-1000/0955.Delete-Columns-to-Make-Sorted-II/README.md index 91055fe90..ce0a2c499 100644 --- a/leetcode/901-1000/0955.Delete-Columns-to-Make-Sorted-II/README.md +++ b/leetcode/901-1000/0955.Delete-Columns-to-Make-Sorted-II/README.md @@ -1,28 +1,43 @@ # [955.Delete Columns to Make Sorted II][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 its elements in **lexicographic** order (i.e., `strs[0] <= strs[1] <= strs[2] <= ... <= strs[n - 1]`). Return the minimum possible value of `answer.length`. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: strs = ["ca","bb","ac"] +Output: 1 +Explanation: +After deleting the first column, strs = ["a", "b", "c"]. +Now strs is in lexicographic order (ie. strs[0] <= strs[1] <= strs[2]). +We require at least 1 deletion since initially strs was not in lexicographic order, so the answer is 1. ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Delete Columns to Make Sorted II -```go ``` +Input: strs = ["xc","yb","za"] +Output: 0 +Explanation: +strs is already in lexicographic order, so we do not need to delete anything. +Note that the rows of strs are not necessarily in lexicographic order: +i.e., it is NOT necessarily true that (strs[0][0] <= strs[0][1] <= ...) +``` + +**Example 3:** +``` +Input: strs = ["zyx","wvu","tsr"] +Output: 3 +Explanation: We have to delete every column. +``` ## 结语 diff --git a/leetcode/901-1000/0955.Delete-Columns-to-Make-Sorted-II/Solution.go b/leetcode/901-1000/0955.Delete-Columns-to-Make-Sorted-II/Solution.go index d115ccf5e..2ac4cc98e 100644 --- a/leetcode/901-1000/0955.Delete-Columns-to-Make-Sorted-II/Solution.go +++ b/leetcode/901-1000/0955.Delete-Columns-to-Make-Sorted-II/Solution.go @@ -1,5 +1,30 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(strs []string) int { + var ret, row int + rows := len(strs) + cols := len(strs[0]) + + sorted := make([]bool, rows-1) + /* + bbjwefkpb + axmksfchw + */ + for col := 0; col < cols; col++ { + for row = 0; row < rows-1; row++ { + if !sorted[row] && strs[row][col] > strs[row+1][col] { + ret++ + break + } + } + if row == rows-1 { + for row = 0; row < rows-1; row++ { + if !sorted[row] && strs[row][col] < strs[row+1][col] { + sorted[row] = true + } + } + } + + } + return ret } diff --git a/leetcode/901-1000/0955.Delete-Columns-to-Make-Sorted-II/Solution_test.go b/leetcode/901-1000/0955.Delete-Columns-to-Make-Sorted-II/Solution_test.go index 14ff50eb4..1f09d1eda 100644 --- a/leetcode/901-1000/0955.Delete-Columns-to-Make-Sorted-II/Solution_test.go +++ b/leetcode/901-1000/0955.Delete-Columns-to-Make-Sorted-II/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{"ca", "bb", "ac"}, 1}, + {"TestCase2", []string{"xc", "yb", "za"}, 0}, + {"TestCase3", []string{"zyx", "wvu", "tsr"}, 3}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }