Skip to content

Commit 16a3c3d

Browse files
committed
feat: add solutions to lc problems: No.1493,2730
1 parent 42f3110 commit 16a3c3d

File tree

14 files changed

+521
-0
lines changed

14 files changed

+521
-0
lines changed

solution/1400-1499/1493.Longest Subarray of 1's After Deleting One Element/README.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,4 +304,100 @@ function longestSubarray(nums: number[]): number {
304304

305305
<!-- solution:end -->
306306

307+
<!-- solution:start -->
308+
309+
### 方法三:双指针(优化)
310+
311+
方法二中,我们每次会循环移动左指针,直到 $cnt \leq 1$。由于题目求的是最长子数组,意味着我们不需要缩小子数组的长度,因此,如果 $\textit{cnt} \gt 1$,我们只移动左指针一次,右指针也继续向右移动。这样可以保证子数组的长度不会减小。
312+
313+
最后,我们返回的答案即为 $n - l - 1$,其中 $l$ 为左指针的位置。
314+
315+
时间复杂度 $O(n)$,其中 $n$ 为数组 $nums$ 的长度。空间复杂度 $O(1)$。
316+
317+
<!-- tabs:start -->
318+
319+
#### Python3
320+
321+
```python
322+
class Solution:
323+
def longestSubarray(self, nums: List[int]) -> int:
324+
cnt = l = 0
325+
for x in nums:
326+
cnt += x ^ 1
327+
if cnt > 1:
328+
cnt -= nums[l] ^ 1
329+
l += 1
330+
return len(nums) - l - 1
331+
```
332+
333+
#### Java
334+
335+
```java
336+
class Solution {
337+
public int longestSubarray(int[] nums) {
338+
int ans = 0, cnt = 0, l = 0;
339+
for (int x : nums) {
340+
cnt += x ^ 1;
341+
if (cnt > 1) {
342+
cnt -= nums[l++] ^ 1;
343+
}
344+
}
345+
return nums.length - l - 1;
346+
}
347+
}
348+
```
349+
350+
#### C++
351+
352+
```cpp
353+
class Solution {
354+
public:
355+
int longestSubarray(vector<int>& nums) {
356+
int ans = 0, cnt = 0, l = 0;
357+
for (int x : nums) {
358+
cnt += x ^ 1;
359+
if (cnt > 1) {
360+
cnt -= nums[l++] ^ 1;
361+
}
362+
}
363+
return nums.size() - l - 1;
364+
}
365+
};
366+
```
367+
368+
#### Go
369+
370+
```go
371+
func longestSubarray(nums []int) (ans int) {
372+
cnt, l := 0, 0
373+
for _, x := range nums {
374+
cnt += x ^ 1
375+
if cnt > 1 {
376+
cnt -= nums[l] ^ 1
377+
l++
378+
}
379+
}
380+
return len(nums) - l - 1
381+
}
382+
```
383+
384+
#### TypeScript
385+
386+
```ts
387+
function longestSubarray(nums: number[]): number {
388+
let [cnt, l] = [0, 0];
389+
for (const x of nums) {
390+
cnt += x ^ 1;
391+
if (cnt > 1) {
392+
cnt -= nums[l++] ^ 1;
393+
}
394+
}
395+
return nums.length - l - 1;
396+
}
397+
```
398+
399+
<!-- tabs:end -->
400+
401+
<!-- solution:end -->
402+
307403
<!-- problem:end -->

solution/1400-1499/1493.Longest Subarray of 1's After Deleting One Element/README_EN.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,4 +303,100 @@ function longestSubarray(nums: number[]): number {
303303

304304
<!-- solution:end -->
305305

