Skip to content

Commit d95f16b

Browse files
committedAug 23, 2021
Merge pull request #210 from 0xff-dev/master
Add solution and test-cases for problem 729
2 parents 8edc870 + b8215a2 commit d95f16b

File tree

8 files changed

+281
-54
lines changed

8 files changed

+281
-54
lines changed
 
Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,81 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
const (
4+
push = "push"
5+
pop = "pop"
6+
top = "top"
7+
getMin = "getMIn"
8+
)
9+
10+
type MinStack struct {
11+
data, min []int
12+
}
13+
14+
func Constructor4() MinStack {
15+
return MinStack{
16+
data: make([]int, 0),
17+
min: make([]int, 0),
18+
}
19+
}
20+
21+
func (this *MinStack) Push(val int) {
22+
if len(this.data) == 0 {
23+
this.data = append(this.data, val)
24+
this.min = append(this.min, val)
25+
return
26+
}
27+
28+
top := this.min[len(this.min)-1]
29+
this.data = append(this.data, val)
30+
if top > val {
31+
top = val
32+
}
33+
34+
this.min = append(this.min, top)
35+
}
36+
37+
func (this *MinStack) Pop() {
38+
if len(this.data) > 0 {
39+
l := len(this.data)
40+
this.data = this.data[:l-1]
41+
this.min = this.min[:l-1]
42+
}
43+
}
44+
45+
func (this *MinStack) Top() int {
46+
if len(this.data) > 0 {
47+
return this.data[len(this.data)-1]
48+
}
49+
50+
return -1
51+
}
52+
53+
func (this *MinStack) GetMin() int {
54+
return this.min[len(this.min)-1]
55+
}
56+
57+
// action, push, pop, top, getMin
58+
func Solution(actions []string, vals []int) []int {
59+
o := Constructor4()
60+
expect := make([]int, 0)
61+
for idx, act := range actions {
62+
if act == push {
63+
o.Push(vals[idx])
64+
continue
65+
}
66+
if act == pop {
67+
o.Pop()
68+
continue
69+
}
70+
71+
if act == top {
72+
top := o.Top()
73+
expect = append(expect, top)
74+
continue
75+
}
76+
77+
m := o.GetMin()
78+
expect = append(expect, m)
79+
}
80+
return expect
581
}

‎leetcode/101-200/0155.Min-Stack/Solution_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
actions []string
14+
inputs []int
15+
expect []int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []string{push, push, push, getMin, pop, top, getMin}, []int{-2, 0, -3, 0, 0, 0, 0}, []int{-3, 0, -2}},
18+
{"TestCase2", []string{push, push, getMin, top, pop, push, getMin, top}, []int{1, 2, 0, 0, 0, -2, 0, 0}, []int{1, 2, -2, -2}},
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.actions, c.inputs)
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 input-actions: %v, input-inputs: %v",
27+
c.expect, got, c.actions, c.inputs)
2828
}
2929
})
3030
}

‎leetcode/301-400/0341.Flatten-Nested-List-Iterator/README.md

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,36 @@
11
# [341.Flatten Nested List Iterator][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
4+
You are given a nested list of integers `nestedList`. Each element is either an integer or a list whose elements may also be integers or other lists. Implement an iterator to flatten it.
75

8-
**Example 1:**
6+
Implement the `NestedIterator` class:
97

8+
- `NestedIterator(List<NestedInteger> nestedList)` Initializes the iterator with the nested list `nestedList`.
9+
- `int next()` Returns the next integer in the nested list.
10+
- `boolean hasNext()` Returns `true` if there are still some integers in the nested list and `false` otherwise.
11+
Your code will be tested with the following pseudocode:
1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
initialize iterator with nestedList
14+
res = []
15+
while iterator.hasNext()
16+
append iterator.next() to the end of res
17+
return res
1318
```
19+
If `res` matches the expected flattened list, then your code will be judged as correct.
1420

15-
## 题意
16-
> ...
21+
**Example 1:**
1722

18-
## 题解
23+
```
24+
Input: nestedList = [[1,1],2,[1,1]]
25+
Output: [1,1,2,1,1]
26+
Explanation: By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,1,2,1,1].
27+
```
1928

20-
### 思路1
21-
> ...
22-
Flatten Nested List Iterator
23-
```go
29+
__Example 2:__
30+
```
31+
Input: nestedList = [1,[4,[6]]]
32+
Output: [1,4,6]
33+
Explanation: By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,4,6].
2434
```
2535

