Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit f175d3e

Browse files
committed
1031 done
1 parent ce307f6 commit f175d3e

File tree

2 files changed

+12
-89
lines changed

2 files changed

+12
-89
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,21 @@
11
package problem1031
22

3-
import "sort"
3+
// ref: https://leetcode.com/problems/maximum-sum-of-two-non-overlapping-subarrays/discuss/278251/JavaC%2B%2BPython-O(N)Time-O(1)-Space
44

55
func maxSumTwoNoOverlap(A []int, L int, M int) int {
6-
ls, ms := sums(A, L), sums(A, M)
7-
lSize, mSize := len(ls), len(ms)
8-
res := 0
9-
for i := 0; i < lSize; i++ {
10-
if ls[i].sum+ms[0].sum <= res {
11-
break
12-
}
13-
j := 0
14-
for j < mSize && ls[i].isOverlapping(ms[j]) {
15-
j++
16-
}
17-
if j < mSize {
18-
res = max(res, ls[i].sum+ms[j].sum)
19-
}
6+
for i := 1; i < len(A); i++ {
7+
A[i] += A[i-1]
208
}
21-
return res
22-
}
23-
24-
type entry struct {
25-
sum, left, right int
26-
}
27-
28-
func (e *entry) isOverlapping(other *entry) bool {
29-
return max(e.left, other.left) <= min(e.right, other.right)
30-
}
31-
32-
func sums(A []int, l int) []*entry {
33-
n := len(A)
34-
res := make([]*entry, n-l+1)
35-
sum := 0
36-
for i := 0; i < l; i++ {
37-
sum += A[i]
9+
// assume original A is A'
10+
// now, A[i] = sum(A'[:i+1])
11+
res, lMax, mMax := A[L+M-1], A[L-1], A[M-1]
12+
for i := L + M; i < len(A); i++ {
13+
// lMax is max sum of contiguous L elements before the last M elements.
14+
lMax = max(lMax, A[i-M]-A[i-L-M])
15+
// mMax is max sum of contiguous M elements before the last L elements.
16+
mMax = max(mMax, A[i-L]-A[i-L-M])
17+
res = max(res, max(lMax+A[i]-A[i-M], mMax+A[i]-A[i-L]))
3818
}
39-
for i := l; i < n; i++ {
40-
res[i-l] = &entry{
41-
sum: sum,
42-
left: i - l,
43-
right: i - 1,
44-
}
45-
sum += A[i] - A[i-l]
46-
}
47-
res[n-l] = &entry{
48-
sum: sum,
49-
left: n - l,
50-
right: n - 1,
51-
}
52-
sort.Slice(res, func(i int, j int) bool {
53-
return res[i].sum > res[j].sum
54-
})
5519
return res
5620
}
5721

@@ -61,10 +25,3 @@ func max(a, b int) int {
6125
}
6226
return b
6327
}
64-
65-
func min(a, b int) int {
66-
if a < b {
67-
return a
68-
}
69-
return b
70-
}

Algorithms/1031.maximum-sum-of-two-non-overlapping-subarrays/maximum-sum-of-two-non-overlapping-subarrays_test.go

-34
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package problem1031
22

33
import (
4-
"reflect"
54
"testing"
65

76
"github.com/stretchr/testify/assert"
@@ -68,36 +67,3 @@ func Benchmark_maxSumTwoNoOverlap(b *testing.B) {
6867
}
6968
}
7069
}
71-
72-
func Test_sums(t *testing.T) {
73-
type args struct {
74-
A []int
75-
l int
76-
}
77-
tests := []struct {
78-
name string
79-
args args
80-
want []*entry
81-
}{
82-
{
83-
"test",
84-
args{
85-
A: []int{0, 1, 2, 3},
86-
l: 3,
87-
},
88-
[]*entry{
89-
&entry{6, 1, 3},
90-
&entry{3, 0, 2},
91-
},
92-
},
93-
94-
// TODO: Add test cases.
95-
}
96-
for _, tt := range tests {
97-
t.Run(tt.name, func(t *testing.T) {
98-
if got := sums(tt.args.A, tt.args.l); !reflect.DeepEqual(got, tt.want) {
99-
t.Errorf("sums() = %v, want %v", got, tt.want)
100-
}
101-
})
102-
}
103-
}

0 commit comments

Comments
 (0)