@@ -20,6 +20,9 @@ import (
20
20
小奥
21
21
https://codeforces.com/problemset/problem/700/A
22
22
23
+ 原地哈希
24
+ LC442 https://leetcode.cn/problems/find-all-duplicates-in-an-array/
25
+
23
26
三维 n 皇后 https://oeis.org/A068940
24
27
Maximal number of chess queens that can be placed on a 3-dimensional chessboard of order n so that no two queens attack each other
25
28
@@ -899,83 +902,6 @@ func champernowneConstant(k int) int {
899
902
}
900
903
}
901
904
902
- // 解析时间 力扣见过多次了
903
- func parseTime (s string ) (hour , minute , total int ) {
904
- hour = int (s [0 ]& 15 )* 10 + int (s [1 ]& 15 )
905
- minute = int (s [3 ]& 15 )* 10 + int (s [4 ]& 15 )
906
- total = hour * 60 + minute
907
- return
908
- }
909
-
910
- // 合并 a 中所有重叠的闭区间(哪怕只有一个端点重叠,也算重叠)
911
- // 注意 [1,1] 和 [2,2] 不能合并成 [1,2]
912
- // 注:这种做法在变形题中容易写错,更加稳定的做法是差分数组
913
- // - [56. 合并区间](https://leetcode.cn/problems/merge-intervals/)
914
- // - [55. 跳跃游戏](https://leetcode.cn/problems/jump-game/)
915
- // - [2580. 统计将重叠区间合并成组的方案数](https://leetcode.cn/problems/count-ways-to-group-overlapping-ranges/) 1632
916
- // - [2963. 统计好分割方案的数目](https://leetcode.cn/problems/count-the-number-of-good-partitions/) 1985
917
- // - [2584. 分割数组使乘积互质](https://leetcode.cn/problems/split-the-array-to-make-coprime-products/) 2159
918
- // - [2655. 寻找最大长度的未覆盖区间](https://leetcode.cn/problems/find-maximal-uncovered-ranges/)(会员题)
919
- // 另见 common.go 中的「区间贪心」
920
- // https://codeforces.com/problemset/problem/1626/C 1700
921
- // - 倒序合并代码 https://codeforces.com/contest/1626/submission/211306494
922
- // https://codeforces.com/problemset/problem/1859/D 1800
923
- // https://codeforces.com/problemset/problem/1260/D 1900
924
- func mergeIntervals (a [][]int ) [][]int {
925
- slices .SortFunc (a , func (a , b []int ) int { return a [0 ] - b [0 ] }) // 按区间左端点排序
926
- merged := [][]int {}
927
- l0 := a [0 ][0 ]
928
- maxR := a [0 ][1 ]
929
- for _ , p := range a [1 :] { // 从第二个区间开始
930
- l , r := p [0 ], p [1 ]
931
- // 如果要合并 [1,1] 和 [2,2],下面改成 if l-1 > maxR
932
- if l > maxR { // 发现一个新区间
933
- merged = append (merged , []int {l0 , maxR }) // 先把旧的加入答案
934
- l0 = l // 记录新区间左端点
935
- }
936
- maxR = max (maxR , r )
937
- }
938
- merged = append (merged , []int {l0 , maxR }) // 最后发现的新区间加入答案
939
-
940
- {
941
- // 包含 x 的闭区间
942
- var x int
943
- i := sort .Search (len (merged ), func (i int ) bool { return merged [i ][1 ] >= x })
944
- if i < len (merged ) && merged [i ][0 ] <= x {
945
- // ans[i]...
946
- }
947
- }
948
-
949
- return merged
950
- }
951
-
952
- // 从 i 可以跳到 [i,i+a[i]] 中的任意整点
953
- // 返回从 0 跳到 n-1 的最小跳跃次数
954
- // 如果无法到达 n-1,返回 -1
955
- // 注:对于复杂变形题,采用分组循环不易写错
956
- // - [45. 跳跃游戏 II](https://leetcode.cn/problems/jump-game-ii/)
957
- // - [1024. 视频拼接](https://leetcode.cn/problems/video-stitching/) 1746
958
- // - [1326. 灌溉花园的最少水龙头数目](https://leetcode.cn/problems/minimum-number-of-taps-to-open-to-water-a-garden/) 1885
959
- // 【图解】https://leetcode.cn/problems/minimum-number-of-taps-to-open-to-water-a-garden/solution/yi-zhang-tu-miao-dong-pythonjavacgo-by-e-wqry/
960
- // 变形 https://codeforces.com/contest/1630/problem/C
961
- func minJumpNumbers (a []int ) (ans int ) {
962
- curR := 0 // 已建造的桥的右端点
963
- nxtR := 0 // 下一座桥的右端点的最大值
964
- // 这里没有遍历到 n-1,因为它已经是终点了
965
- for i , d := range a [:len (a )- 1 ] {
966
- r := i + d
967
- nxtR = max (nxtR , r )
968
- if i == curR { // 到达已建造的桥的右端点
969
- if i == nxtR { // 无论怎么造桥,都无法从 i 到 i+1
970
- return - 1
971
- }
972
- curR = nxtR // 建造下一座桥
973
- ans ++
974
- }
975
- }
976
- return
977
- }
978
-
979
905
// 摩尔投票法求绝对众数(absolute mode, majority)
980
906
// Boyer–Moore majority vote algorithm
981
907
// https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_majority_vote_algorithm
@@ -1223,3 +1149,88 @@ func makeLexicographicallySmallestStringBySwappingAdjacentElements2(a []int) []i
1223
1149
}
1224
1150
return ans
1225
1151
}
1152
+
1153
+ // 解析时分
1154
+ // - [1736. 替换隐藏数字得到的最晚时间](https://leetcode.cn/problems/latest-time-by-replacing-hidden-digits/) 1264
1155
+ // - [3114. 替换字符可以得到的最晚时间](https://leetcode.cn/problems/latest-time-you-can-obtain-after-replacing-characters/) 1291
1156
+ // - [2224. 转化时间需要的最少操作数](https://leetcode.cn/problems/minimum-number-of-operations-to-convert-time/) 1296
1157
+ // - [2933. 高访问员工](https://leetcode.cn/problems/high-access-employees/) 1537
1158
+ // 解析月日:LC2409 https://leetcode.cn/problems/count-days-spent-together/
1159
+ func parseTime (s string ) (hour , minute , total int ) {
1160
+ hour = int (s [0 ]& 15 )* 10 + int (s [1 ]& 15 )
1161
+ minute = int (s [3 ]& 15 )* 10 + int (s [4 ]& 15 )
1162
+ total = hour * 60 + minute
1163
+ return
1164
+ }
1165
+
1166
+ // 从 i 可以跳到 [i,i+a[i]] 中的任意整点
1167
+ // 返回从 0 跳到 n-1 的最小跳跃次数
1168
+ // 如果无法到达 n-1,返回 -1
1169
+ // 注:对于复杂变形题,采用分组循环不易写错
1170
+ // - [45. 跳跃游戏 II](https://leetcode.cn/problems/jump-game-ii/)
1171
+ // - [1024. 视频拼接](https://leetcode.cn/problems/video-stitching/) 1746
1172
+ // - [1326. 灌溉花园的最少水龙头数目](https://leetcode.cn/problems/minimum-number-of-taps-to-open-to-water-a-garden/) 1885
1173
+ // 【图解】https://leetcode.cn/problems/minimum-number-of-taps-to-open-to-water-a-garden/solution/yi-zhang-tu-miao-dong-pythonjavacgo-by-e-wqry/
1174
+ // https://codeforces.com/problemset/problem/1630/C 2200 变形
1175
+ // https://codeforces.com/problemset/problem/1066/B 1500 这题其实不算,但如果每个加热器的 r 不同,就是跳跃游戏 II 了
1176
+ func minJumpNumbers (a []int ) (ans int ) {
1177
+ curR := 0 // 已建造的桥的右端点
1178
+ nxtR := 0 // 下一座桥的右端点的最大值
1179
+ // 这里没有遍历到 n-1,因为它已经是终点了
1180
+ for i , d := range a [:len (a )- 1 ] {
1181
+ r := i + d
1182
+ nxtR = max (nxtR , r )
1183
+ if i == curR { // 到达已建造的桥的右端点
1184
+ if i == nxtR { // 无论怎么造桥,都无法从 i 到 i+1
1185
+ return - 1
1186
+ }
1187
+ curR = nxtR // 建造下一座桥
1188
+ ans ++
1189
+ }
1190
+ }
1191
+ return
1192
+ }
1193
+
1194
+ // 合并 a 中所有重叠的闭区间(哪怕只有一个端点重叠,也算重叠)
1195
+ // 注意 [1,1] 和 [2,2] 不能合并成 [1,2]
1196
+ // 注:这种做法在变形题中容易写错,更加稳定的做法是差分数组
1197
+ // - [56. 合并区间](https://leetcode.cn/problems/merge-intervals/)
1198
+ // - [55. 跳跃游戏](https://leetcode.cn/problems/jump-game/)
1199
+ // - [763. 划分字母区间](https://leetcode.cn/problems/partition-labels/) 1443
1200
+ // - [3169. 无需开会的工作日](https://leetcode.cn/problems/count-days-without-meetings/) ~1500
1201
+ // - [2580. 统计将重叠区间合并成组的方案数](https://leetcode.cn/problems/count-ways-to-group-overlapping-ranges/) 1632
1202
+ // - [2963. 统计好分割方案的数目](https://leetcode.cn/problems/count-the-number-of-good-partitions/) 1985
1203
+ // - [2584. 分割数组使乘积互质](https://leetcode.cn/problems/split-the-array-to-make-coprime-products/) 2159
1204
+ // - [2655. 寻找最大长度的未覆盖区间](https://leetcode.cn/problems/find-maximal-uncovered-ranges/)(会员题)
1205
+ // 另见 common.go 中的「区间贪心」
1206
+ // https://codeforces.com/problemset/problem/1626/C 1700
1207
+ // - 倒序合并代码 https://codeforces.com/contest/1626/submission/211306494
1208
+ // https://codeforces.com/problemset/problem/1859/D 1800
1209
+ // https://codeforces.com/problemset/problem/1260/D 1900
1210
+ func mergeIntervals (a [][]int ) [][]int {
1211
+ slices .SortFunc (a , func (a , b []int ) int { return a [0 ] - b [0 ] }) // 按区间左端点排序
1212
+ merged := [][]int {}
1213
+ l0 := a [0 ][0 ]
1214
+ maxR := a [0 ][1 ]
1215
+ for _ , p := range a [1 :] { // 从第二个区间开始
1216
+ l , r := p [0 ], p [1 ]
1217
+ // 如果要合并 [1,1] 和 [2,2],下面改成 if l-1 > maxR
1218
+ if l > maxR { // 发现一个新区间
1219
+ merged = append (merged , []int {l0 , maxR }) // 先把旧的加入答案
1220
+ l0 = l // 记录新区间左端点
1221
+ }
1222
+ maxR = max (maxR , r )
1223
+ }
1224
+ merged = append (merged , []int {l0 , maxR }) // 最后发现的新区间加入答案
1225
+
1226
+ {
1227
+ // 包含 x 的闭区间
1228
+ var x int
1229
+ i := sort .Search (len (merged ), func (i int ) bool { return merged [i ][1 ] >= x })
1230
+ if i < len (merged ) && merged [i ][0 ] <= x {
1231
+ // ans[i]...
1232
+ }
1233
+ }
1234
+
1235
+ return merged
1236
+ }
0 commit comments