2636

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,69 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
type NestedInteger struct {
4+
Val int
5+
isInt bool
6+
list []*NestedInteger
7+
}
8+
9+
func (this NestedInteger) IsInteger() bool {
10+
return this.isInt
11+
}
12+
13+
func (this NestedInteger) GetInteger() int {
14+
return this.Val
15+
}
16+
17+
func (this NestedInteger) SetInteger(value int) {
18+
19+
}
20+
func (this NestedInteger) Add(ele NestedInteger) {
21+
22+
}
23+
24+
func (this NestedInteger) GetList() []*NestedInteger {
25+
return this.list
26+
}
27+
28+
type NestedIterator struct {
29+
Raw []*NestedInteger
30+
idx int
31+
}
32+
33+
func Constructor3(nestedList []*NestedInteger) *NestedIterator {
34+
raw := make([]*NestedInteger, 0)
35+
deepTraverse(nestedList, &raw)
36+
return &NestedIterator{
37+
Raw: raw,
38+
idx: 0,
39+
}
40+
}
41+
42+
func deepTraverse(nestedList []*NestedInteger, raw *[]*NestedInteger) {
43+
for idx, ele := range nestedList {
44+
if ele.IsInteger() {
45+
*raw = append(*raw, nestedList[idx])
46+
continue
47+
}
48+
deepTraverse(ele.GetList(), raw)
49+
}
50+
}
51+
52+
func (this *NestedIterator) Next() int {
53+
r := this.Raw[this.idx].GetInteger()
54+
this.idx++
55+
return r
56+
}
57+
58+
func (this *NestedIterator) HasNext() bool {
59+
return this.idx < len(this.Raw)
60+
}
61+
62+
func Solution(nestedList []*NestedInteger) []int {
63+
o := Constructor3(nestedList)
64+
expect := make([]int, 0)
65+
for o.HasNext() {
66+
expect = append(expect, o.Next())
67+
}
68+
return expect
569
}

‎leetcode/301-400/0341.Flatten-Nested-List-Iterator/Solution_test.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,22 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []*NestedInteger
14+
expect []int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []*NestedInteger{{Val: 1, isInt: true}, {isInt: false, list: []*NestedInteger{{Val: 2, isInt: true}, {Val: 3, isInt: true}}}, {Val: 1, isInt: true, list: nil}}, []int{1, 2, 3, 1}},
17+
{"TestCase2", []*NestedInteger{{Val: 1, isInt: true}, {Val: 2, isInt: true}, {Val: 3, isInt: true}}, []int{1, 2, 3}},
18+
{"TestCase3", []*NestedInteger{
19+
{Val: 1, isInt: true},
20+
{isInt: false, list: []*NestedInteger{
21+
{isInt: false, list: []*NestedInteger{{Val: 2, isInt: true}}},
22+
{isInt: false, list: []*NestedInteger{
23+
{Val: 3, isInt: true},
24+
{Val: 4, isInt: true},
25+
}},
26+
}},
27+
{Val: 5, isInt: true}},
28+
[]int{1, 2, 3, 4, 5}},
1929
}
2030

2131
// 开始测试

‎leetcode/701-800/0729.My-Calendar-I/README.md

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
11
# [729.My Calendar I][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)
53

64
## Description
5+
You are implementing a program to use as your calendar. We can add a new event if adding the event will not cause a __double booking__.
76

8-
**Example 1:**
7+
A __double booking__ happens when two events have some non-empty intersection (i.e., some moment is common to both events.).
98

