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 7526455

Browse files
feat: add golang solution to lc problem: No.0699
No.0699.Falling Squares
1 parent 8cce607 commit 7526455

File tree

3 files changed

+316
-0
lines changed

3 files changed

+316
-0
lines changed

‎solution/0600-0699/0699.Falling Squares/README.md

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,113 @@ public:
393393
};
394394
```
395395
396+
### **Go**
397+
398+
```go
399+
type node struct {
400+
left *node
401+
right *node
402+
l, mid, r int
403+
v, add int
404+
}
405+
406+
func newNode(l, r int) *node {
407+
return &node{
408+
l: l,
409+
r: r,
410+
mid: int(uint(l+r) >> 1),
411+
}
412+
}
413+
414+
func max(x, y int) int {
415+
if x > y {
416+
return x
417+
}
418+
return y
419+
}
420+
421+
type segmentTree struct {
422+
root *node
423+
}
424+
425+
func newSegmentTree() *segmentTree {
426+
return &segmentTree{
427+
root: newNode(1, 1e9),
428+
}
429+
}
430+
431+
func (t *segmentTree) modify(l, r, v int, n *node) {
432+
if l > r {
433+
return
434+
}
435+
if n.l >= l && n.r <= r {
436+
n.v = v
437+
n.add = v
438+
return
439+
}
440+
t.pushdown(n)
441+
if l <= n.mid {
442+
t.modify(l, r, v, n.left)
443+
}
444+
if r > n.mid {
445+
t.modify(l, r, v, n.right)
446+
}
447+
t.pushup(n)
448+
}
449+
450+
func (t *segmentTree) query(l, r int, n *node) int {
451+
if l > r {
452+
return 0
453+
}
454+
if n.l >= l && n.r <= r {
455+
return n.v
456+
}
457+
t.pushdown(n)
458+
v := 0
459+
if l <= n.mid {
460+
v = max(v, t.query(l, r, n.left))
461+
}
462+
if r > n.mid {
463+
v = max(v, t.query(l, r, n.right))
464+
}
465+
return v
466+
}
467+
468+
func (t *segmentTree) pushup(n *node) {
469+
n.v = max(n.left.v, n.right.v)
470+
}
471+
472+
func (t *segmentTree) pushdown(n *node) {
473+
if n.left == nil {
474+
n.left = newNode(n.l, n.mid)
475+
}
476+
if n.right == nil {
477+
n.right = newNode(n.mid+1, n.r)
478+
}
479+
if n.add != 0 {
480+
n.left.add = n.add
481+
n.right.add = n.add
482+
n.left.v = n.add
483+
n.right.v = n.add
484+
n.add = 0
485+
}
486+
}
487+
488+
func fallingSquares(positions [][]int) []int {
489+
ans := make([]int, len(positions))
490+
t := newSegmentTree()
491+
mx := 0
492+
for i, p := range positions {
493+
l, w, r := p[0], p[1], p[0]+p[1]-1
494+
h := t.query(l, r, t.root) + w
495+
mx = max(mx, h)
496+
ans[i] = mx
497+
t.modify(l, r, h, t.root)
498+
}
499+
return ans
500+
}
501+
```
502+
396503
### **...**
397504

398505
```

