Skip to content

Commit ffd35d7

Browse files
committed
Merge branch 'develop'
2 parents 8266599 + 0309900 commit ffd35d7

File tree

12 files changed

+210
-64
lines changed

12 files changed

+210
-64
lines changed

leetcode/1201-1300/1277.Count-Square-Submatrices-with-All-Ones/README.md

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,31 @@
11
# [1277.Count Square Submatrices with All Ones][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
74

5+
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.
6+
87
**Example 1:**
98

109
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
10+
Input: matrix =
11+
[
12+
[0,1,1,1],
13+
[1,1,1,1],
14+
[0,1,1,1]
15+
]
16+
Output: 15
17+
```
18+
19+
**Example 2:**
20+
21+
```
22+
Input: matrix =
23+
[
24+
[1,0,1],
25+
[1,1,0],
26+
[1,1,0]
27+
]
28+
Output: 7
1329
```
1430

1531
## 题意
Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(matrix [][]int) int {
4+
n, m := len(matrix), len(matrix[0])
5+
dp := make([][]int, n + 1)
6+
for i := range dp {
7+
dp[i] = make([]int, m + 1)
8+
}
9+
res := 0
10+
for i := 1; i < n + 1; i++ {
11+
for j := 1; j < m + 1; j++ {
12+
if matrix[i - 1][j - 1] == 1 {
13+
dp[i][j] = 1 + min(dp[i - 1][j - 1], min(dp[i - 1][j], dp[i][j - 1]))
14+
}
15+
res += dp[i][j]
16+
}
17+
}
18+
return res
19+
}
20+
21+
func min(a, b int) int {
22+
if a < b { return a }
23+
return b
524
}

leetcode/1201-1300/1277.Count-Square-Submatrices-with-All-Ones/Solution_test.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,19 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs [][]int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase", [][]int{
17+
{0,1,1,1},
18+
{1,1,1,1},
19+
{0,1,1,1},
20+
}, 15},
21+
{"TestCase", [][]int{
22+
{1,0,1},
23+
{1,1,0},
24+
{1,1,0},
25+
}, 7},
1926
}
2027

2128
// 开始测试

leetcode/201-300/0295.Find-Median-from-Data-Stream/README.md

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
11
# [295.Find Median from Data Stream][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
74

5+
The median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value and the median is the mean of the two middle values.
6+
7+
For example, for arr = [2,3,4], the median is 3.
8+
For example, for arr = [2,3], the median is (2 + 3) / 2 = 2.5.
9+
10+
Implement the MedianFinder class:
11+
12+
MedianFinder() initializes the MedianFinder object.
13+
void addNum(int num) adds the integer num from the data stream to the data structure.
14+
double findMedian() returns the median of all elements so far. Answers within 10-5 of the actual answer will be accepted.
15+
16+
817
**Example 1:**
918

1019
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
20+
Input
21+
["MedianFinder", "addNum", "addNum", "findMedian", "addNum", "findMedian"]
22+
[[], [1], [2], [], [3], []]
23+
Output
24+
[null, null, null, 1.5, null, 2.0]
1325
```
1426

1527
## 题意
Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,47 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "container/heap"
4+
5+
type IntHeap []int
6+
7+
func (ih IntHeap) Len() int { return len(ih) }
8+
func (ih IntHeap) Less(i, j int) bool { return ih[i] < ih[j] }
9+
func (ih IntHeap) Swap(i, j int) { ih[i], ih[j] = ih[j], ih[i] }
10+
func (ih *IntHeap) Push(num interface{}) {
11+
*ih = append(*ih, num.(int))
12+
}
13+
func (ih *IntHeap) Pop() interface{} {
14+
old := *ih
15+
n := ih.Len()
16+
num := old[n - 1]
17+
*ih = old[: n - 1]
18+
return num
19+
}
20+
21+
type MedianFinder struct {
22+
Small IntHeap
23+
Large IntHeap
524
}
25+
26+
func Constructor() MedianFinder {
27+
return MedianFinder{[]int{}, []int{}}
28+
}
29+
30+
func (mf *MedianFinder) AddNum(num int) {
31+
if mf.Small.Len() == mf.Large.Len() {
32+
heap.Push(&mf.Large, -num)
33+
val := heap.Pop(&mf.Large)
34+
heap.Push(&mf.Small, -(val.(int)))
35+
} else {
36+
heap.Push(&mf.Small, num)
37+
val := heap.Pop(&mf.Small)
38+
heap.Push(&mf.Large, -(val.(int)))
39+
}
40+
}
41+
42+
func (mf *MedianFinder) FindMedian() float64 {
43+
if mf.Small.Len() == mf.Large.Len() {
44+
return float64(mf.Small[0] - mf.Large[0]) / 2
45+
}
46+
return float64(mf.Small[0])
47+
}

