Skip to content

Commit 8bf447f

Browse files
authored
Merge branch 'doocs:main' into main
2 parents 9c67a48 + abedc9f commit 8bf447f

File tree

25 files changed

+778
-39
lines changed

25 files changed

+778
-39
lines changed

solution/2600-2699/2674.Split a Circular Linked List/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646

4747
### 方法一:快慢指针
4848

49-
我们定义两个指针 $a$ 和 $b$,初始时都指向链表的头节点。每次迭代时,$a$ 指针向前移动一步,$b$ 指针向前移动两步,直到 $b$ 指针到达链表的末尾。此时,$a$ 指针指向链表节点数的一半,我们将链表从 $a$ 指针处断开,即可得到两个链表的头节点。
49+
我们定义两个指针 $a$ 和 $b$,初始时都指向链表的头节点。每次迭代时,指针 $a$ 向前移动一步,指针 $b$ 向前移动两步,直到指针 $b$ 到达链表的末尾。此时,指针 $a$ 指向链表节点数的一半,我们将链表从指针 $a$ 处断开,即可得到两个链表的头节点。
5050

5151
时间复杂度 $O(n)$,其中 $n$ 是链表的长度。需要遍历链表一次。空间复杂度 $O(1)$。
5252

solution/2600-2699/2674.Split a Circular Linked List/README_EN.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@
4040

4141
## Solutions
4242

43-
### Solution 1
43+
### Solution 1: Fast and Slow Pointers
44+
45+
We define two pointers $a$ and $b$, both initially pointing to the head of the linked list. Each iteration, pointer $a$ moves forward one step, and pointer $b$ moves forward two steps, until pointer $b$ reaches the end of the linked list. At this point, pointer $a$ points to half of the linked list nodes, and we break the linked list from pointer $a$, thus obtaining the head nodes of the two linked lists.
46+
47+
The time complexity is $O(n)$, where $n$ is the length of the linked list. It requires one traversal of the linked list. The space complexity is $O(1)$.
4448

4549
<!-- tabs:start -->
4650

solution/3000-3099/3063.Linked List Frequency/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
<!-- 这里写题目描述 -->
1010

11-
<p>给定包含 <code>k</code> 个&nbsp;<strong>不同&nbsp;</strong>元素的链表的&nbsp;<code>head</code>&nbsp;节点,返回包含给定链表中每个 <strong>不同元素</strong> 以 <strong>任何顺序</strong> 出现的 <span data-keyword="frequency-linkedlist">频率</span> 的长度为&nbsp;<code>k</code>&nbsp;的链表的头节点。</p>
11+
<p>给定包含 <code>k</code> 个&nbsp;<strong>不同&nbsp;</strong>元素的链表的&nbsp;<code>head</code>&nbsp;节点,创建一个长度为&nbsp;<code>k</code>&nbsp;的链表,包含给定链表中每个 <strong>不同元素</strong> 以 <strong>任何顺序</strong> 出现的 <span data-keyword="frequency-linkedlist">频率</span>&nbsp;。返回这个链表的头节点。</p>
1212

1313
<p>&nbsp;</p>
1414

solution/3000-3099/3073.Maximum Increasing Triplet Value/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[English Version](/solution/3000-3099/3073.Maximum%20Increasing%20Triplet%20Value/README_EN.md)
44

5-
<!-- tags: -->
5+
<!-- tags:数组,有序集合 -->
66

77
## 题目描述
88

solution/3000-3099/3073.Maximum Increasing Triplet Value/README_EN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[中文文档](/solution/3000-3099/3073.Maximum%20Increasing%20Triplet%20Value/README.md)
44

5-
<!-- tags: -->
5+
<!-- tags:Array,Ordered Set -->
66

77
## Description
88

solution/3000-3099/3074.Apple Redistribution into Boxes/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[English Version](/solution/3000-3099/3074.Apple%20Redistribution%20into%20Boxes/README_EN.md)
44

5-
<!-- tags: -->
5+
<!-- tags:贪心,数组,排序 -->
66

77
## 题目描述
88

solution/3000-3099/3074.Apple Redistribution into Boxes/README_EN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[中文文档](/solution/3000-3099/3074.Apple%20Redistribution%20into%20Boxes/README.md)
44

5-
<!-- tags: -->
5+
<!-- tags:Greedy,Array,Sorting -->
66

77
## Description
88

solution/3000-3099/3075.Maximize Happiness of Selected Children/README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[English Version](/solution/3000-3099/3075.Maximize%20Happiness%20of%20Selected%20Children/README_EN.md)
44

5-
<!-- tags: -->
5+
<!-- tags:贪心,数组,排序 -->
66

77
## 题目描述
88

