Skip to content

Commit 5c6586e

Browse files
committed
feat: add solutions to lc problems: No.2500~2503
* No.2500.Delete Greatest Value in Each Row * No.2501.Longest Square Streak in an Array * No.2502.Design Memory Allocator * No.2503.Maximum Number of Points From Grid Queries
1 parent a5a094c commit 5c6586e

File tree

10 files changed

+604
-20
lines changed

10 files changed

+604
-20
lines changed

solution/1900-1999/1930.Unique Length-3 Palindromic Subsequences/Solution.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ def countPalindromicSubsequence(self, s: str) -> int:
44
for c in ascii_lowercase:
55
l, r = s.find(c), s.rfind(c)
66
if r - l > 1:
7-
ans += len(set(s[l + 1: r]))
7+
ans += len(set(s[l + 1 : r]))
88
return ans

solution/2500-2599/2500.Delete Greatest Value in Each Row/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@
6262

6363
<!-- 这里可写通用的实现逻辑 -->
6464

65+
**方法一:排序**
66+
67+
由于每一次操作都是从每一行中删除最大值,然后取最大值加到答案中,因此我们可以先对每一行进行排序。
68+
69+
然后遍历每一列,取每一列的最大值,然后将其加到答案中即可。
70+
71+
时间复杂度 $O(m \times n \times \log n)$,空间复杂度 $O(1)$。其中 $m$ 和 $n$ 分别是矩阵的行数和列数。
72+
6573
<!-- tabs:start -->
6674

6775
### **Python3**

solution/2500-2599/2501.Longest Square Streak in an Array/README.md

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,25 @@
5050

5151
<!-- 这里可写通用的实现逻辑 -->
5252

53+
**方法一:哈希表 + 枚举**
54+
55+
我们先用哈希表记录数组中的所有元素,然后枚举数组中的每个元素作为子序列的第一个元素,将该元素不断平方,并判断平方后的结果是否在哈希表中,如果在,则将平方后的结果作为下一个元素,继续判断,直到平方后的结果不在哈希表中,此时判断子序列的长度是否大于 $1$,如果是,则更新答案。
56+
57+
时间复杂度 $O(n \times \log \log M)$,空间复杂度 $O(n)$。其中 $n$ 为数组 `nums` 的长度,而 $M$ 为数组 `nums` 中的最大元素。
58+
59+
**方法二:记忆化搜索**
60+
61+
与方法一类似,我们先用哈希表记录数组中的所有元素。然后设计一个函数 $dfs(x)$,表示以 $x$ 为第一个元素的方波的长度。那么答案就是 $max(dfs(x))$,其中 $x$ 为数组 `nums` 中的元素。
62+
63+
函数 $dfs(x)$ 的计算过程如下:
64+
65+
- 如果 $x$ 不在哈希表中,则返回 $0$。
66+
- 否则,返回 $1 + dfs(x^2)$。
67+
68+
过程中我们可以使用记忆化搜索,即使用哈希表记录函数 $dfs(x)$ 的值,避免重复计算。
69+
70+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 `nums` 的长度。
71+
5372
<!-- tabs:start -->
5473

5574
### **Python3**
@@ -71,6 +90,20 @@ class Solution:
7190
return ans
7291
```
7392

93+
```python
94+
class Solution:
95+
def longestSquareStreak(self, nums: List[int]) -> int:
96+
@cache
97+
def dfs(x):
98+
if x not in s:
99+
return 0
100+
return 1 + dfs(x * x)
101+
102+
s = set(nums)
103+
ans = max(dfs(x) for x in nums)
104+
return -1 if ans < 2 else ans
105+
```
106+
74107
### **Java**
75108

76109
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -98,6 +131,36 @@ class Solution {
98131
}
99132
```
100133

134+
```java
135+
class Solution {
136+
private Map<Integer, Integer> f = new HashMap<>();
137+
private Set<Integer> s = new HashSet<>();
138+
139+
public int longestSquareStreak(int[] nums) {
140+
for (int v : nums) {
141+
s.add(v);
142+
}
143+
int ans = 0;
144+
for (int v : nums) {
145+
ans = Math.max(ans, dfs(v));
146+
}
147+
return ans < 2 ? -1 : ans;
148+
}
149+
150+
private int dfs(int x) {
151+
if (!s.contains(x)) {
152+
return 0;
153+
}
154+
if (f.containsKey(x)) {
155+
return f.get(x);
156+
}
157+
int ans = 1 + dfs(x * x);
158+
f.put(x, ans);
159+
return ans;
160+
}
161+
}
162+
```
163+
101164
### **C++**
102165

