Skip to content

Commit 5b4ea68

Browse files
authored
feat: add solutions to lc problem: No.3691 (#5250)
1 parent 2c2fbe7 commit 5b4ea68

14 files changed

Lines changed: 1103 additions & 18 deletions

File tree

solution/3600-3699/3691.Maximum Total Subarray Value II/README.md

Lines changed: 370 additions & 4 deletions
Large diffs are not rendered by default.

solution/3600-3699/3691.Maximum Total Subarray Value II/README_EN.md

Lines changed: 370 additions & 4 deletions
Large diffs are not rendered by default.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
class SparseTableRMQ {
2+
public:
3+
int n;
4+
int maxLog;
5+
vector<vector<int>> fMax;
6+
vector<vector<int>> fMin;
7+
vector<int> lg;
8+
9+
SparseTableRMQ(const vector<int>& data) {
10+
n = data.size();
11+
maxLog = 32 - __builtin_clz(n) + 1;
12+
fMax.assign(n, vector<int>(maxLog, 0));
13+
fMin.assign(n, vector<int>(maxLog, 0));
14+
lg.assign(n + 1, 0);
15+
16+
for (int i = 2; i <= n; i++) {
17+
lg[i] = lg[i >> 1] + 1;
18+
}
19+
20+
for (int i = 0; i < n; i++) {
21+
fMax[i][0] = data[i];
22+
fMin[i][0] = data[i];
23+
}
24+
25+
for (int j = 1; j < maxLog; j++) {
26+
for (int i = 0; i <= n - (1 << j); i++) {
27+
fMax[i][j] = max(fMax[i][j - 1], fMax[i + (1 << (j - 1))][j - 1]);
28+
fMin[i][j] = min(fMin[i][j - 1], fMin[i + (1 << (j - 1))][j - 1]);
29+
}
30+
}
31+
}
32+
33+
int queryMax(int l, int r) {
34+
int k = lg[r - l + 1];
35+
return max(fMax[l][k], fMax[r - (1 << k) + 1][k]);
36+
}
37+
38+
int queryMin(int l, int r) {
39+
int k = lg[r - l + 1];
40+
return min(fMin[l][k], fMin[r - (1 << k) + 1][k]);
41+
}
42+
};
43+
44+
class Solution {
45+
public:
46+
long long maxTotalValue(vector<int>& nums, int k) {
47+
int n = nums.size();
48+
SparseTableRMQ st(nums);
49+
auto cmp = [](const tuple<long long, int, int>& a, const tuple<long long, int, int>& b) {
50+
return get<0>(a) < get<0>(b);
51+
};
52+
priority_queue<tuple<long long, int, int>, vector<tuple<long long, int, int>>, decltype(cmp)> pq(cmp);
53+
54+
for (int l = 0; l < n; l++) {
55+
long long val = st.queryMax(l, n - 1) - st.queryMin(l, n - 1);
56+
pq.push({val, l, n - 1});
57+
}
58+
59+
long long ans = 0;
60+
for (int i = 0; i < k; i++) {
61+
auto curr = pq.top();
62+
pq.pop();
63+
long long val = get<0>(curr);
64+
int l = get<1>(curr);
65+
int r = get<2>(curr);
66+
ans += val;
67+
if (r > l) {
68+
long long nextVal = st.queryMax(l, r - 1) - st.queryMin(l, r - 1);
69+
pq.push({nextVal, l, r - 1});
70+
}
71+
}
72+
return ans;
73+
}
74+
};
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
type SparseTableRMQ struct {
2+
n int
3+
maxLog int
4+
fMax [][]int
5+
fMin [][]int
6+
lg []int
7+
}
8+
9+
func NewSparseTableRMQ(data []int) *SparseTableRMQ {
10+
n := len(data)
11+
maxLog := bits.Len(uint(n)) + 1
12+
fMax := make([][]int, n)
13+
fMin := make([][]int, n)
14+
for i := range fMax {
15+
fMax[i] = make([]int, maxLog)
16+
fMin[i] = make([]int, maxLog)
17+
}
18+
lg := make([]int, n+1)
19+
20+
for i := 2; i <= n; i++ {
21+
lg[i] = lg[i>>1] + 1
22+
}
23+
24+
for i := 0; i < n; i++ {
25+
fMax[i][0] = data[i]
26+
fMin[i][0] = data[i]
27+
}
28+
29+
for j := 1; j < maxLog; j++ {
30+
for i := 0; i <= n-(1<<j); i++ {
31+
fMax[i][j] = max(fMax[i][j-1], fMax[i+(1<<(j-1))][j-1])
32+
fMin[i][j] = min(fMin[i][j-1], fMin[i+(1<<(j-1))][j-1])
33+
}
34+
}
35+
36+
return &SparseTableRMQ{n: n, maxLog: maxLog, fMax: fMax, fMin: fMin, lg: lg}
37+
}
38+
39+
func (st *SparseTableRMQ) queryMax(l, r int) int {
40+
k := st.lg[r-l+1]
41+
return max(st.fMax[l][k], st.fMax[r-(1<<k)+1][k])
42+
}
43+
44+
func (st *SparseTableRMQ) queryMin(l, r int) int {
45+
k := st.lg[r-l+1]
46+
return min(st.fMin[l][k], st.fMin[r-(1<<k)+1][k])
47+
}
48+
49+
type Item struct {
50+
val int64
51+
l, r int
52+
}
53+
type PriorityQueue []*Item
54+
55+
func (pq PriorityQueue) Len() int { return len(pq) }
56+
func (pq PriorityQueue) Less(i, j int) bool { return pq[i].val > pq[j].val }
57+
func (pq PriorityQueue) Swap(i, j int) { pq[i], pq[j] = pq[j], pq[i] }
58+
func (pq *PriorityQueue) Push(x any) { *pq = append(*pq, x.(*Item)) }
59+
func (pq *PriorityQueue) Pop() any {
60+
old := *pq
61+
n := len(old)
62+
item := old[n-1]
63+
*pq = old[0 : n-1]
64+
return item
65+
}
66+
67+
func maxTotalValue(nums []int, k int) int64 {
68+
n := len(nums)
69+
st := NewSparseTableRMQ(nums)
70+
pq := &PriorityQueue{}
71+
heap.Init(pq)
72+
73+
for l := 0; l < n; l++ {
74+
val := int64(st.queryMax(l, n-1) - st.queryMin(l, n-1))
75+
heap.Push(pq, &Item{val: val, l: l, r: n - 1})
76+
}
77+
78+
var ans int64 = 0
79+
for i := 0; i < k; i++ {
80+
curr := heap.Pop(pq).(*Item)
81+
ans += curr.val
82+
if curr.r > curr.l {
83+
nextVal := int64(st.queryMax(curr.l, curr.r-1) - st.queryMin(curr.l, curr.r-1))
84+
heap.Push(pq, &Item{val: nextVal, l: curr.l, r: curr.r - 1})
85+
}
86+
}
87+
return ans
88+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
class SparseTableRMQ {
2+
int n;
3+
int maxLog;
4+
int[][] fMax;
5+
int[][] fMin;
6+
int[] lg;
7+
8+
public SparseTableRMQ(int[] data) {
9+
this.n = data.length;
10+
this.maxLog = 32 - Integer.numberOfLeadingZeros(n) + 1;
11+
this.fMax = new int[n][maxLog];
12+
this.fMin = new int[n][maxLog];
13+
this.lg = new int[n + 1];
14+
15+
for (int i = 2; i <= n; i++) {
16+
this.lg[i] = this.lg[i >> 1] + 1;
17+
}
18+
19+
for (int i = 0; i < n; i++) {
20+
this.fMax[i][0] = data[i];
21+
this.fMin[i][0] = data[i];
22+
}
23+
24+
for (int j = 1; j < maxLog; j++) {
25+
for (int i = 0; i <= n - (1 << j); i++) {
26+
this.fMax[i][j]
27+
= Math.max(this.fMax[i][j - 1], this.fMax[i + (1 << (j - 1))][j - 1]);
28+
this.fMin[i][j]
29+
= Math.min(this.fMin[i][j - 1], this.fMin[i + (1 << (j - 1))][j - 1]);
30+
}
31+
}
32+
}
33+
34+
public int queryMax(int l, int r) {
35+
int k = lg[r - l + 1];
36+
return Math.max(fMax[l][k], fMax[r - (1 << k) + 1][k]);
37+
}
38+
39+
public int queryMin(int l, int r) {
40+
int k = lg[r - l + 1];
41+
return Math.min(fMin[l][k], fMin[r - (1 << k) + 1][k]);
42+
}
43+
}
44+
45+
class Solution {
46+
public long maxTotalValue(int[] nums, int k) {
47+
int n = nums.length;
48+
SparseTableRMQ st = new SparseTableRMQ(nums);
49+
PriorityQueue<long[]> pq = new PriorityQueue<>((a, b) -> Long.compare(b[0], a[0]));
50+
51+
for (int l = 0; l < n; l++) {
52+
long val = st.queryMax(l, n - 1) - st.queryMin(l, n - 1);
53+
pq.offer(new long[] {val, l, n - 1});
54+
}
55+
56+
long ans = 0;
57+
for (int i = 0; i < k; i++) {
58+
long[] curr = pq.poll();
59+
long val = curr[0];
60+
int l = (int) curr[1];
61+
int r = (int) curr[2];
62+
ans += val;
63+
if (r > l) {
64+
long nextVal = st.queryMax(l, r - 1) - st.queryMin(l, r - 1);
65+
pq.offer(new long[] {nextVal, l, r - 1});
66+
}
67+
}
68+
return ans;
69+
}
70+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
class SparseTableRMQ:
2+
def __init__(self, data: List[int]):
3+
self.n = len(data)
4+
self.max_log = self.n.bit_length() + 1
5+
self.f_max = [[0] * self.max_log for _ in range(self.n)]
6+
self.f_min = [[0] * self.max_log for _ in range(self.n)]
7+
8+
self.lg = [0] * (self.n + 1)
9+
for i in range(2, self.n + 1):
10+
self.lg[i] = self.lg[i >> 1] + 1
11+
12+
for i in range(self.n):
13+
self.f_max[i][0] = data[i]
14+
self.f_min[i][0] = data[i]
15+
16+
for j in range(1, self.max_log):
17+
for i in range(self.n - (1 << j) + 1):
18+
self.f_max[i][j] = max(
19+
self.f_max[i][j - 1], self.f_max[i + (1 << (j - 1))][j - 1]
20+
)
21+
self.f_min[i][j] = min(
22+
self.f_min[i][j - 1], self.f_min[i + (1 << (j - 1))][j - 1]
23+
)
24+
25+
def query_max(self, l: int, r: int) -> int:
26+
k = self.lg[r - l + 1]
27+
return max(self.f_max[l][k], self.f_max[r - (1 << k) + 1][k])
28+
29+
def query_min(self, l: int, r: int) -> int:
30+
k = self.lg[r - l + 1]
31+
return min(self.f_min[l][k], self.f_min[r - (1 << k) + 1][k])
32+
33+
34+
class Solution:
35+
def maxTotalValue(self, nums: List[int], k: int) -> int:
36+
n = len(nums)
37+
st = SparseTableRMQ(nums)
38+
pq = []
39+
for l in range(n):
40+
val = st.query_max(l, n - 1) - st.query_min(l, n - 1)
41+
heappush(pq, (-val, l, n - 1))
42+
43+
ans = 0
44+
for _ in range(k):
45+
val, l, r = heappop(pq)
46+
ans += -val
47+
if r > l:
48+
val = st.query_max(l, r - 1) - st.query_min(l, r - 1)
49+
heappush(pq, (-val, l, r - 1))
50+
return ans
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
class SparseTableRMQ {
2+
n: number;
3+
maxLog: number;
4+
fMax: number[][];
5+
fMin: number[][];
6+
lg: number[];
7+
8+
constructor(data: number[]) {
9+
this.n = data.length;
10+
this.maxLog = Math.floor(Math.log2(this.n)) + 2;
11+
this.fMax = Array.from({ length: this.n }, () => Array(this.maxLog).fill(0));
12+
this.fMin = Array.from({ length: this.n }, () => Array(this.maxLog).fill(0));
13+
this.lg = Array(this.n + 1).fill(0);
14+
15+
for (let i = 2; i <= this.n; i++) {
16+
this.lg[i] = this.lg[i >> 1] + 1;
17+
}
18+
19+
for (let i = 0; i < this.n; i++) {
20+
this.fMax[i][0] = data[i];
21+
this.fMin[i][0] = data[i];
22+
}
23+
24+
for (let j = 1; j < this.maxLog; j++) {
25+
for (let i = 0; i <= this.n - (1 << j); i++) {
26+
this.fMax[i][j] = Math.max(
27+
this.fMax[i][j - 1],
28+
this.fMax[i + (1 << (j - 1))][j - 1],
29+
);
30+
this.fMin[i][j] = Math.min(
31+
this.fMin[i][j - 1],
32+
this.fMin[i + (1 << (j - 1))][j - 1],
33+
);
34+
}
35+
}
36+
}
37+
38+
queryMax(l: number, r: number): number {
39+
const k = this.lg[r - l + 1];
40+
return Math.max(this.fMax[l][k], this.fMax[r - (1 << k) + 1][k]);
41+
}
42+
43+
queryMin(l: number, r: number): number {
44+
const k = this.lg[r - l + 1];
45+
return Math.min(this.fMin[l][k], this.fMin[r - (1 << k) + 1][k]);
46+
}
47+
}
48+
49+
function maxTotalValue(nums: number[], k: number): number {
50+
const n = nums.length;
51+
const st = new SparseTableRMQ(nums);
52+
const pq = new PriorityQueue<[number, number, number]>((a, b) => b[0] - a[0]);
53+
54+
for (let l = 0; l < n; l++) {
55+
const val = st.queryMax(l, n - 1) - st.queryMin(l, n - 1);
56+
pq.enqueue([val, l, n - 1]);
57+
}
58+
59+
let ans = 0;
60+
for (let i = 0; i < k; i++) {
61+
if (pq.isEmpty()) break;
62+
const curr = pq.dequeue()!;
63+
const [val, l, r] = curr;
64+
ans += val;
65+
if (r > l) {
66+
const nextVal = st.queryMax(l, r - 1) - st.queryMin(l, r - 1);
67+
pq.enqueue([nextVal, l, r - 1]);
68+
}
69+
}
70+
return ans;
71+
}

solution/3900-3999/3949.Subtree Inversion Sum II/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ tags:
5151

5252
</ul>
5353

54-
<p data-end="1358" data-start="1249">返回应用&nbsp;<strong>反转操作&nbsp;</strong>后树上节点值的&nbsp;<strong>最大</strong>可能&nbsp;<strong>总和&nbsp;</strong>。</p>
54+
<p data-end="1358" data-start="1249">返回应用&nbsp;<strong>反转操作&nbsp;</strong>后树上节点值的&nbsp;<strong>最大&nbsp;</strong>可能&nbsp;<strong>总和&nbsp;</strong>。</p>
5555

5656
<p>&nbsp;</p>
5757

solution/3900-3999/3950.Exactly One Consecutive Set Bits Pair/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3900-3999/3950.Ex
2424
<p><strong class="example">示例 1:</strong></p>
2525

2626
<div class="example-block">
27-
<p><strong>输入:</strong> <span class="example-io">nums = 6</span></p>
27+
<p><strong>输入:</strong> <span class="example-io">n = 6</span></p>
2828

2929
<p><strong>输出:</strong> <span class="example-io">true</span></p>
3030

@@ -39,7 +39,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3900-3999/3950.Ex
3939
<p><strong class="example">示例 2:</strong></p>
4040

4141
<div class="example-block">
42-
<p><strong>输入:</strong> <span class="example-io">nums = 5</span></p>
42+
<p><strong>输入:</strong> <span class="example-io">n = 5</span></p>
4343

4444
<p><strong>输出:</strong> <span class="example-io">false</span></p>
4545

solution/3900-3999/3956.Maximum Sum of M Non-Overlapping Subarrays I/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3900-3999/3956.Ma
66

77
<!-- problem:start -->
88

9-
# [3956. 非重叠子数组最大和 I](https://leetcode.cn/problems/maximum-sum-of-m-non-overlapping-subarrays-i)
9+
# [3956. M 个非重叠子数组最大和 I](https://leetcode.cn/problems/maximum-sum-of-m-non-overlapping-subarrays-i)
1010

1111
[English Version](/solution/3900-3999/3956.Maximum%20Sum%20of%20M%20Non-Overlapping%20Subarrays%20I/README_EN.md)
1212

0 commit comments

Comments
 (0)