Skip to content

Commit a52ac55

Browse files
committed
feat: add solutions to lc problem: No.1668
No.1668.Maximum Repeating Substring
1 parent ce2c410 commit a52ac55

File tree

5 files changed

+270
-0
lines changed

5 files changed

+270
-0
lines changed

solution/1600-1699/1668.Maximum Repeating Substring/README.md

+95
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,101 @@
6868

6969
```
7070

71+
### **C++**
72+
73+
```cpp
74+
class Solution {
75+
public:
76+
int minOperations(vector<int>& nums, int x) {
77+
int n = nums.size();
78+
int sum = 0;
79+
for (int num: nums) {
80+
sum += num;
81+
}
82+
83+
int target = sum - x;
84+
int res = -1;
85+
int l = 0;
86+
int r = 0;
87+
sum = 0;
88+
while (r < n) {
89+
sum += nums[r++];
90+
while (sum > target && l < n) {
91+
sum -= nums[l++];
92+
}
93+
if (sum == target) {
94+
res = max(res, r - l);
95+
}
96+
}
97+
98+
if (res == -1) {
99+
return res;
100+
}
101+
return n - res;
102+
}
103+
};
104+
```
105+
106+
### **TypeScript**
107+
108+
```ts
109+
function minOperations(nums: number[], x: number): number {
110+
const n = nums.length;
111+
const target = nums.reduce((r, v) => r + v) - x;
112+
113+
let l = 0;
114+
let r = 0;
115+
let sum = 0;
116+
let max = -1;
117+
while (r < n) {
118+
sum += nums[r++];
119+
while (sum > target && l < r) {
120+
sum -= nums[l++];
121+
}
122+
123+
if (sum === target) {
124+
max = Math.max(max, r - l);
125+
}
126+
}
127+
128+
if (max === -1) {
129+
return max;
130+
}
131+
return n - max;
132+
}
133+
```
134+
135+
### **Rust**
136+
137+
```rust
138+
impl Solution {
139+
pub fn min_operations(nums: Vec<i32>, x: i32) -> i32 {
140+
let n = nums.len();
141+
let target = nums.iter().sum::<i32>() - x;
142+
143+
let (mut l, mut r) = (0, 0);
144+
let (mut sum, mut max) = (0, -1);
145+
while r < n {
146+
sum += nums[r];
147+
r += 1;
148+
while sum > target && l < r {
149+
sum -= nums[l];
150+
l += 1;
151+
}
152+
153+
if sum == target {
154+
max = max.max((r - l) as i32);
155+
}
156+
}
157+
158+
if max == -1 {
159+
return max;
160+
}
161+
return n as i32 - max;
162+
}
163+
}
164+
```
165+
71166
### **...**
72167

73168
```

solution/1600-1699/1668.Maximum Repeating Substring/README_EN.md

+95
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,101 @@
5858

5959
```
6060

61+
### **C++**
62+
63+
```cpp
64+
class Solution {
65+
public:
66+
int minOperations(vector<int>& nums, int x) {
67+
int n = nums.size();
68+
int sum = 0;
69+
for (int num: nums) {
70+
sum += num;
71+
}
72+
73+
int target = sum - x;
74+
int res = -1;
75+
int l = 0;
76+
int r = 0;
77+
sum = 0;
78+
while (r < n) {
79+
sum += nums[r++];
80+
while (sum > target && l < n) {
81+
sum -= nums[l++];
82+
}
83+
if (sum == target) {
84+
res = max(res, r - l);
85+
}
86+
}
87+
88+
if (res == -1) {
89+
return res;
90+
}
91+
return n - res;
92+
}
93+
};
94+
```
95+
96+
### **TypeScript**
97+
98+
```ts
99+
function minOperations(nums: number[], x: number): number {
100+
const n = nums.length;
101+
const target = nums.reduce((r, v) => r + v) - x;
102+
103+
let l = 0;
104+
let r = 0;
105+
let sum = 0;
106+
let max = -1;
107+
while (r < n) {
108+
sum += nums[r++];
109+
while (sum > target && l < r) {
110+
sum -= nums[l++];
111+
}
112+
113+
if (sum === target) {
114+
max = Math.max(max, r - l);
115+
}
116+
}
117+
118+
if (max === -1) {
119+
return max;
120+
}
121+
return n - max;
122+
}
123+
```
124+
125+
### **Rust**
126+
127+
```rust
128+
impl Solution {
129+
pub fn min_operations(nums: Vec<i32>, x: i32) -> i32 {
130+
let n = nums.len();
131+
let target = nums.iter().sum::<i32>() - x;
132+
133+
let (mut l, mut r) = (0, 0);
134+
let (mut sum, mut max) = (0, -1);
135+
while r < n {
136+
sum += nums[r];
137+
r += 1;
138+
while sum > target && l < r {
139+
sum -= nums[l];
140+
l += 1;
141+
}
142+
143+
if sum == target {
144+
max = max.max((r - l) as i32);
145+
}
146+
}
147+
148+
if max == -1 {
149+
return max;
150+
}
151+
return n as i32 - max;
152+
}
153+
}
154+
```
155+
61156
### **...**
62157

63158
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public:
3+
int minOperations(vector<int>& nums, int x) {
4+
int n = nums.size();
5+
int sum = 0;
6+
for (int num: nums) {
7+
sum += num;
8+
}
9+
10+
int target = sum - x;
11+
int res = -1;
12+
int l = 0;
13+
int r = 0;
14+
sum = 0;
15+
while (r < n) {
16+
sum += nums[r++];
17+
while (sum > target && l < n) {
18+
sum -= nums[l++];
19+
}
20+
if (sum == target) {
21+
res = max(res, r - l);
22+
}
23+
}
24+
25+
if (res == -1) {
26+
return res;
27+
}
28+
return n - res;
29+
}
30+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
impl Solution {
2+
pub fn min_operations(nums: Vec<i32>, x: i32) -> i32 {
3+
let n = nums.len();
4+
let target = nums.iter().sum::<i32>() - x;
5+
6+
let (mut l, mut r) = (0, 0);
7+
let (mut sum, mut max) = (0, -1);
8+
while r < n {
9+
sum += nums[r];
10+
r += 1;
11+
while sum > target && l < r {
12+
sum -= nums[l];
13+
l += 1;
14+
}
15+
16+
if sum == target {
17+
max = max.max((r - l) as i32);
18+
}
19+
}
20+
21+
if max == -1 {
22+
return max;
23+
}
24+
return n as i32 - max;
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function minOperations(nums: number[], x: number): number {
2+
const n = nums.length;
3+
const target = nums.reduce((r, v) => r + v) - x;
4+
5+
let l = 0;
6+
let r = 0;
7+
let sum = 0;
8+
let max = -1;
9+
while (r < n) {
10+
sum += nums[r++];
11+
while (sum > target && l < r) {
12+
sum -= nums[l++];
13+
}
14+
15+
if (sum === target) {
16+
max = Math.max(max, r - l);
17+
}
18+
}
19+
20+
if (max === -1) {
21+
return max;
22+
}
23+
return n - max;
24+
}

0 commit comments

Comments
 (0)