Skip to content

Commit ecb9ccc

Browse files
committed
added problem 944
1 parent 2df0928 commit ecb9ccc

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

problems/problem0944/del_cols.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package problem0944
2+
3+
/*
4+
You are given an array of n strings strs, all of the same length.
5+
The strings can be arranged such that there is one on each line, making a grid.
6+
For example, strs = ["abc", "bce", "cae"] can be arranged as:
7+
abc
8+
bce
9+
cae
10+
You want to delete the columns that are not sorted lexicographically.
11+
In the above example (0-indexed), columns 0 ('a', 'b', 'c') and 2 ('c', 'e', 'e') are sorted while
12+
column 1 ('b', 'c', 'a') is not,so you would delete column 1.
13+
Return the number of columns that you will delete.
14+
*/
15+
16+
func minDeletionSize(strs []string) int {
17+
var res int
18+
eachCol:
19+
for i := range strs[0] {
20+
var prev = strs[0][i]
21+
for j := 1; j < len(strs); j++ {
22+
if strs[j][i] < prev {
23+
res++
24+
continue eachCol
25+
}
26+
prev = strs[j][i]
27+
}
28+
}
29+
return res
30+
}

problems/problem0944/del_cols_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package problem0944
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
type Result struct {
11+
Input []string
12+
Expected int
13+
}
14+
15+
var Results = []Result{
16+
{[]string{"abc", "bce", "cae"}, 1},
17+
{[]string{"cba", "daf", "ghi"}, 1},
18+
{[]string{"a", "b"}, 0},
19+
{[]string{"zyx", "wvu", "tsr"}, 3},
20+
}
21+
22+
func TestDeleteCollumnsNotSorted(t *testing.T) {
23+
assert := assert.New(t)
24+
25+
for _, res := range Results {
26+
want := res.Expected
27+
got := minDeletionSize(res.Input)
28+
assert.Equal(want, got, fmt.Sprintf("%+v", res))
29+
}
30+
}

readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ Each problem is in it's own directory, with test files. There are helper package
244244
| 0931 | [Minimum Falling Path Sum](https://leetcode.com/problems/minimum-falling-path-sum) | [My Solution](./problems/problem0931) ||
245245
| 0936 | [Stamping The Sequence](https://leetcode.com/problems/stamping-the-sequence) | [My Solution](./problems/problem0936) ||
246246
| 0938 | [Range Sum of BST](https://leetcode.com/problems/range-sum-of-bst) | [My Solution](./problems/problem0938) ||
247+
| 0944 | [Delete Columns to Make Sorted](https://leetcode.com/problems/delete-columns-to-make-sorted) | [My Solution](./problems/problem0944) ||
247248
| 0947 | [Most Stones Removed with Same Row or Column](https://leetcode.com/problems/most-stones-removed-with-same-row-or-column) | [My Solution](./problems/problem0947) ||
248249
| 0948 | [Bag of Tokens](https://leetcode.com/problems/bag-of-tokens) | [My Solution](./problems/problem0948) ||
249250
| 0951 | [Flip Equivalent Binary Trees](https://leetcode.com/problems/flip-equivalent-binary-trees) | [My Solution](./problems/problem0951) ||

0 commit comments

Comments
 (0)