Skip to content

Commit c53eaf3

Browse files
committed
add file
1 parent 6628d58 commit c53eaf3

File tree

54 files changed

+4105
-31
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+4105
-31
lines changed

README.md

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,12 @@
515515
| | [2068.检查两个字符串是否几乎相等](https://leetcode-cn.com/problems/check-whether-two-strings-are-almost-equivalent/) | 哈希表、字符串、计数 | Easy | 完成 |
516516
| | [2073.买票需要的时间](https://leetcode-cn.com/problems/time-needed-to-buy-tickets/) | 队列、数组、模拟 | Easy | 完成 |
517517
| | [2078.两栋颜色不同且距离最远的房子](https://leetcode-cn.com/problems/two-furthest-houses-with-different-colors/) | 贪心、数组 | Easy | 完成 |
518+
| | [2085.统计出现过一次的公共字符串](https://leetcode-cn.com/problems/count-common-words-with-one-occurrence/) | | Easy | 完成 |
519+
| | [2089.找出数组排序后的目标下标](https://leetcode-cn.com/problems/find-target-indices-after-sorting-array/) | | Easy | 完成 |
520+
| | [2094.找出3位偶数](https://leetcode-cn.com/problems/finding-3-digit-even-numbers/) | 数组、哈希表、枚举、排序 | Easy | 完成 |
521+
| | [2099.找到和最大的长度为K的子序列](https://leetcode-cn.com/problems/find-subsequence-of-length-k-with-the-largest-sum/) | 数组、哈希表、排序、堆(优先队列) | Easy | 完成 |
522+
| | [2103.环和杆](https://leetcode-cn.com/problems/rings-and-rods/) | 哈希表、字符串 | Easy | 完成 |
523+
| | [2108.找出数组中的第一个回文字符串](https://leetcode-cn.com/problems/find-first-palindromic-string-in-the-array/) | | Easy | 完成 |
518524

519525
### Medium系列
520526

@@ -1308,7 +1314,7 @@
13081314
| | [1985.找出数组中的第K大整数](https://leetcode-cn.com/problems/find-the-kth-largest-integer-in-the-array/) | 数组、字符串、分治、快速选择、排序、<br />堆(优先队列) | Medium | 完成 |
13091315
| | [1986.完成任务的最少工作时间段](https://leetcode-cn.com/problems/minimum-number-of-work-sessions-to-finish-the-tasks/) | 位运算、数组、动态规划、回溯、状态压缩 | Medium | 完成 |
13101316
| | [1992.找到所有的农场组](https://leetcode-cn.com/problems/find-all-groups-of-farmland/) | 深度优先搜索、广度优先搜索、数组、矩阵 | Medium | 完成 |
1311-
| | [1993.树上的操作](https://leetcode-cn.com/problems/operations-on-tree/) | 树、广度优先搜索、设计、哈希表 | Medium | 【】 |
1317+
| | [1993.树上的操作](https://leetcode-cn.com/problems/operations-on-tree/) | 树、广度优先搜索、设计、哈希表 | Medium | 完成 |
13121318
| | [1996.游戏中弱角色的数量](https://leetcode-cn.com/problems/the-number-of-weak-characters-in-the-game/) | 栈、贪心、数组、排序、单调栈 | Medium | 完成 |
13131319
| | [1997.访问完所有房间的第一天](https://leetcode-cn.com/problems/first-day-where-you-have-been-in-all-the-rooms/) | 数组、动态规划 | Medium | 完成 |
13141320
| | [2001.可互换矩形的组数](https://leetcode-cn.com/problems/number-of-pairs-of-interchangeable-rectangles/) | 数组、哈希表、数学、计数、数论 | Medium | 完成 |
@@ -1324,7 +1330,7 @@
13241330
| | [2028.找出缺失的观测数据](https://leetcode-cn.com/problems/find-missing-observations/) | 数组、数学、模拟 | Medium | 完成 |
13251331
| | [2029.石子游戏IX](https://leetcode-cn.com/problems/stone-game-ix/) | 贪心、数组、数学、计数、博弈 | Medium | 完成 |
13261332
| | [2033.获取单值网格的最小操作数](https://leetcode-cn.com/problems/minimum-operations-to-make-a-uni-value-grid/) | 数组、数学、矩阵、排序 | Medium | 完成 |
1327-
| | [2034.股票价格波动](https://leetcode-cn.com/problems/stock-price-fluctuation/) | 设计、哈希表、有序集合、堆(优先队列) | Medium | 【】 |
1333+
| | [2034.股票价格波动](https://leetcode-cn.com/problems/stock-price-fluctuation/) | 设计、哈希表、有序集合、堆(优先队列) | Medium | 完成 |
13281334
| | [2038.如果相邻两个颜色均相同则删除当前颜色](https://leetcode-cn.com/problems/remove-colored-pieces-if-both-neighbors-are-the-same-color/) | 贪心、数学、字符串、博弈 | Medium | 完成 |
13291335
| | [2039.网络空闲的时刻](https://leetcode-cn.com/problems/the-time-when-the-network-becomes-idle/) | 广度优先搜索、图、数组 | Medium | 完成 |
13301336
| | [2043.简易银行系统](https://leetcode-cn.com/problems/simple-bank-system/) | 设计、数组、哈希表、模拟 | Medium | 完成 |
@@ -1337,11 +1343,24 @@
13371343
| | [2059.转化数字的最小运算数](https://leetcode-cn.com/problems/minimum-operations-to-convert-number/) | 广度优先搜索、数组 | Medium | 完成 |
13381344
| | [2063.所有子字符串中的元音](https://leetcode-cn.com/problems/vowels-of-all-substrings/) | 数学、字符串、动态规划、组合数学 | Medium | 完成 |
13391345
| | [2064.分配给商店的最多商品的最小值](https://leetcode-cn.com/problems/minimized-maximum-of-products-distributed-to-any-store/) | 数组、二分查找 | Medium | 完成 |
1346+
| | [2069.模拟行走机器人II](https://leetcode-cn.com/problems/walking-robot-simulation-ii/) | 设计、模拟 | Medium | 完成 |
13401347
| | [2070.每一个查询的最大美丽值](https://leetcode-cn.com/problems/most-beautiful-item-for-each-query/) | 数组、二分查找、排序 | Medium | 完成 |
13411348
| | [2074.反转偶数长度组的节点](https://leetcode-cn.com/problems/reverse-nodes-in-even-length-groups/) | 链表 | Medium | 完成 |
13421349
| | [2075.解码斜向换位密码](https://leetcode-cn.com/problems/decode-the-slanted-ciphertext/) | 字符串、模拟 | Medium | 完成 |
13431350
| | [2079.给植物浇水](https://leetcode-cn.com/problems/watering-plants/) | 数组 | Medium | 完成 |
13441351
| | [2080.区间内查询数字的频率](https://leetcode-cn.com/problems/range-frequency-queries/) | 设计、线段树、数组、哈希表、二分查找 | Medium | 完成 |
1352+
| | [2086.从房屋收集雨水需要的最少水桶数](https://leetcode-cn.com/problems/minimum-number-of-buckets-required-to-collect-rainwater-from-houses/) | | Medium | 完成 |
1353+
| | [2087.网格图中机器人回家的最小代价](https://leetcode-cn.com/problems/minimum-cost-homecoming-of-a-robot-in-a-grid/) | | Medium | 完成 |
1354+
| | [2090.半径为k的子数组平均值](https://leetcode-cn.com/problems/k-radius-subarray-averages/) | | Medium | 完成 |
1355+
| | [2091.从数组中移除最大值和最小值](https://leetcode-cn.com/problems/removing-minimum-and-maximum-from-array/) | 贪心、数组 | Medium | 完成 |
1356+
| | [2095.删除链表的中间节点](https://leetcode-cn.com/problems/delete-the-middle-node-of-a-linked-list/) | 链表、双指针 | Medium | 完成 |
1357+
| | [2096.从二叉树一个节点到另一个节点每一步的方向](https://leetcode-cn.com/problems/step-by-step-directions-from-a-binary-tree-node-to-another/) | 树、深度优先搜索、字符串、二叉树 | Medium | 完成 |
1358+
| | [2100.适合打劫银行的日子](https://leetcode-cn.com/problems/find-good-days-to-rob-the-bank/) | 数组、动态规划、前缀和 | Medium | 完成 |
1359+
| | [2101.引爆最多的炸弹](https://leetcode-cn.com/problems/detonate-the-maximum-bombs/) | 哈希表、字符串 | Medium | 完成 |
1360+
| | [2104.子数组范围和](https://leetcode-cn.com/problems/sum-of-subarray-ranges/) | 栈、数组、单调栈 | Medium | 完成 |
1361+
| | [2105.给植物浇水II](https://leetcode-cn.com/problems/watering-plants-ii/) | 数组、双指针、模拟 | Medium | 完成 |
1362+
| | [2109.向字符串添加空格](https://leetcode-cn.com/problems/adding-spaces-to-a-string/) | | Medium | 完成 |
1363+
| | [2110.股票平滑下跌阶段的数目](https://leetcode-cn.com/problems/number-of-smooth-descent-periods-of-a-stock/) | | Medium | 完成 |
13451364

13461365
### Hard系列
13471366

@@ -1553,4 +1572,8 @@
15531572
| | [2050.并行课程III](https://leetcode-cn.com/problems/parallel-courses-iii/) | 图、拓扑排序、动态规划 | Hard | 完成 |
15541573
| | [2065.最大化一张图中的路径价值](https://leetcode-cn.com/problems/maximum-path-quality-of-a-graph/) | 图、数组、回溯 | Hard | 完成 |
15551574
| | [2076.处理含限制条件的好友请求](https://leetcode-cn.com/problems/process-restricted-friend-requests/) | 并查集、图 | Hard | 完成 |
1575+
| | [2092.找出知晓秘密的所有专家](https://leetcode-cn.com/problems/find-all-people-with-secret/) | 深度优先搜索、广度优先搜索、<br />并查集、图、排序 | Hard | 【】 |
1576+
| | [2097.合法重新排列数对](https://leetcode-cn.com/problems/valid-arrangement-of-pairs/) | 深度优先搜索、图、欧拉回路 | Hard | 完成 |
1577+
| | [2106.摘水果](https://leetcode-cn.com/problems/maximum-fruits-harvested-after-at-most-k-steps/) | 数组、二分查找、前缀和、滑动窗口 | Hard | 完成 |
1578+
| | [2111.使数组K递增的最少操作次数](https://leetcode-cn.com/problems/minimum-operations-to-make-the-array-k-increasing/) | | Hard | 完成 |
15561579

docs/source/algorithm/二分查找.md

Lines changed: 106 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# template
22

3-
- 参考:
3+
- 参考:https://books.studygolang.com/The-Golang-Standard-Library-by-Example/chapter03/03.1.html
44

55
## 0、定义
66

@@ -106,10 +106,114 @@ func upperBound(arr []int, target int) int {
106106

107107
### 二分查找-
108108

109-
### 内置函数
109+
#### 内置函数-sort.Search
110+
111+
- 查找使 f(x)(0<=x<n) 返回 ture 的最小值 i
112+
113+
```go
114+
package main
115+
116+
import (
117+
"fmt"
118+
"sort"
119+
)
120+
121+
func main() {
122+
var index int
123+
arr := []int{1, 1, 10, 10, 50, 51, 60, 90, 100, 100, 100, 101}
124+
index = sort.Search(len(arr), func(i int) bool {
125+
return arr[i] >= 100 // 8
126+
})
127+
fmt.Println(index)
128+
index = sort.Search(len(arr), func(i int) bool {
129+
return arr[i] >= 101 // 11
130+
})
131+
fmt.Println(index)
132+
index = sort.Search(len(arr), func(i int) bool {
133+
return arr[i] >= 1 // 0
134+
})
135+
fmt.Println(index)
136+
index = sort.Search(len(arr), func(i int) bool {
137+
return arr[i] >= 0 // 0
138+
})
139+
fmt.Println(index)
140+
fmt.Println(index)
141+
index = sort.Search(len(arr), func(i int) bool {
142+
return arr[i] >= -1 // 0
143+
})
144+
fmt.Println(index)
145+
index = sort.Search(len(arr), func(i int) bool {
146+
return arr[i] >= 95 // 8
147+
})
148+
if index < len(arr) && arr[index] == 95 {
149+
fmt.Println("95在数组中:\t", arr)
150+
} else {
151+
fmt.Println("95不在数组中") // 输出
152+
}
153+
}
154+
```
155+
156+
- 底层实现
157+
158+
```go
159+
func Search(n int, f func(int) bool) int {
160+
// Define f(-1) == false and f(n) == true.
161+
// Invariant: f(i-1) == false, f(j) == true.
162+
i, j := 0, n
163+
for i < j {
164+
h := int(uint(i+j) >> 1) // avoid overflow when computing h
165+
// i ≤ h < j
166+
if !f(h) {
167+
i = h + 1 // preserves f(i-1) == false
168+
} else {
169+
j = h // preserves f(j) == true
170+
}
171+
}
172+
// i == j, f(i-1) == false, and f(j) (= f(i)) == true => answer is i.
173+
return i
174+
}
175+
```
176+
177+
#### 内置函数-sort.SearchInt
178+
179+
-
110180

111181
```go
182+
package main
183+
184+
import (
185+
"fmt"
186+
"sort"
187+
)
188+
189+
func main() {
190+
var index int
191+
arr := []int{1, 1, 10, 10, 50, 51, 60, 90, 100, 100, 100, 101}
192+
index = sort.SearchInts(arr, 100) // 8
193+
fmt.Println(index)
194+
index = sort.SearchInts(arr, 101) // 11
195+
fmt.Println(index)
196+
index = sort.SearchInts(arr, 1) // 0
197+
fmt.Println(index)
198+
index = sort.SearchInts(arr, 0) // 0
199+
fmt.Println(index)
200+
index = sort.SearchInts(arr, -1) //0
201+
fmt.Println(index)
202+
index = sort.SearchInts(arr, 95)
203+
if index < len(arr) && arr[index] == 95 {
204+
fmt.Println("95在数组中:\t", arr)
205+
} else {
206+
fmt.Println("95不在数组中") // 输出
207+
}
208+
}
209+
```
210+
211+
- 底层实现
112212

213+
```go
214+
func SearchInts(a []int, x int) int {
215+
return Search(len(a), func(i int) bool { return a[i] >= x })
216+
}
113217
```
114218

115219
## 3、Leetcode

docs/source/algorithm/动态规划-最长上升子序列.md

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,53 @@
11
# template
22

3-
- 参考:
3+
- 参考:https://leetcode-cn.com/problems/minimum-operations-to-make-the-array-k-increasing/solution/zui-chang-fei-jiang-zi-xu-lie-by-endless-x54c/
44

55
## 0、定义
66

77
## 1、操作
88

9+
- 求最长严格递增子序列需要二分找到大于或等于当前元素的元素位置(即 C++ 中的 lower_bound);
10+
11+
- 求最长不降子序列需要二分找到大于当前元素的元素位置(即 C++ 中的 upper_bound)。
12+
13+
14+
915
## 2、Go实现
1016

1117
```go
18+
// 最长上升子序列(不严格递增)+ 二分查找
19+
func lengthOfLIS(nums []int) int {
20+
if len(nums) < 2 {
21+
return len(nums)
22+
}
23+
arr := make([]int, 0)
24+
arr = append(arr, nums[0])
25+
for i := 1; i < len(nums); i++ {
26+
index := upperBound(arr, nums[i])
27+
if index == len(arr) {
28+
arr = append(arr, nums[i])
29+
} else {
30+
arr[index] = nums[i]
31+
}
32+
}
33+
return len(arr)
34+
}
1235

36+
// 返回第一个大于等于target的位置
37+
func upperBound(arr []int, target int) int {
38+
left, right := 0, len(arr)
39+
for left < right {
40+
mid := left + (right-left)/2
41+
if arr[mid] == target {
42+
left = mid + 1 // 收缩左边界
43+
} else if arr[mid] < target {
44+
left = mid + 1
45+
} else {
46+
right = mid
47+
}
48+
}
49+
return left
50+
}
1351
```
1452

1553
## 3、Leetcode
Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,32 @@
11
# 图-欧拉回路
22

3+
- 参考:https://leetcode-cn.com/problems/valid-arrangement-of-pairs/solution/he-fa-zhong-xin-pai-lie-shu-dui-by-leetc-h8rl/
4+
- https://leetcode-cn.com/problems/valid-arrangement-of-pairs/solution/ou-la-tu-zui-qiang-gong-lue-gai-nian-mo-8t2d6/
5+
36
## 0、定义
47

8+
- 如果图中所有节点的入度和出度都相等,那么从**任意节点**开始都存在欧拉通路;
9+
- 起点:如果图中存在一个节点的出度比入度恰好多 1;
10+
- 终点:另一个节点的入度恰好比出度多 1,那么欧拉通路必须从前一个节点开始,到后一个节点结束。
11+
12+
### Hierholzer 算法
13+
14+
- 从起点出发,进行深度优先搜索。
15+
16+
- 每次沿着某条边从某个顶点移动到另外一个顶点的时候,都需要删除这条边(灵魂)。
17+
18+
- 如果没有可移动的路径,则将所在节点加入到结果中,并返回。
19+
20+
21+
522
## 1、操作
623

724
## 2、Go实现
825

926
## 3、Leetcode
1027

11-
| Title | Tag | 难度 | 完成情况 |
12-
| ----- | ---- | ---- | -------- |
13-
| | | | |
28+
| Title | Tag | 难度 | 完成情况 |
29+
| ------------------------------------------------------------ | -------------------------- | ------ | -------- |
30+
| [332.重新安排行程](https://leetcode-cn.com/problems/reconstruct-itinerary/) | 深度优先搜索、图 | Medium | 完成 |
31+
| 753 | | | |
32+
| [2097.合法重新排列数对](https://leetcode-cn.com/problems/valid-arrangement-of-pairs/) | 深度优先搜索、图、欧拉回路 | Hard | 完成 |

docs/source/algorithm/排序算法-堆.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,63 @@ func (h *IntHeap) Pop() interface{} {
170170
}
171171
```
172172

173+
- 混合堆
174+
175+
```go
176+
package main
177+
178+
import "container/heap"
179+
180+
func main() {
181+
182+
}
183+
184+
type mixHeap struct {
185+
arr [][]int
186+
isBig bool
187+
}
188+
189+
func (m *mixHeap) Len() int {
190+
return len(m.arr)
191+
}
192+
193+
func (m *mixHeap) Swap(i, j int) {
194+
m.arr[i], m.arr[j] = m.arr[j], m.arr[i]
195+
}
196+
197+
func (m *mixHeap) Less(i, j int) bool {
198+
if m.isBig {
199+
return m.arr[i][0] > m.arr[j][0] // 大根堆
200+
}
201+
return m.arr[i][0] < m.arr[j][0] // 小根堆
202+
}
203+
204+
func (m *mixHeap) Push(x interface{}) {
205+
m.arr = append(m.arr, x.([]int))
206+
}
207+
208+
func (m *mixHeap) Pop() interface{} {
209+
value := (m.arr)[len(m.arr)-1]
210+
m.arr = (m.arr)[:len(m.arr)-1]
211+
return value
212+
}
213+
214+
func (m *mixHeap) push(x []int) {
215+
heap.Push(m, x)
216+
}
217+
218+
func (m *mixHeap) pop() []int {
219+
return heap.Pop(m).([]int)
220+
}
221+
222+
func (m *mixHeap) Top() []int {
223+
if m.Len() > 0 {
224+
return m.arr[0]
225+
}
226+
return nil
227+
}
228+
```
229+
173230
## 3、Leetcode
174231

175232
| Title | Tag | 难度 | 完成情况 |

0 commit comments

Comments
 (0)