Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bf27a51

Browse files
feat: add solutions to lc problems: No.0152,2213
- No.0152.Maximum Product Subarray - No.2213.Longest Substring of One Repeating Character
1 parent d4a75dc commit bf27a51

File tree

6 files changed

+304
-2
lines changed

6 files changed

+304
-2
lines changed

‎solution/0100-0199/0152.Maximum Product Subarray/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,25 @@ func min(a, b int) int {
176176
}
177177
```
178178

179+
### **Rust**
180+
181+
```rust
182+
impl Solution {
183+
pub fn max_product(nums: Vec<i32>) -> i32 {
184+
let mut min = nums[0];
185+
let mut max = nums[0];
186+
let mut res = nums[0];
187+
for &num in nums.iter().skip(1) {
188+
let (pre_min, pre_max) = (min, max);
189+
min = num.min(num * pre_min).min(num * pre_max);
190+
max = num.max(num * pre_min).max(num * pre_max);
191+
res = res.max(max);
192+
}
193+
res
194+
}
195+
}
196+
```
197+
179198
### **...**
180199

181200
```

‎solution/0100-0199/0152.Maximum Product Subarray/README_EN.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,25 @@ func min(a, b int) int {
156156
}
157157
```
158158

159+
### **Rust**
160+
161+
```rust
162+
impl Solution {
163+
pub fn max_product(nums: Vec<i32>) -> i32 {
164+
let mut min = nums[0];
165+
let mut max = nums[0];
166+
let mut res = nums[0];
167+
for &num in nums.iter().skip(1) {
168+
let (pre_min, pre_max) = (min, max);
169+
min = num.min(num * pre_min).min(num * pre_max);
170+
max = num.max(num * pre_min).max(num * pre_max);
171+
res = res.max(max);
172+
}
173+
res
174+
}
175+
}
176+
```
177+
159178
### **...**
160179

161180
```
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
impl Solution {
2+
pub fn max_product(nums: Vec<i32>) -> i32 {
3+
let mut min = nums[0];
4+
let mut max = nums[0];
5+
let mut res = nums[0];
6+
for &num in nums.iter().skip(1) {
7+
let (pre_min, pre_max) = (min, max);
8+
min = num.min(num * pre_min).min(num * pre_max);
9+
max = num.max(num * pre_min).max(num * pre_max);
10+
res = res.max(max);
11+
}
12+
res
13+
}
14+
}

‎solution/2200-2299/2213.Longest Substring of One Repeating Character/README.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,91 @@ public:
390390
};
391391
```
392392
393+
### **Go**
394+
395+
```go
396+
type segmentTree struct {
397+
str []byte
398+
mx []int
399+
lmx []int
400+
rmx []int
401+
}
402+
403+
func newSegmentTree(s string) *segmentTree {
404+
n := len(s)
405+
t := &segmentTree{
406+
str: []byte(s),
407+
mx: make([]int, n<<2),
408+
lmx: make([]int, n<<2),
409+
rmx: make([]int, n<<2),
410+
}
411+
t.build(0, 0, n-1)
412+
return t
413+
}
414+
415+
func (t *segmentTree) build(x, l, r int) {
416+
if l == r {
417+
t.lmx[x] = 1
418+
t.rmx[x] = 1
419+
t.mx[x] = 1
420+
return
421+
}
422+
m := int(uint(l+r) >> 1)
423+
t.build(x*2+1, l, m)
424+
t.build(x*2+2, m+1, r)
425+
t.pushup(x, l, m, r)
426+
}
427+
428+
func (t *segmentTree) pushup(x, l, m, r int) {
429+
lch, rch := x*2+1, x*2+2
430+
t.lmx[x] = t.lmx[lch]
431+
t.rmx[x] = t.rmx[rch]
432+
t.mx[x] = max(t.mx[lch], t.mx[rch])
433+
// can be merged
434+
if t.str[m] == t.str[m+1] {
435+
if t.lmx[lch] == m-l+1 {
436+
t.lmx[x] += t.lmx[rch]
437+
}
438+
if t.rmx[rch] == r-(m+1)-1 {
439+
t.rmx[x] += t.rmx[lch]
440+
}
441+
t.mx[x] = max(t.mx[x], t.rmx[lch]+t.lmx[rch])
442+
}
443+
}
444+
445+
func (t *segmentTree) update(x, l, r, pos int, val byte) {
446+
if l == r {
447+
t.str[pos] = val
448+
return
449+
}
450+
m := int(uint(l+r) >> 1)
451+
if pos <= m {
452+
t.update(x*2+1, l, m, pos, val)
453+
} else {
454+
t.update(x*2+2, m+1, r, pos, val)
455+
}
456+
t.pushup(x, l, m, r)
457+
}
458+
459+
func max(x, y int) int {
460+
if x > y {
461+
return x
462+
}
463+
return y
464+
}
465+
466+
func longestRepeating(s string, queryCharacters string, queryIndices []int) []int {
467+
ans := make([]int, len(queryCharacters))
468+
t := newSegmentTree(s)
469+
n := len(s)
470+
for i, c := range queryCharacters {
471+
t.update(0, 0, n-1, queryIndices[i], byte(c))
472+
ans[i] = t.mx[0]
473+
}
474+
return ans
475+
}
476+
```
477+
393478
### **TypeScript**
394479

395480
```ts