10-
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
9+
The event can be represented as a pair of integers `start` and `end` that represents a booking on the half-open interval `[start, end)`, the range of real numbers `x` such that `start <= x < end`.
1410

15-
## 题意
16-
> ...
11+
Implement the `MyCalendar` class:
1712

18-
## 题解
13+
- `MyCalendar()` Initializes the calendar object.
14+
- `boolean book(int start, int end)` Returns `true` if the event can be added to the calendar successfully without causing a __double booking__. Otherwise, return `false` and do not add the event to the calendar.
1915

20-
### 思路1
21-
> ...
22-
My Calendar I
23-
```go
16+
**Example 1:**
17+
18+
```
19+
Input
20+
["MyCalendar", "book", "book", "book"]
21+
[[], [10, 20], [15, 25], [20, 30]]
22+
Output
23+
[null, true, false, true]
24+
25+
Explanation
26+
MyCalendar myCalendar = new MyCalendar();
27+
myCalendar.book(10, 20); // return True
28+
myCalendar.book(15, 25); // return False, It can not be booked because time 15 is already booked by another event.
29+
myCalendar.book(20, 30); // return True, The event can be booked, as the first event takes every time less than 20, but not including 20.
2430
```
2531

2632

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,65 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
type seg struct {
4+
start, end int
5+
}
6+
7+
type MyCalendar struct {
8+
books []seg
9+
}
10+
11+
func Constructor() MyCalendar {
12+
return MyCalendar{
13+
books: make([]seg, 0),
14+
}
15+
}
16+
17+
func (this *MyCalendar) Book(start, end int) bool {
18+
s := seg{start, end}
19+
if len(this.books) == 0 {
20+
this.books = append(this.books, s)
21+
return true
22+
}
23+
24+
idx := len(this.books) - 1
25+
breakPoint := false
26+
for ; idx >= 0; idx-- {
27+
if s.end >= this.books[idx].end && s.start >= this.books[idx].end {
28+
breakPoint = true
29+
break
30+
}
31+
if (s.start >= this.books[idx].start && s.start < this.books[idx].end) || (s.end > this.books[idx].start && s.end < this.books[idx].end) || (s.end >= this.books[idx].end && s.start <= this.books[idx].start) {
32+
return false
33+
}
34+
if idx == 0 {
35+
breakPoint = true
36+
}
37+
}
38+
if breakPoint {
39+
if idx < 0 {
40+
this.books = append([]seg{s}, this.books...)
41+
} else {
42+
tmp := make([]seg, 0)
43+
tmp = append(tmp, this.books[:idx+1]...)
44+
tmp = append(tmp, s)
45+
tmp = append(tmp, this.books[idx+1:]...)
46+
this.books = tmp
47+
}
48+
}
49+
50+
return breakPoint
51+
}
52+
53+
func Solution(segs []seg, res []bool) bool {
54+
if len(segs) != len(res) {
55+
return false
56+
}
57+
c := Constructor()
58+
59+
for idx := 0; idx < len(segs); idx++ {
60+
if res[idx] != c.Book(segs[idx].start, segs[idx].end) {
61+
return false
62+
}
63+
}
64+
return true
565
}

‎leetcode/701-800/0729.My-Calendar-I/Solution_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,22 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
13+
segs []seg
14+
res []bool
1415
expect bool
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []seg{{2, 6}, {12, 15}, {8, 9}, {9, 10}}, []bool{true, true, true, true}, true},
18+
{"TestCase2", []seg{{10, 20}, {15, 25}, {20, 30}}, []bool{true, false, true}, true},
19+
{"TestCase3", []seg{{37, 50}, {33, 50}, {4, 17}, {35, 48}, {8, 25}}, []bool{true, false, true, false, false}, true},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.segs, c.res)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with input-segs: %v, input-res: %v",
28+
c.expect, got, c.segs, c.res)
2829
}
2930
})
3031
}

0 commit comments

Comments
 (0)
Please sign in to comment.