103166
```cpp
@@ -120,6 +183,27 @@ public:
120183
};
121184
```
122185
186+
```cpp
187+
class Solution {
188+
public:
189+
int longestSquareStreak(vector<int>& nums) {
190+
unordered_set<long long> s(nums.begin(), nums.end());
191+
int ans = 0;
192+
unordered_map<int, int> f;
193+
function<int(int)> dfs = [&](int x) -> int {
194+
if (!s.count(x)) return 0;
195+
if (f.count(x)) return f[x];
196+
long long t = 1ll * x * x;
197+
if (t > INT_MAX) return 1;
198+
f[x] = 1 + dfs(x * x);
199+
return f[x];
200+
};
201+
for (int& v : nums) ans = max(ans, dfs(v));
202+
return ans < 2 ? -1 : ans;
203+
}
204+
};
205+
```
206+
123207
### **Go**
124208

125209
```go
@@ -143,6 +227,36 @@ func longestSquareStreak(nums []int) int {
143227
}
144228
```
145229

230+
```go
231+
func longestSquareStreak(nums []int) (ans int) {
232+
s := map[int]bool{}
233+
for _, v := range nums {
234+
s[v] = true
235+
}
236+
f := map[int]int{}
237+
var dfs func(int) int
238+
dfs = func(x int) int {
239+
if !s[x] {
240+
return 0
241+
}
242+
if v, ok := f[x]; ok {
243+
return v
244+
}
245+
f[x] = 1 + dfs(x*x)
246+
return f[x]
247+
}
248+
for _, v := range nums {
249+
if t := dfs(v); ans < t {
250+
ans = t
251+
}
252+
}
253+
if ans < 2 {
254+
return -1
255+
}
256+
return ans
257+
}
258+
```
259+
146260
### **...**
147261

148262
```

solution/2500-2599/2501.Longest Square Streak in an Array/README_EN.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,20 @@ class Solution:
6565
return ans
6666
```
6767

68+
```python
69+
class Solution:
70+
def longestSquareStreak(self, nums: List[int]) -> int:
71+
@cache
72+
def dfs(x):
73+
if x not in s:
74+
return 0
75+
return 1 + dfs(x * x)
76+
77+
s = set(nums)
78+
ans = max(dfs(x) for x in nums)
79+
return -1 if ans < 2 else ans
80+
```
81+
6882
### **Java**
6983

7084
```java
@@ -90,6 +104,36 @@ class Solution {
90104
}
91105
```
92106

107+
```java
108+
class Solution {
109+
private Map<Integer, Integer> f = new HashMap<>();
110+
private Set<Integer> s = new HashSet<>();
111+
112+
public int longestSquareStreak(int[] nums) {
113+
for (int v : nums) {
114+
s.add(v);
115+
}
116+
int ans = 0;
117+
for (int v : nums) {
118+
ans = Math.max(ans, dfs(v));
119+
}
120+
return ans < 2 ? -1 : ans;
121+
}
122+
123+
private int dfs(int x) {
124+
if (!s.contains(x)) {
125+
return 0;
126+
}
127+
if (f.containsKey(x)) {
128+
return f.get(x);
129+
}
130+
int ans = 1 + dfs(x * x);
131+
f.put(x, ans);
132+
return ans;
133+
}
134+
}
135+
```
136+
93137
### **C++**
94138

95139
```cpp
@@ -112,6 +156,27 @@ public:
112156
};
113157
```
114158
159+
```cpp
160+
class Solution {
161+
public:
162+
int longestSquareStreak(vector<int>& nums) {
163+
unordered_set<long long> s(nums.begin(), nums.end());
164+
int ans = 0;
165+
unordered_map<int, int> f;
166+
function<int(int)> dfs = [&](int x) -> int {
167+
if (!s.count(x)) return 0;
168+
if (f.count(x)) return f[x];
169+
long long t = 1ll * x * x;
170+
if (t > INT_MAX) return 1;
171+
f[x] = 1 + dfs(x * x);
172+
return f[x];
173+
};
174+
for (int& v : nums) ans = max(ans, dfs(v));
175+
return ans < 2 ? -1 : ans;
176+
}
177+
};
178+
```
179+
115180
### **Go**
116181

117182
```go
@@ -135,6 +200,36 @@ func longestSquareStreak(nums []int) int {
135200
}
136201
```
137202

203+
```go
204+
func longestSquareStreak(nums []int) (ans int) {
205+
s := map[int]bool{}
206+
for _, v := range nums {
207+
s[v] = true
208+
}
209+
f := map[int]int{}
210+
var dfs func(int) int
211+
dfs = func(x int) int {
212+
if !s[x] {
213+
return 0
214+
}
215+
if v, ok := f[x]; ok {
216+
return v
217+
}
218+
f[x] = 1 + dfs(x*x)
219+
return f[x]
220+
}
221+
for _, v := range nums {
222+
if t := dfs(v); ans < t {
223+
ans = t
224+
}
225+
}
226+
if ans < 2 {
227+
return -1
228+
}
229+
return ans
230+
}
231+
```
232+
138233
### **...**
139234

140235
```

0 commit comments

Comments
 (0)