leetcode/201-300/0295.Find-Median-from-Data-Stream/Solution_test.go

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,52 @@ package Solution
22

33
import (
44
"reflect"
5-
"strconv"
65
"testing"
76
)
87

98
func TestSolution(t *testing.T) {
109
// 测试用例
11-
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
15-
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
10+
var medianFinder MedianFinder
11+
var median float64
12+
13+
// Test Case 1
14+
medianFinder = Constructor()
15+
medianFinder.AddNum(1)
16+
medianFinder.AddNum(2)
17+
median = medianFinder.FindMedian()
18+
if !reflect.DeepEqual(median, 1.5) {
19+
t.Fatalf("expected: %v, but got: %v", 1.5, median)
20+
}
21+
medianFinder.AddNum(3)
22+
median = medianFinder.FindMedian()
23+
if !reflect.DeepEqual(median, 2.0) {
24+
t.Fatalf("expected: %v, but got: %v", 2.0, median)
1925
}
2026

21-
// 开始测试
22-
for i, c := range cases {
23-
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25-
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
28-
}
29-
})
27+
// Test Case 2
28+
medianFinder = Constructor()
29+
medianFinder.AddNum(2)
30+
medianFinder.AddNum(1)
31+
median = medianFinder.FindMedian()
32+
if !reflect.DeepEqual(median, 1.5) {
33+
t.Fatalf("expected: %v, but got: %v", 1.5, median)
34+
}
35+
medianFinder.AddNum(5)
36+
median = medianFinder.FindMedian()
37+
if !reflect.DeepEqual(median, 2.0) {
38+
t.Fatalf("expected: %v, but got: %v", 2.0, median)
39+
}
40+
medianFinder.AddNum(7)
41+
medianFinder.AddNum(2)
42+
median = medianFinder.FindMedian()
43+
if !reflect.DeepEqual(median, 2.0) {
44+
t.Fatalf("expected: %v, but got: %v", 2.0, median)
45+
}
46+
medianFinder.AddNum(0)
47+
medianFinder.AddNum(5)
48+
median = medianFinder.FindMedian()
49+
if !reflect.DeepEqual(median, 2.0) {
50+
t.Fatalf("expected: %v, but got: %v", 2.0, median)
3051
}
3152
}
3253

leetcode/301-400/0338.Counting-Bits/README.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
# [338.Counting Bits][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
74

5+
Given an integer n, return an array ans of length n + 1 such that for each i (0 <= i <= n), ans[i] is the number of 1's in the binary representation of i.
6+
87
**Example 1:**
98

109
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
10+
Input: n = 2
11+
Output: [0,1,1]
12+
```
13+
14+
**Example 2:**
15+
16+
```
17+
Input: n = 5
18+
Output: [0,1,1,2,1,2]
1319
```
1420

1521
## 题意
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(n int) []int {
4+
res := make([]int, n + 1)
5+
for i := 1; i < n + 1; i++ {
6+
if i % 2 == 0 {
7+
res[i] = res[i/2]
8+
} else {
9+
res[i] = res[i/2] + 1
10+
}
11+
}
12+
return res
513
}

leetcode/301-400/0338.Counting-Bits/Solution_test.go

Lines changed: 5 additions & 5 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 int
14+
expect []int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase", 2, []int{0,1,1}},
17+
{"TestCase", 5, []int{0,1,1,2,1,2}},
18+
{"TestCase", 10, []int{0,1,1,2,1,2,2,3,1,2,2}},
1919
}
2020

2121
// 开始测试

leetcode/701-800/0791.Custom-Sort-String/README.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
# [791.Custom Sort String][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
74

5+
order and str are strings composed of lowercase letters. In order, no letter occurs more than once.
6+
7+
order was sorted in some custom order previously. We want to permute the characters of str so that they match the order that order was sorted. More specifically, if x occurs before y in order, then x should occur before y in the returned string.
8+
9+
Return any permutation of str (as a string) that satisfies this property.
10+
811
**Example 1:**
912

1013
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
14+
Input:
15+
order = "cba"
16+
str = "abcd"
17+
Output: "cbad"
1318
```
1419

1520
## 题意
Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
func Solution(order string, str string) string {
6+
mapper := make(map[byte]int)
7+
for i:= range order {
8+
mapper[order[i]] = i - 26
9+
}
10+
res := []byte(str)
11+
sort.Slice(res, func(i, j int) bool {
12+
return mapper[res[i]] < mapper[res[j]]
13+
})
14+
return string(res)
515
}

leetcode/701-800/0791.Custom-Sort-String/Solution_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
order string
14+
str string
15+
expect string
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase", "cba", "abcd", "cbad"},
18+
{"TestCase", "bee", "bumblebee", "bbbeeelum"},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.order, c.str)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
27+
c.expect, got, c.order, c.str)
2828
}
2929
})
3030
}

0 commit comments

Comments
 (0)