‎solution/2200-2299/2213.Longest Substring of One Repeating Character/README_EN.md

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
<pre>
1717
<strong>Input:</strong> s = &quot;babacc&quot;, queryCharacters = &quot;bcb&quot;, queryIndices = [1,3,3]
1818
<strong>Output:</strong> [3,3,4]
19-
<strong>Explanation:</strong>
19+
<strong>Explanation:</strong>
2020
- 1<sup>st</sup> query updates s = &quot;<u>b<strong>b</strong>b</u>acc&quot;. The longest substring consisting of one repeating character is &quot;bbb&quot; with length 3.
21-
- 2<sup>nd</sup> query updates s = &quot;bbb<u><strong>c</strong>cc</u>&quot;.
21+
- 2<sup>nd</sup> query updates s = &quot;bbb<u><strong>c</strong>cc</u>&quot;.
2222
The longest substring consisting of one repeating character can be &quot;bbb&quot; or &quot;ccc&quot; with length 3.
2323
- 3<sup>rd</sup> query updates s = &quot;<u>bbb<strong>b</strong></u>cc&quot;. The longest substring consisting of one repeating character is &quot;bbbb&quot; with length 4.
2424
Thus, we return [3,3,4].
@@ -367,6 +367,91 @@ public:
367367
};
368368
```
369369
370+
### **Go**
371+
372+
```go
373+
type segmentTree struct {
374+
str []byte
375+
mx []int
376+
lmx []int
377+
rmx []int
378+
}
379+
380+
func newSegmentTree(s string) *segmentTree {
381+
n := len(s)
382+
t := &segmentTree{
383+
str: []byte(s),
384+
mx: make([]int, n<<2),
385+
lmx: make([]int, n<<2),
386+
rmx: make([]int, n<<2),
387+
}
388+
t.build(0, 0, n-1)
389+
return t
390+
}
391+
392+
func (t *segmentTree) build(x, l, r int) {
393+
if l == r {
394+
t.lmx[x] = 1
395+
t.rmx[x] = 1
396+
t.mx[x] = 1
397+
return
398+
}
399+
m := int(uint(l+r) >> 1)
400+
t.build(x*2+1, l, m)
401+
t.build(x*2+2, m+1, r)
402+
t.pushup(x, l, m, r)
403+
}
404+
405+
func (t *segmentTree) pushup(x, l, m, r int) {
406+
lch, rch := x*2+1, x*2+2
407+
t.lmx[x] = t.lmx[lch]
408+
t.rmx[x] = t.rmx[rch]
409+
t.mx[x] = max(t.mx[lch], t.mx[rch])
410+
// can be merged
411+
if t.str[m] == t.str[m+1] {
412+
if t.lmx[lch] == m-l+1 {
413+
t.lmx[x] += t.lmx[rch]
414+
}
415+
if t.rmx[rch] == r-(m+1)-1 {
416+
t.rmx[x] += t.rmx[lch]
417+
}
418+
t.mx[x] = max(t.mx[x], t.rmx[lch]+t.lmx[rch])
419+
}
420+
}
421+
422+
func (t *segmentTree) update(x, l, r, pos int, val byte) {
423+
if l == r {
424+
t.str[pos] = val
425+
return
426+
}
427+
m := int(uint(l+r) >> 1)
428+
if pos <= m {
429+
t.update(x*2+1, l, m, pos, val)
430+
} else {
431+
t.update(x*2+2, m+1, r, pos, val)
432+
}
433+
t.pushup(x, l, m, r)
434+
}
435+
436+
func max(x, y int) int {
437+
if x > y {
438+
return x
439+
}
440+
return y
441+
}
442+
443+
func longestRepeating(s string, queryCharacters string, queryIndices []int) []int {
444+
ans := make([]int, len(queryCharacters))
445+
t := newSegmentTree(s)
446+
n := len(s)
447+
for i, c := range queryCharacters {
448+
t.update(0, 0, n-1, queryIndices[i], byte(c))
449+
ans[i] = t.mx[0]
450+
}
451+
return ans
452+
}
453+
```
454+
370455
### **TypeScript**
371456

372457
```ts
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
type segmentTree struct {
2+
str []byte
3+
mx []int
4+
lmx []int
5+
rmx []int
6+
}
7+
8+
func newSegmentTree(s string) *segmentTree {
9+
n := len(s)
10+
t := &segmentTree{
11+
str: []byte(s),
12+
mx: make([]int, n<<2),
13+
lmx: make([]int, n<<2),
14+
rmx: make([]int, n<<2),
15+
}
16+
t.build(0, 0, n-1)
17+
return t
18+
}
19+
20+
func (t *segmentTree) build(x, l, r int) {
21+
if l == r {
22+
t.lmx[x] = 1
23+
t.rmx[x] = 1
24+
t.mx[x] = 1
25+
return
26+
}
27+
m := int(uint(l+r) >> 1)
28+
t.build(x*2+1, l, m)
29+
t.build(x*2+2, m+1, r)
30+
t.pushup(x, l, m, r)
31+
}
32+
33+
func (t *segmentTree) pushup(x, l, m, r int) {
34+
lch, rch := x*2+1, x*2+2
35+
t.lmx[x] = t.lmx[lch]
36+
t.rmx[x] = t.rmx[rch]
37+
t.mx[x] = max(t.mx[lch], t.mx[rch])
38+
// can be merged
39+
if t.str[m] == t.str[m+1] {
40+
if t.lmx[lch] == m-l+1 {
41+
t.lmx[x] += t.lmx[rch]
42+
}
43+
if t.rmx[rch] == r-(m+1)-1 {
44+
t.rmx[x] += t.rmx[lch]
45+
}
46+
t.mx[x] = max(t.mx[x], t.rmx[lch]+t.lmx[rch])
47+
}
48+
}
49+
50+
func (t *segmentTree) update(x, l, r, pos int, val byte) {
51+
if l == r {
52+
t.str[pos] = val
53+
return
54+
}
55+
m := int(uint(l+r) >> 1)
56+
if pos <= m {
57+
t.update(x*2+1, l, m, pos, val)
58+
} else {
59+
t.update(x*2+2, m+1, r, pos, val)
60+
}
61+
t.pushup(x, l, m, r)
62+
}
63+
64+
func max(x, y int) int {
65+
if x > y {
66+
return x
67+
}
68+
return y
69+
}
70+
71+
func longestRepeating(s string, queryCharacters string, queryIndices []int) []int {
72+
ans := make([]int, len(queryCharacters))
73+
t := newSegmentTree(s)
74+
n := len(s)
75+
for i, c := range queryCharacters {
76+
t.update(0, 0, n-1, queryIndices[i], byte(c))
77+
ans[i] = t.mx[0]
78+
}
79+
return ans
80+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /