Skip to content

Commit d33dbbf

Browse files
committed
Merge branch 'main' into feat/Crokily/mdEditor
2 parents 6b21a3c + 80b334c commit d33dbbf

15 files changed

+893
-63
lines changed

.github/workflows/sync-uuid.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ concurrency:
2222
jobs:
2323
backfill:
2424
# 防止 fork、限定 main、并避免机器人循环
25-
if: github.repository == 'InvolutionHell/involutionhell.github.io' &&
25+
if:
2626
(github.ref == 'refs/heads/main' || github.ref == 'refs/heads/feat/contributor') &&
2727
github.actor != 'github-actions[bot]'
2828
runs-on: ubuntu-latest
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
---
2+
title: 1004.Maximum continuity1Number III Maximum continuity1Number III
3+
date: "2022.12.07-01:15"
4+
tags:
5+
- - Python
6+
- - solved
7+
- answer
8+
abbrlink: ed19b576
9+
docId: ytg2bds2dnhzw37nrb3vassy
10+
---
11+
12+
Today's daily question is too difficult,So find the problem by yourself。Today's question is a hash table+Sliding window algorithm,虽然感觉只用了Sliding window algorithm。
13+
14+
```python
15+
16+
class Solution:
17+
def longestOnes(self, nums: List[int], k: int) -> int:
18+
k_mean = k
19+
# Used to record how many arrays there are now
20+
flag = 0
21+
# Used to record the maximum land number
22+
max_flag = 0
23+
for start in range(len(nums)):
24+
tail = start
25+
while k >= 0 and tail <= len(nums) - 1:
26+
if nums[tail] == 1:
27+
tail += 1
28+
flag += 1
29+
elif nums[tail] == 0 and k > 0:
30+
tail += 1
31+
k -= 1
32+
flag += 1
33+
elif nums[tail] == 0 and k == 0:
34+
k = k_mean
35+
max_flag = max(max_flag, flag)
36+
flag = 0
37+
break
38+
if tail == len(nums):
39+
max_flag = max(max_flag, flag)
40+
flag = 0
41+
break
42+
return max_flag
43+
```
44+
45+
This is my approach at the beginning,Although the double pointer is used,But there is no flexibility,Very empty feeling,Very dry slide。
46+
the following[@Lincoln](/u/lincoln)@Lincoln Big practice,Just as one record of yourself,不作为我的answer发表
47+
48+
```
49+
class Solution:
50+
def longestOnes(self, nums: List[int], k: int) -> int:
51+
"""
52+
Thinking:1. k=0It can be understood as the problem of the maximum duplication sub -string
53+
2. If the current window value-In the window1Number <= k: Then expand the window(right+1)
54+
If the current window value-In the window1Number > k: Swipe the window to the right(left+1)
55+
method:Hash table + Sliding window
56+
"""
57+
n = len(nums)
58+
o_res = 0
59+
left = right = 0
60+
while right < n:
61+
if nums[right]== 1: o_res += 1
62+
if right-left+1- o_res > k:
63+
if nums[left]== 1: o_res -= 1
64+
left += 1
65+
right += 1
66+
return right - left
67+
```
68+
69+
Look atLincolnBigThinking,very clearly,remember
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
---
2+
title: 1234. Replace the sub -string to get a balanced string One question daily
3+
date: "2024.01.01 0:00"
4+
tags:
5+
- - Python
6+
- - answer
7+
abbrlink: 56d97dcf
8+
docId: p8igr19xfxnuyo2lpngnr6fg
9+
---
10+
11+
# topic:
12+
13+
[1234. Replace the sub -string to get a balanced string.md](https://leetcode.cn/problems/replace-the-substring-for-balanced-string/description/)
14+
15+
# Thought:
16+
17+
`all()`:if bool(x) For all the values ​​in iterative objects x All for True,Then return True。 if可迭代对象为空,Then return True。
18+
19+
Tongxiang dual pointer,The solution of the spiritual god of this question。
20+
If in this string,These four characters happen just to appear n/4 Second-rate,Then it is one「Balanced string」。
21+
if在待替换子串之外的任意字符的出现Second-rate数都**Exceed** $m=\dfrac{n}{4}$
22+
,So no matter how you replace it,都无法make这个字符的出现Second-rate数等于m。
23+
on the other hand,if在待替换子串之外的任意字符的出现Second-rate数都**不Exceed** m,
24+
25+
> So it can be replaced ,make s 为Balanced string,即每个字符的出现Second-rate数均为 m。
26+
> For this question,The left and right end points of the sub -string are left and right,enumerate right,
27+
> if子串外的任意字符的出现Second-rate数都不Exceedm,The explanation from left arrive
28+
> rightThis sub -string can be to replace the sub -string,Length right−left+1
29+
> Update the minimum value of the answer,Move to the right left,Sumid sub -string length。
30+
31+
# Code:
32+
33+
```python
34+
class Solution:
35+
def balancedString(self, s: str) -> int:
36+
s_c = Counter(s)
37+
n = len(s)
38+
if all(s_c[v] <= n//4 for v in s_c):
39+
return 0
40+
ans, left = inf, 0
41+
# enumerate右端点
42+
for i, j in enumerate(s):
43+
s_c[j] -= 1
44+
while all(s_c[v] <= n // 4 for v in s_c):
45+
ans = min(ans, i - left + 1)
46+
s_c[s[left]] += 1
47+
left += 1
48+
return ans
49+
50+
```
51+
52+
```go
53+
func balancedString(s string) int {
54+
cnt, m := ['X']int{}, len(s)/4
55+
for _, c := range s {
56+
cnt[c]++
57+
}
58+
if cnt['Q'] == m && cnt['W'] == m && cnt['E'] == m && cnt['R'] == m {
59+
return 0
60+
}
61+
ans, left := len(s), 0
62+
for right, c := range s {
63+
cnt[c]--
64+
for cnt['Q'] <= m && cnt['W'] <= m && cnt['E'] <= m && cnt['R'] <= m {
65+
ans = min(ans, right-left+1)
66+
cnt[s[left]]++
67+
left++
68+
}
69+
}
70+
return ans
71+
}
72+
func min(a, b int) int { if a > b { return b }; return a }
73+
```
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
title: 1653. The minimum number of times to balance the string balance.md
3+
date: "2024.01.01 0:00"
4+
tags:
5+
- - Python
6+
- - answer
7+
abbrlink: cac21f27
8+
docId: bsf0yz1zrmlz7masrdmq8fq6
9+
---
10+
11+
# topic:
12+
13+
[1653. The minimum number of times to balance the string balance.md](https://leetcode.cn/problems/minimum-deletions-to-make-string-balanced/description/)
14+
15+
# Thought:
16+
17+
ask:Why if-else Write (c - 'a') \* 2 - 1 It will be much faster?
18+
19+
answer:CPU I encounter a branch(Condition jump instruction)Which branch of the code will be predicted when the code is executed,If the prediction is correct,
20+
CPU It will continue to execute the program in accordance with the predicted path。But if the prediction fails,CPU You need to roll back the previous instructions and load the correct instructions,To ensure the correctness of the program execution。
21+
22+
For the data of this question,character ‘a’ and ‘b’ It can be considered to appear random,In this case, the branch prediction will be available 50% Probability failure。
23+
失败导致的回滚and加载操作需要消耗额外的 CPU cycle,If the branch can be removed at a smaller price,The situation of this question can inevitably bring efficiency improvement。
24+
25+
Notice:This optimization method often reduces readability,It's best not to use in business code。
26+
27+
# Code:
28+
29+
```python
30+
class Solution:
31+
def minimumDeletions(self, s: str) -> int:
32+
ans = delete = s.count('a')
33+
for c in s:
34+
delete -= 1 if c == 'a' else -1
35+
if delete < ans: # Manually min It will be much faster
36+
ans = delete
37+
return ans
38+
39+
```
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
title: 2270. Number of Ways to Split Array.md
3+
date: "2025/1/14-9:31"
4+
tags:
5+
- - Python
6+
- - Answer
7+
abbrlink: c25bb550
8+
docId: a6inw303oslb7i5tcqj5xxx4
9+
---
10+
11+
# QUESTION:
12+
13+
[2270. Number of Ways to Split Array.md](https://leetcode.cn/problems/number-of-ways-to-split-array/description/)
14+
15+
# My Think:
16+
17+
`2 <= nums.length <= 105`, 因此我们可以直接获取到第一个数字, 初始状态就在指针于index0, 正要往index1走的时候.
18+
然后只需要一次For循环就可以搞定
19+
20+
重点是第二个方法, 来自题解.
21+
22+
# Code:
23+
24+
```python
25+
class Solution:
26+
def waysToSplitArray(self, nums: List[int]) -> int:
27+
temp_sum = nums[0]
28+
total_sum = sum(nums) - temp_sum
29+
ans = 0
30+
for i in range(1, len(nums)):
31+
if temp_sum >= total_sum:
32+
ans += 1
33+
temp_sum += nums[i]
34+
total_sum -= nums[i]
35+
return ans
36+
```
37+
38+
```python
39+
t = (sum(nums) + 1) // 2
40+
return sum(s >= t for s in accumulate(nums[:-1]))
41+
```
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
---
2+
title: 2679.In the matrix and the harmony.md
3+
date: "2024.01.01 0:00"
4+
tags:
5+
- - Python
6+
- - answer
7+
- - Array
8+
- - matrix
9+
- - Sort
10+
- - simulation
11+
- - heap(Priority queue)
12+
abbrlink: "5277100"
13+
docId: clx9mmqqvxipdfamqciuo146
14+
---
15+
16+
# topic:
17+
18+
[2679.In the matrix and the harmony.md](https://leetcode.cn/problems/sum-in-a-matrix/)
19+
20+
# Thought:
21+
22+
**One -line**
23+
First of all, the meaning is to find the largest number of each sub -list,Thenpopgo out,Finally ask for peace。
24+
The effect of traversing again and again is too bad,So I thought of using itzip一次性Traversal多个子Array。
25+
于yes先对每个子ArraySort,Then usezipTraversal,Find the maximum。
26+
for example:
27+
`nums = [[7,2,1],[6,4,2],[6,5,3],[3,2,1]]`
28+
Sort后:
29+
`nums = [[1,2,7],[2,4,6],[3,5,6],[1,2,3]]`
30+
Then usezipTraversal得到:
31+
`[(1,2,3,1),(2,4,5,2),(7,6,6,3)]`
32+
Find the maximum:
33+
`[3,5,7]`
34+
Context:
35+
`15`
36+
37+
# Code:
38+
39+
```python
40+
class Solution:
41+
def matrixSum(self, nums: List[List[int]]) -> int:
42+
return sum(max(i) for i in \
43+
zip(*(sorted(sublist) for sublist in nums)))
44+
```
45+
46+
### \*The role and the rolezip()explain
47+
48+
```python
49+
nums = [[1,2,7],[2,4,6],[3,5,6],[1,2,3]]
50+
51+
for i in range(len(nums[1])):
52+
for j in range(len(nums)):
53+
print(nums[j][i])
54+
# ans = 123124527663
55+
num1 = [1,2,7]
56+
num2 = [2,4,6]
57+
num3 = [3,5,6]
58+
num4 = [1,2,3]
59+
```
60+
61+
`zip()`The function corresponds to one -to -one elements in multiple lists,Then return onezipObject,Can uselist()Function convert to list。
62+
63+
```python
64+
for i in zip(num1, num2, num3, num4):
65+
print(i)
66+
#(1, 2, 3, 1)
67+
#(2, 4, 5, 2)
68+
#(7, 6, 6, 3)
69+
```
70+
71+
`*nums`The role ispythonNot a pointer,InsteadnumsEach element in the parameter is passed into the function。I understand here as a list。
72+
73+
```python
74+
75+
# Enumerate
76+
print(*nums)
77+
# [1, 2, 7] [2, 4, 6] [3, 5, 6] [1, 2, 3]
78+
```
79+
80+
`zip(*nums)`WillnumsEach element is passed in as a parameterzip()In the function,Then return onezipObject,Can uselist()Function convert to list。
81+
`zip(*nums)`Equivalent to`zip(num1, num2, num3, num4)`,innum1, num2, num3, num4yesnumsElement。
82+
83+
```python
84+
for i in zip(*nums):
85+
print(i)
86+
# Equivalent
87+
#(1, 2, 3, 1)
88+
#(2, 4, 5, 2)
89+
#(7, 6, 6, 3)
90+
```
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
title: Python beat98.40% collectionsofCounter method!
3+
date: "2024.01.01 0:00"
4+
tags:
5+
- - Python
6+
- - solved
7+
- answer
8+
abbrlink: 73b5ce9c
9+
category: null
10+
docId: ryp6s59uwc10w2dywgs6f66h
11+
---
12+
13+
Pyhton `collection` In the bag `Counter()` Be rightlist里出现of元素conduct计数,And the output is a dictionary。
14+
for example`nums=[1, 1, 1, 2, 2, 3]` ToCounter后of结果是`Counter({1: 3, 2: 2, 3: 1})`
15+
16+
1. So traverse a dictionary,when`value>3`of时候`value=2`,Can be greater than2indivualof元素计数become2indivual。
17+
1. So we will`Counter({1: 3, 2: 2, 3: 1})`become`Counter({1: 2, 2: 2, 3: 1})`后再Toelementsconductlist操作就可以得到改变后of列表了。
18+
19+
---
20+
21+
Due to the meaning of the question“Input the array「Quote」方式传递of”,So we willnumsJust fill in and fill in
22+
23+
```python
24+
from collections import Counter # Imported package
25+
class Solution:
26+
def removeDuplicates(self, nums: List[int]) -> List[int]:
27+
dict1 = Counter(nums)
28+
for i in dict1:
29+
if dict1[i] > 2:
30+
dict1[i] = 2
31+
list1 = list(dict1.elements())
32+
nums.clear() # clear the list
33+
nums.extend(list1) # Add the collection to the list
34+
return len(nums)
35+
```
36+
37+
Complexity analysis
38+
39+
time complexity:`O(n)`,in `n` 是数组of长度。We have a maximum of this array once。
40+
41+
Spatial complexity:`O(1)`。我们只需要常数of空间存储若干变量。

0 commit comments

Comments
 (0)