‎solution/0600-0699/0699.Falling Squares/README_EN.md

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,113 @@ public:
344344
};
345345
```
346346
347+
### **Go**
348+
349+
```go
350+
type node struct {
351+
left *node
352+
right *node
353+
l, mid, r int
354+
v, add int
355+
}
356+
357+
func newNode(l, r int) *node {
358+
return &node{
359+
l: l,
360+
r: r,
361+
mid: int(uint(l+r) >> 1),
362+
}
363+
}
364+
365+
func max(x, y int) int {
366+
if x > y {
367+
return x
368+
}
369+
return y
370+
}
371+
372+
type segmentTree struct {
373+
root *node
374+
}
375+
376+
func newSegmentTree() *segmentTree {
377+
return &segmentTree{
378+
root: newNode(1, 1e9),
379+
}
380+
}
381+
382+
func (t *segmentTree) modify(l, r, v int, n *node) {
383+
if l > r {
384+
return
385+
}
386+
if n.l >= l && n.r <= r {
387+
n.v = v
388+
n.add = v
389+
return
390+
}
391+
t.pushdown(n)
392+
if l <= n.mid {
393+
t.modify(l, r, v, n.left)
394+
}
395+
if r > n.mid {
396+
t.modify(l, r, v, n.right)
397+
}
398+
t.pushup(n)
399+
}
400+
401+
func (t *segmentTree) query(l, r int, n *node) int {
402+
if l > r {
403+
return 0
404+
}
405+
if n.l >= l && n.r <= r {
406+
return n.v
407+
}
408+
t.pushdown(n)
409+
v := 0
410+
if l <= n.mid {
411+
v = max(v, t.query(l, r, n.left))
412+
}
413+
if r > n.mid {
414+
v = max(v, t.query(l, r, n.right))
415+
}
416+
return v
417+
}
418+
419+
func (t *segmentTree) pushup(n *node) {
420+
n.v = max(n.left.v, n.right.v)
421+
}
422+
423+
func (t *segmentTree) pushdown(n *node) {
424+
if n.left == nil {
425+
n.left = newNode(n.l, n.mid)
426+
}
427+
if n.right == nil {
428+
n.right = newNode(n.mid+1, n.r)
429+
}
430+
if n.add != 0 {
431+
n.left.add = n.add
432+
n.right.add = n.add
433+
n.left.v = n.add
434+
n.right.v = n.add
435+
n.add = 0
436+
}
437+
}
438+
439+
func fallingSquares(positions [][]int) []int {
440+
ans := make([]int, len(positions))
441+
t := newSegmentTree()
442+
mx := 0
443+
for i, p := range positions {
444+
l, w, r := p[0], p[1], p[0]+p[1]-1
445+
h := t.query(l, r, t.root) + w
446+
mx = max(mx, h)
447+
ans[i] = mx
448+
t.modify(l, r, h, t.root)
449+
}
450+
return ans
451+
}
452+
```
453+
347454
### **...**
348455

349456
```
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
type node struct {
2+
left *node
3+
right *node
4+
l, mid, r int
5+
v, add int
6+
}
7+
8+
func newNode(l, r int) *node {
9+
return &node{
10+
l: l,
11+
r: r,
12+
mid: int(uint(l+r) >> 1),
13+
}
14+
}
15+
16+
func max(x, y int) int {
17+
if x > y {
18+
return x
19+
}
20+
return y
21+
}
22+
23+
type segmentTree struct {
24+
root *node
25+
}
26+
27+
func newSegmentTree() *segmentTree {
28+
return &segmentTree{
29+
root: newNode(1, 1e9),
30+
}
31+
}
32+
33+
func (t *segmentTree) modify(l, r, v int, n *node) {
34+
if l > r {
35+
return
36+
}
37+
if n.l >= l && n.r <= r {
38+
n.v = v
39+
n.add = v
40+
return
41+
}
42+
t.pushdown(n)
43+
if l <= n.mid {
44+
t.modify(l, r, v, n.left)
45+
}
46+
if r > n.mid {
47+
t.modify(l, r, v, n.right)
48+
}
49+
t.pushup(n)
50+
}
51+
52+
func (t *segmentTree) query(l, r int, n *node) int {
53+
if l > r {
54+
return 0
55+
}
56+
if n.l >= l && n.r <= r {
57+
return n.v
58+
}
59+
t.pushdown(n)
60+
v := 0
61+
if l <= n.mid {
62+
v = max(v, t.query(l, r, n.left))
63+
}
64+
if r > n.mid {
65+
v = max(v, t.query(l, r, n.right))
66+
}
67+
return v
68+
}
69+
70+
func (t *segmentTree) pushup(n *node) {
71+
n.v = max(n.left.v, n.right.v)
72+
}
73+
74+
func (t *segmentTree) pushdown(n *node) {
75+
if n.left == nil {
76+
n.left = newNode(n.l, n.mid)
77+
}
78+
if n.right == nil {
79+
n.right = newNode(n.mid+1, n.r)
80+
}
81+
if n.add != 0 {
82+
n.left.add = n.add
83+
n.right.add = n.add
84+
n.left.v = n.add
85+
n.right.v = n.add
86+
n.add = 0
87+
}
88+
}
89+
90+
func fallingSquares(positions [][]int) []int {
91+
ans := make([]int, len(positions))
92+
t := newSegmentTree()
93+
mx := 0
94+
for i, p := range positions {
95+
l, w, r := p[0], p[1], p[0]+p[1]-1
96+
h := t.query(l, r, t.root) + w
97+
mx = max(mx, h)
98+
ans[i] = mx
99+
t.modify(l, r, h, t.root)
100+
}
101+
return ans
102+
}

0 commit comments

Comments
(0)

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