@@ -121,6 +121,18 @@ func maximumHappinessSum(happiness []int, k int) (ans int64) {
121121
}
122122
```
123123

124+
```ts
125+
function maximumHappinessSum(happiness: number[], k: number): number {
126+
happiness.sort((a, b) => b - a);
127+
let ans = 0;
128+
for (let i = 0; i < k; ++i) {
129+
const x = happiness[i] - i;
130+
ans += Math.max(x, 0);
131+
}
132+
return ans;
133+
}
134+
```
135+
124136
<!-- tabs:end -->
125137

126138
<!-- end -->

solution/3000-3099/3075.Maximize Happiness of Selected Children/README_EN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[中文文档](/solution/3000-3099/3075.Maximize%20Happiness%20of%20Selected%20Children/README.md)
44

5-
<!-- tags: -->
5+
<!-- tags:Greedy,Array,Sorting -->
66

77
## Description
88

solution/3000-3099/3076.Shortest Uncommon Substring in an Array/README.md

Lines changed: 126 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[English Version](/solution/3000-3099/3076.Shortest%20Uncommon%20Substring%20in%20an%20Array/README_EN.md)
44

5-
<!-- tags: -->
5+
<!-- tags:字典树,数组,哈希表,字符串 -->
66

77
## 题目描述
88

@@ -56,24 +56,145 @@
5656

5757
## 解法
5858

59-
### 方法一
59+
### 方法一:枚举
60+
61+
我们注意到数据规模很小,所以可以直接枚举每个字符串的所有子串,然后判断是否是其他字符串的子串。
62+
63+
具体地,我们先枚举每个字符串 `arr[i]`,然后从小到大枚举每个子串的长度 $j$,然后枚举每个子串的起始位置 $l$,可以得到当前子串为 `sub = arr[i][l:l+j]`,然后判断 `sub` 是否是其他字符串的子串,如果是的话,就跳过当前子串,否则更新答案。
64+
65+
时间复杂度 $O(n^2 \times m^4)$,空间复杂度 $O(m)$。其中 $n$ 是字符串数组 `arr` 的长度,而 $m$ 是字符串的最大长度,本题中 $m \le 20$。
6066

6167
<!-- tabs:start -->
6268

6369
```python
64-
70+
class Solution:
71+
def shortestSubstrings(self, arr: List[str]) -> List[str]:
72+
ans = [""] * len(arr)
73+
for i, s in enumerate(arr):
74+
m = len(s)
75+
for j in range(1, m + 1):
76+
for l in range(m - j + 1):
77+
sub = s[l : l + j]
78+
if not ans[i] or ans[i] > sub:
79+
if all(k == i or sub not in t for k, t in enumerate(arr)):
80+
ans[i] = sub
81+
if ans[i]:
82+
break
83+
return ans
6584
```
6685

6786
```java
68-
87+
class Solution {
88+
public String[] shortestSubstrings(String[] arr) {
89+
int n = arr.length;
90+
String[] ans = new String[n];
91+
Arrays.fill(ans, "");
92+
for (int i = 0; i < n; ++i) {
93+
int m = arr[i].length();
94+
for (int j = 1; j <= m && ans[i].isEmpty(); ++j) {
95+
for (int l = 0; l <= m - j; ++l) {
96+
String sub = arr[i].substring(l, l + j);
97+
if (ans[i].isEmpty() || sub.compareTo(ans[i]) < 0) {
98+
boolean ok = true;
99+
for (int k = 0; k < n && ok; ++k) {
100+
if (k != i && arr[k].contains(sub)) {
101+
ok = false;
102+
}
103+
}
104+
if (ok) {
105+
ans[i] = sub;
106+
}
107+
}
108+
}
109+
}
110+
}
111+
return ans;
112+
}
113+
}
69114
```
70115

71116
```cpp
72-
117+
class Solution {
118+
public:
119+
vector<string> shortestSubstrings(vector<string>& arr) {
120+
int n = arr.size();
121+
vector<string> ans(n);
122+
for (int i = 0; i < n; ++i) {
123+
int m = arr[i].size();
124+
for (int j = 1; j <= m && ans[i].empty(); ++j) {
125+
for (int l = 0; l <= m - j; ++l) {
126+
string sub = arr[i].substr(l, j);
127+
if (ans[i].empty() || sub < ans[i]) {
128+
bool ok = true;
129+
for (int k = 0; k < n && ok; ++k) {
130+
if (k != i && arr[k].find(sub) != string::npos) {
131+
ok = false;
132+
}
133+
}
134+
if (ok) {
135+
ans[i] = sub;
136+
}
137+
}
138+
}
139+
}
140+
}
141+
return ans;
142+
}
143+
};
73144
```
74145
75146
```go
147+
func shortestSubstrings(arr []string) []string {
148+
ans := make([]string, len(arr))
149+
for i, s := range arr {
150+
m := len(s)
151+
for j := 1; j <= m && len(ans[i]) == 0; j++ {
152+
for l := 0; l <= m-j; l++ {
153+
sub := s[l : l+j]
154+
if len(ans[i]) == 0 || ans[i] > sub {
155+
ok := true
156+
for k, t := range arr {
157+
if k != i && strings.Contains(t, sub) {
158+
ok = false
159+
break
160+
}
161+
}
162+
if ok {
163+
ans[i] = sub
164+
}
165+
}
166+
}
167+
}
168+
}
169+
return ans
170+
}
171+
```
76172

173+
```ts
174+
function shortestSubstrings(arr: string[]): string[] {
175+
const n: number = arr.length;
176+
const ans: string[] = Array(n).fill('');
177+
for (let i = 0; i < n; ++i) {
178+
const m: number = arr[i].length;
179+
for (let j = 1; j <= m && ans[i] === ''; ++j) {
180+
for (let l = 0; l <= m - j; ++l) {
181+
const sub: string = arr[i].slice(l, l + j);
182+
if (ans[i] === '' || sub.localeCompare(ans[i]) < 0) {
183+
let ok: boolean = true;
184+
for (let k = 0; k < n && ok; ++k) {
185+
if (k !== i && arr[k].includes(sub)) {
186+
ok = false;
187+
}
188+
}
189+
if (ok) {
190+
ans[i] = sub;
191+
}
192+
}
193+
}
194+
}
195+
}
196+
return ans;
197+
}
77198
```
78199

79200
<!-- tabs:end -->

0 commit comments

Comments
 (0)