Skip to content

Commit aa4900d

Browse files
committed
Merge branch 'develop' of https://github.com/aQuaYi/LeetCode-in-Go into develop
2 parents 6bc109f + 041c09d commit aa4900d

File tree

12 files changed

+754
-233
lines changed

12 files changed

+754
-233
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# [895. Maximum Frequency Stack](https://leetcode.com/problems/maximum-frequency-stack/)
2+
3+
## 题目
4+
5+
Implement FreqStack, a class which simulates the operation of a stack-like data structure.
6+
7+
FreqStack`has two functions:
8+
9+
- push(int x), which pushes an integer x onto the stack.
10+
- pop(), which removes and returns the most frequent element in the stack.
11+
- If there is a tie for most frequent element, the element closest to the top of the stack is removed and returned.
12+
13+
Example 1:
14+
15+
```text
16+
Input:
17+
["FreqStack","push","push","push","push","push","push","pop","pop","pop","pop"],
18+
[[],[5],[7],[5],[7],[4],[5],[],[],[],[]]
19+
Output: [null,null,null,null,null,null,null,5,7,5,4]
20+
Explanation:
21+
After making six .push operations, the stack is [5,7,5,7,4,5] from bottom to top. Then:
22+
23+
pop() -> returns 5, as 5 is the most frequent.
24+
The stack becomes [5,7,5,7,4].
25+
26+
pop() -> returns 7, as 5 and 7 is the most frequent, but 7 is closest to the top.
27+
The stack becomes [5,7,5,4].
28+
29+
pop() -> returns 5.
30+
The stack becomes [5,7,4].
31+
32+
pop() -> returns 4.
33+
The stack becomes [5,7].
34+
```
35+
36+
Note:
37+
38+
- Calls to FreqStack.push(int x)`will be such that 0 <= x <= 10^9.
39+
- It is guaranteed that FreqStack.pop() won't be called if the stack has zero elements.
40+
- The total number of FreqStack.push calls will not exceed 10000 in a single test case.
41+
- The total number of FreqStack.pop`calls will not exceed 10000 in a single test case.
42+
- The total number of FreqStack.push and FreqStack.pop calls will not exceed 150000 across all test cases.
43+
44+
## 解题思路
45+
46+
见程序注释
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package problem0895
2+
3+
import (
4+
"container/heap"
5+
)
6+
7+
// FreqStack object will be instantiated and called as such:
8+
// obj := Constructor();
9+
// obj.Push(x);
10+
// param_2 := obj.Pop();
11+
type FreqStack struct {
12+
index int
13+
freq map[int]int
14+
pq *PQ
15+
}
16+
17+
// Constructor 构建 FreqStack
18+
func Constructor() FreqStack {
19+
pq := make(PQ, 0, 10000)
20+
return FreqStack{
21+
freq: make(map[int]int, 10000),
22+
pq: &pq,
23+
}
24+
}
25+
26+
// Push 在 fs 中放入 x
27+
func (fs *FreqStack) Push(x int) {
28+
fs.index++
29+
fs.freq[x]++
30+
e := &entry{
31+
key: x,
32+
index: fs.index,
33+
freq: fs.freq[x],
34+
}
35+
heap.Push(fs.pq, e)
36+
}
37+
38+
// Pop 从 fs 中弹出元素
39+
func (fs *FreqStack) Pop() int {
40+
x := heap.Pop(fs.pq).(*entry).key
41+
fs.freq[x]--
42+
return x
43+
}
44+
45+
// entry 是 priorityQueue 中的元素
46+
type entry struct {
47+
key, index, freq int
48+
}
49+
50+
// PQ implements heap.Interface and holds entries.
51+
type PQ []*entry
52+
53+
func (pq PQ) Len() int { return len(pq) }
54+
55+
func (pq PQ) Less(i, j int) bool {
56+
if pq[i].freq == pq[j].freq {
57+
return pq[i].index > pq[j].index
58+
}
59+
return pq[i].freq > pq[j].freq
60+
}
61+
62+
func (pq PQ) Swap(i, j int) {
63+
pq[i], pq[j] = pq[j], pq[i]
64+
}
65+
66+
// Push 往 pq 中放 entry
67+
func (pq *PQ) Push(x interface{}) {
68+
temp := x.(*entry)
69+
*pq = append(*pq, temp)
70+
}
71+
72+
// Pop 从 pq 中取出最优先的 entry
73+
func (pq *PQ) Pop() interface{} {
74+
temp := (*pq)[len(*pq)-1]
75+
*pq = (*pq)[0 : len(*pq)-1]
76+
return temp
77+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package problem0895
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func Test_FreqStack_1(t *testing.T) {
10+
ast := assert.New(t)
11+
fs := Constructor()
12+
pushes := []int{5, 7, 5, 7, 4, 5}
13+
for _, p := range pushes {
14+
fs.Push(p)
15+
}
16+
expecteds := []int{5, 7, 5, 4}
17+
for _, expected := range expecteds {
18+
actual := fs.Pop()
19+
ast.Equal(expected, actual)
20+
}
21+
}
22+
23+
func Test_FreqStack_2(t *testing.T) {
24+
ast := assert.New(t)
25+
fs := Constructor()
26+
//
27+
pushes := []int{1, 1, 1, 2}
28+
for _, p := range pushes {
29+
fs.Push(p)
30+
}
31+
expecteds := []int{1, 1}
32+
for _, expected := range expecteds {
33+
actual := fs.Pop()
34+
ast.Equal(expected, actual)
35+
}
36+
//
37+
pushes = []int{2, 2, 1}
38+
for _, p := range pushes {
39+
fs.Push(p)
40+
}
41+
expecteds = []int{2, 1, 2}
42+
for _, expected := range expecteds {
43+
actual := fs.Pop()
44+
ast.Equal(expected, actual)
45+
}
46+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# [897. Increasing Order Search Tree](https://leetcode.com/problems/increasing-order-search-tree/)
2+
3+
## 题目
4+
5+
Given a tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root of the tree, and every node has no left child and only 1 right child.
6+
7+
```text
8+
Example 1:
9+
Input: [5,3,6,2,4,null,8,1,null,null,null,7,9]
10+
11+
5
12+
/ \
13+
3 6
14+
/ \ \
15+
2 4 8
16+
/ / \
17+
1 7 9
18+
19+
Output: [1,null,2,null,3,null,4,null,5,null,6,null,7,null,8,null,9]
20+
21+
1
22+
\
23+
2
24+
\
25+
3
26+
\
27+
4
28+
\
29+
5
30+
\
31+
6
32+
\
33+
7
34+
\
35+
8
36+
\
37+
9
38+
```
39+
40+
Note:
41+
42+
- The number of nodes in the given tree will be between 1 and 100.
43+
- Each node will have a unique integer value from 0 to 1000.
44+
45+
## 解题思路
46+
47+
见程序注释
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package problem0897
2+
3+
import "github.com/aQuaYi/LeetCode-in-Go/kit"
4+
5+
// TreeNode Definition for a binary tree node.
6+
// type TreeNode struct {
7+
// Val int
8+
// Left *TreeNode
9+
// Right *TreeNode
10+
// }
11+
type TreeNode = kit.TreeNode
12+
13+
func increasingBST(root *TreeNode) *TreeNode {
14+
var head = &TreeNode{}
15+
tail := head
16+
var rec func(root *TreeNode)
17+
rec = func(root *TreeNode) {
18+
if root == nil {
19+
return
20+
}
21+
rec(root.Left)
22+
root.Left = nil // 切断 root 与其 Left 的连接,避免形成环
23+
tail.Right, tail = root, root // 把 root 接上 tail,并保持 tail 指向尾部
24+
rec(root.Right)
25+
}
26+
rec(root)
27+
return head.Right
28+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package problem0897
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/aQuaYi/LeetCode-in-Go/kit"
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
var null = kit.NULL
12+
13+
// tcs is testcase slice
14+
var tcs = []struct {
15+
p []int
16+
ans []int
17+
}{
18+
19+
{
20+
[]int{5, 3, 6, 2, 4, null, 8, 1, null, null, null, 7, 9},
21+
[]int{1, null, 2, null, 3, null, 4, null, 5, null, 6, null, 7, null, 8, null, 9},
22+
},
23+
24+
// 可以有多个 testcase
25+
}
26+
27+
func Test_increasingBST(t *testing.T) {
28+
ast := assert.New(t)
29+
30+
for _, tc := range tcs {
31+
fmt.Printf("~~%v~~\n", tc)
32+
root := kit.Ints2TreeNode(tc.p)
33+
ans := kit.Tree2ints(increasingBST(root))
34+
ast.Equal(tc.ans, ans, "输入:%v", tc)
35+
}
36+
}
37+
38+
func Benchmark_myFunc(b *testing.B) {
39+
for i := 0; i < b.N; i++ {
40+
for _, tc := range tcs {
41+
root := kit.Ints2TreeNode(tc.p)
42+
increasingBST(root)
43+
}
44+
}
45+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# [905. Sort Array By Parity](https://leetcode.com/problems/sort-array-by-parity/)
2+
3+
## 题目
4+
5+
Given an array `A` of non-negative integers, return an array consisting of all the even elements of `A`, followed by all the odd elements of `A`.
6+
7+
You may return any answer array that satisfies this condition.
8+
9+
Example 1:
10+
11+
```text
12+
Input: [3,1,2,4]
13+
Output: [2,4,3,1]
14+
The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted.
15+
```
16+
17+
Note:
18+
19+
- 1 <= A.length <= 5000
20+
- 0 <= A[i] <= 5000
21+
22+
## 解题思路
23+
24+
见程序注释
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package problem0905
2+
3+
func sortArrayByParity(a []int) []int {
4+
i, j := 0, len(a)-1
5+
for {
6+
for i < j && a[i]%2 == 0 {
7+
i++
8+
}
9+
for i < j && a[j]%2 == 1 {
10+
j--
11+
}
12+
13+
if i < j {
14+
a[i], a[j] = a[j], a[i]
15+
} else {
16+
break
17+
}
18+
19+
}
20+
return a
21+
}

0 commit comments

Comments
 (0)