306+
<!-- solution:start -->
307+
308+
### Solution 3: Two Pointers (Optimization)
309+
310+
In Solution 2, we move the left pointer in a loop until $cnt \leq 1$. Since the problem asks for the longest subarray, it means we don't need to reduce the length of the subarray. Therefore, if $\textit{cnt} \gt 1$, we only move the left pointer once, and the right pointer continues to move to the right. This ensures that the length of the subarray does not decrease.
311+
312+
Finally, the answer we return is $n - l - 1$, where $l$ is the position of the left pointer.
313+
314+
The time complexity is $O(n)$, where $n$ is the length of the array $nums$. The space complexity is $O(1)$.
315+
316+
<!-- tabs:start -->
317+
318+
#### Python3
319+
320+
```python
321+
class Solution:
322+
def longestSubarray(self, nums: List[int]) -> int:
323+
cnt = l = 0
324+
for x in nums:
325+
cnt += x ^ 1
326+
if cnt > 1:
327+
cnt -= nums[l] ^ 1
328+
l += 1
329+
return len(nums) - l - 1
330+
```
331+
332+
#### Java
333+
334+
```java
335+
class Solution {
336+
public int longestSubarray(int[] nums) {
337+
int ans = 0, cnt = 0, l = 0;
338+
for (int x : nums) {
339+
cnt += x ^ 1;
340+
if (cnt > 1) {
341+
cnt -= nums[l++] ^ 1;
342+
}
343+
}
344+
return nums.length - l - 1;
345+
}
346+
}
347+
```
348+
349+
#### C++
350+
351+
```cpp
352+
class Solution {
353+
public:
354+
int longestSubarray(vector<int>& nums) {
355+
int ans = 0, cnt = 0, l = 0;
356+
for (int x : nums) {
357+
cnt += x ^ 1;
358+
if (cnt > 1) {
359+
cnt -= nums[l++] ^ 1;
360+
}
361+
}
362+
return nums.size() - l - 1;
363+
}
364+
};
365+
```
366+
367+
#### Go
368+
369+
```go
370+
func longestSubarray(nums []int) (ans int) {
371+
cnt, l := 0, 0
372+
for _, x := range nums {
373+
cnt += x ^ 1
374+
if cnt > 1 {
375+
cnt -= nums[l] ^ 1
376+
l++
377+
}
378+
}
379+
return len(nums) - l - 1
380+
}
381+
```
382+
383+
#### TypeScript
384+
385+
```ts
386+
function longestSubarray(nums: number[]): number {
387+
let [cnt, l] = [0, 0];
388+
for (const x of nums) {
389+
cnt += x ^ 1;
390+
if (cnt > 1) {
391+
cnt -= nums[l++] ^ 1;
392+
}
393+
}
394+
return nums.length - l - 1;
395+
}
396+
```
397+
398+
<!-- tabs:end -->
399+
400+
<!-- solution:end -->
401+
306402
<!-- problem:end -->
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
int longestSubarray(vector<int>& nums) {
4+
int ans = 0, cnt = 0, l = 0;
5+
for (int x : nums) {
6+
cnt += x ^ 1;
7+
if (cnt > 1) {
8+
cnt -= nums[l++] ^ 1;
9+
}
10+
}
11+
return nums.size() - l - 1;
12+
}
13+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
func longestSubarray(nums []int) (ans int) {
2+
cnt, l := 0, 0
3+
for _, x := range nums {
4+
cnt += x ^ 1
5+
if cnt > 1 {
6+
cnt -= nums[l] ^ 1
7+
l++
8+
}
9+
}
10+
return len(nums) - l - 1
11+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public int longestSubarray(int[] nums) {
3+
int ans = 0, cnt = 0, l = 0;
4+
for (int x : nums) {
5+
cnt += x ^ 1;
6+
if (cnt > 1) {
7+
cnt -= nums[l++] ^ 1;
8+
}
9+
}
10+
return nums.length - l - 1;
11+
}
12+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def longestSubarray(self, nums: List[int]) -> int:
3+
cnt = l = 0
4+
for x in nums:
5+
cnt += x ^ 1
6+
if cnt > 1:
7+
cnt -= nums[l] ^ 1
8+
l += 1
9+
return len(nums) - l - 1
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function longestSubarray(nums: number[]): number {
2+
let [cnt, l] = [0, 0];
3+
for (const x of nums) {
4+
cnt += x ^ 1;
5+
if (cnt > 1) {
6+
cnt -= nums[l++] ^ 1;
7+
}
8+
}
9+
return nums.length - l - 1;
10+
}

solution/2700-2799/2730.Find the Longest Semi-Repetitive Substring/README.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,4 +182,109 @@ function longestSemiRepetitiveSubstring(s: string): number {
182182

183183
<!-- solution:end -->
184184

185+
<!-- solution:start -->
186+
187+
### 方法二:双指针(优化)
188+
189+
由于题目只需要我们找到最长的半重复子字符串的长度,因此,每次当区间内相邻字符相等的个数超过 $1$ 时,我们可以只移动左指针 $l$ 一次,右指针 $r$ 继续向右移动。这样可以保证子字符串的长度不会减小。
190+
191+
最后答案为 $n - l$,其中 $n$ 是字符串的长度。
192+
193+
时间复杂度 $O(n)$,其中 $n$ 是字符串的长度。空间复杂度 $O(1)$。
194+
195+
<!-- tabs:start -->
196+
197+
#### Python3
198+
199+
```python
200+
class Solution:
201+
def longestSemiRepetitiveSubstring(self, s: str) -> int:
202+
n = len(s)
203+
cnt = l = 0
204+
for i in range(1, n):
205+
cnt += s[i] == s[i - 1]
206+
if cnt > 1:
207+
cnt -= s[l] == s[l + 1]
208+
l += 1
209+
return n - l
210+
```
211+
212+
#### Java
213+
214+
```java
215+
class Solution {
216+
public int longestSemiRepetitiveSubstring(String s) {
217+
int n = s.length();
218+
int cnt = 0, l = 0;
219+
for (int i = 1; i < n; ++i) {
220+
cnt += s.charAt(i) == s.charAt(i - 1) ? 1 : 0;
221+
if (cnt > 1) {
222+
cnt -= s.charAt(l) == s.charAt(++l) ? 1 : 0;
223+
}
224+
}
225+
return n - l;
226+
}
227+
}
228+
```
229+
230+
#### C++
231+
232+
```cpp
233+
class Solution {
234+
public:
235+
int longestSemiRepetitiveSubstring(string s) {
236+
int n = s.length();
237+
int cnt = 0, l = 0;
238+
for (int i = 1; i < n; ++i) {
239+
cnt += s[i] == s[i - 1] ? 1 : 0;
240+
if (cnt > 1) {
241+
cnt -= s[l] == s[++l] ? 1 : 0;
242+
}
243+
}
244+
return n - l;
245+
}
246+
};
247+
```
248+
249+
#### Go
250+
251+
```go
252+
func longestSemiRepetitiveSubstring(s string) (ans int) {
253+
cnt, l := 0, 0
254+
for i, c := range s[1:] {
255+
if byte(c) == s[i] {
256+
cnt++
257+
}
258+
if cnt > 1 {
259+
if s[l] == s[l+1] {
260+
cnt--
261+
}
262+
l++
263+
}
264+
}
265+
return len(s) - l
266+
}
267+
```
268+
269+
#### TypeScript
270+
271+
```ts
272+
function longestSemiRepetitiveSubstring(s: string): number {
273+
const n = s.length;
274+
let [cnt, l] = [0, 0];
275+
for (let i = 1; i < n; ++i) {
276+
cnt += s[i] === s[i - 1] ? 1 : 0;
277+
if (cnt > 1) {
278+
cnt -= s[l] === s[l + 1] ? 1 : 0;
279+
++l;
280+
}
281+
}
282+
return n - l;
283+
}
284+
```
285+
286+
<!-- tabs:end -->
287+
288+
<!-- solution:end -->
289+
185290
<!-- problem:end -->

0 commit comments

Comments
 (0)