Skip to content

Commit 45411c3

Browse files
authored
feat: add solutions to lc problem: No.3187 (#4778)
1 parent 24d8fef commit 45411c3

File tree

4 files changed

+431
-0
lines changed

4 files changed

+431
-0
lines changed

solution/3100-3199/3187.Peaks in Array/README.md

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,153 @@ function countOfPeaks(nums: number[], queries: number[][]): number[] {
411411
}
412412
```
413413

414+
#### Rust
415+
416+
```rust
417+
impl Solution {
418+
pub fn count_of_peaks(mut nums: Vec<i32>, queries: Vec<Vec<i32>>) -> Vec<i32> {
419+
struct BinaryIndexedTree {
420+
n: usize,
421+
c: Vec<i32>,
422+
}
423+
424+
impl BinaryIndexedTree {
425+
fn new(n: usize) -> Self {
426+
Self { n, c: vec![0; n + 1] }
427+
}
428+
429+
fn update(&mut self, mut x: usize, delta: i32) {
430+
while x <= self.n {
431+
self.c[x] += delta;
432+
x += x & (!x + 1);
433+
}
434+
}
435+
436+
fn query(&self, mut x: usize) -> i32 {
437+
let mut s = 0;
438+
while x > 0 {
439+
s += self.c[x];
440+
x &= x - 1;
441+
}
442+
s
443+
}
444+
}
445+
446+
let n = nums.len();
447+
let mut tree = BinaryIndexedTree::new(n - 1);
448+
449+
let mut update = |i: usize, val: i32, nums: &Vec<i32>, tree: &mut BinaryIndexedTree| {
450+
if i == 0 || i >= n - 1 {
451+
return;
452+
}
453+
if nums[i - 1] < nums[i] && nums[i] > nums[i + 1] {
454+
tree.update(i, val);
455+
}
456+
};
457+
458+
for i in 1..n - 1 {
459+
update(i, 1, &nums, &mut tree);
460+
}
461+
462+
let mut ans = Vec::new();
463+
for q in queries {
464+
if q[0] == 1 {
465+
let l = (q[1] + 1).max(1) as usize;
466+
let r = (q[2] - 1).max(0) as usize;
467+
if l > r {
468+
ans.push(0);
469+
} else {
470+
ans.push(tree.query(r) - tree.query(l - 1));
471+
}
472+
} else {
473+
let idx = q[1] as usize;
474+
let val = q[2];
475+
let left = if idx > 0 { idx - 1 } else { 0 };
476+
let right = usize::min(idx + 1, n - 1);
477+
for i in left..=right {
478+
update(i, -1, &nums, &mut tree);
479+
}
480+
nums[idx] = val;
481+
for i in left..=right {
482+
update(i, 1, &nums, &mut tree);
483+
}
484+
}
485+
}
486+
487+
ans
488+
}
489+
}
490+
```
491+
492+
#### JavaScript
493+
494+
```js
495+
class BinaryIndexedTree {
496+
constructor(n) {
497+
this.n = n;
498+
this.c = Array(n + 1).fill(0);
499+
}
500+
501+
update(x, delta) {
502+
for (; x <= this.n; x += x & -x) {
503+
this.c[x] += delta;
504+
}
505+
}
506+
507+
query(x) {
508+
let s = 0;
509+
for (; x > 0; x -= x & -x) {
510+
s += this.c[x];
511+
}
512+
return s;
513+
}
514+
}
515+
516+
/**
517+
* @param {number[]} nums
518+
* @param {number[][]} queries
519+
* @return {number[]}
520+
*/
521+
var countOfPeaks = function (nums, queries) {
522+
const n = nums.length;
523+
const tree = new BinaryIndexedTree(n - 1);
524+
525+
const update = (i, val) => {
526+
if (i <= 0 || i >= n - 1) {
527+
return;
528+
}
529+
if (nums[i - 1] < nums[i] && nums[i] > nums[i + 1]) {
530+
tree.update(i, val);
531+
}
532+
};
533+
534+
for (let i = 1; i < n - 1; ++i) {
535+
update(i, 1);
536+
}
537+
538+
const ans = [];
539+
for (const q of queries) {
540+
if (q[0] === 1) {
541+
const l = q[1] + 1;
542+
const r = q[2] - 1;
543+
ans.push(l > r ? 0 : tree.query(r) - tree.query(l - 1));
544+
} else {
545+
const idx = q[1];
546+
const val = q[2];
547+
for (let i = idx - 1; i <= idx + 1; ++i) {
548+
update(i, -1);
549+
}
550+
nums[idx] = val;
551+
for (let i = idx - 1; i <= idx + 1; ++i) {
552+
update(i, 1);
553+
}
554+
}
555+
}
556+
557+
return ans;
558+
};
559+
```
560+
414561
<!-- tabs:end -->
415562

416563
<!-- solution:end -->

solution/3100-3199/3187.Peaks in Array/README_EN.md

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,153 @@ function countOfPeaks(nums: number[], queries: number[][]): number[] {
409409
}
410410
```
411411

412+
#### Rust
413+
414+
```rust
415+
impl Solution {
416+
pub fn count_of_peaks(mut nums: Vec<i32>, queries: Vec<Vec<i32>>) -> Vec<i32> {
417+
struct BinaryIndexedTree {
418+
n: usize,
419+
c: Vec<i32>,
420+
}
421+
422+
impl BinaryIndexedTree {
423+
fn new(n: usize) -> Self {
424+
Self { n, c: vec![0; n + 1] }
425+
}
426+
427+
fn update(&mut self, mut x: usize, delta: i32) {
428+
while x <= self.n {
429+
self.c[x] += delta;
430+
x += x & (!x + 1);
431+
}
432+
}
433+
434+
fn query(&self, mut x: usize) -> i32 {
435+
let mut s = 0;
436+
while x > 0 {
437+
s += self.c[x];
438+
x &= x - 1;
439+
}
440+
s
441+
}
442+
}
443+
444+
let n = nums.len();
445+
let mut tree = BinaryIndexedTree::new(n - 1);
446+
447+
let mut update = |i: usize, val: i32, nums: &Vec<i32>, tree: &mut BinaryIndexedTree| {
448+
if i == 0 || i >= n - 1 {
449+
return;
450+
}
451+
if nums[i - 1] < nums[i] && nums[i] > nums[i + 1] {
452+
tree.update(i, val);
453+
}
454+
};
455+
456+
for i in 1..n - 1 {
457+
update(i, 1, &nums, &mut tree);
458+
}
459+
460+
let mut ans = Vec::new();
461+
for q in queries {
462+
if q[0] == 1 {
463+
let l = (q[1] + 1).max(1) as usize;
464+
let r = (q[2] - 1).max(0) as usize;
465+
if l > r {
466+
ans.push(0);
467+
} else {
468+
ans.push(tree.query(r) - tree.query(l - 1));
469+
}
470+
} else {
471+
let idx = q[1] as usize;
472+
let val = q[2];
473+
let left = if idx > 0 { idx - 1 } else { 0 };
474+
let right = usize::min(idx + 1, n - 1);
475+
for i in left..=right {
476+
update(i, -1, &nums, &mut tree);
477+
}
478+
nums[idx] = val;
479+
for i in left..=right {
480+
update(i, 1, &nums, &mut tree);
481+
}
482+
}
483+
}
484+
485+
ans
486+
}
487+
}
488+
```
489+
490+
#### JavaScript
491+
492+
```js
493+
class BinaryIndexedTree {
494+
constructor(n) {
495+
this.n = n;
496+
this.c = Array(n + 1).fill(0);
497+
}
498+
499+
update(x, delta) {
500+
for (; x <= this.n; x += x & -x) {
501+
this.c[x] += delta;
502+
}
503+
}
504+
505+
query(x) {
506+
let s = 0;
507+
for (; x > 0; x -= x & -x) {
508+
s += this.c[x];
509+
}
510+
return s;
511+
}
512+
}
513+
514+
/**
515+
* @param {number[]} nums
516+
* @param {number[][]} queries
517+
* @return {number[]}
518+
*/
519+
var countOfPeaks = function (nums, queries) {
520+
const n = nums.length;
521+
const tree = new BinaryIndexedTree(n - 1);
522+
523+
const update = (i, val) => {
524+
if (i <= 0 || i >= n - 1) {
525+
return;
526+
}
527+
if (nums[i - 1] < nums[i] && nums[i] > nums[i + 1]) {
528+
tree.update(i, val);
529+
}
530+
};
531+
532+
for (let i = 1; i < n - 1; ++i) {
533+
update(i, 1);
534+
}
535+
536+
const ans = [];
537+
for (const q of queries) {
538+
if (q[0] === 1) {
539+
const l = q[1] + 1;
540+
const r = q[2] - 1;
541+
ans.push(l > r ? 0 : tree.query(r) - tree.query(l - 1));
542+
} else {
543+
const idx = q[1];
544+
const val = q[2];
545+
for (let i = idx - 1; i <= idx + 1; ++i) {
546+
update(i, -1);
547+
}
548+
nums[idx] = val;
549+
for (let i = idx - 1; i <= idx + 1; ++i) {
550+
update(i, 1);
551+
}
552+
}
553+
}
554+
555+
return ans;
556+
};
557+
```
558+
412559
<!-- tabs:end -->
413560

414561
<!-- solution:end -->
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
class BinaryIndexedTree {
2+
constructor(n) {
3+
this.n = n;
4+
this.c = Array(n + 1).fill(0);
5+
}
6+
7+
update(x, delta) {
8+
for (; x <= this.n; x += x & -x) {
9+
this.c[x] += delta;
10+
}
11+
}
12+
13+
query(x) {
14+
let s = 0;
15+
for (; x > 0; x -= x & -x) {
16+
s += this.c[x];
17+
}
18+
return s;
19+
}
20+
}
21+
22+
/**
23+
* @param {number[]} nums
24+
* @param {number[][]} queries
25+
* @return {number[]}
26+
*/
27+
var countOfPeaks = function (nums, queries) {
28+
const n = nums.length;
29+
const tree = new BinaryIndexedTree(n - 1);
30+
31+
const update = (i, val) => {
32+
if (i <= 0 || i >= n - 1) {
33+
return;
34+
}
35+
if (nums[i - 1] < nums[i] && nums[i] > nums[i + 1]) {
36+
tree.update(i, val);
37+
}
38+
};
39+
40+
for (let i = 1; i < n - 1; ++i) {
41+
update(i, 1);
42+
}
43+
44+
const ans = [];
45+
for (const q of queries) {
46+
if (q[0] === 1) {
47+
const l = q[1] + 1;
48+
const r = q[2] - 1;
49+
ans.push(l > r ? 0 : tree.query(r) - tree.query(l - 1));
50+
} else {
51+
const idx = q[1];
52+
const val = q[2];
53+
for (let i = idx - 1; i <= idx + 1; ++i) {
54+
update(i, -1);
55+
}
56+
nums[idx] = val;
57+
for (let i = idx - 1; i <= idx + 1; ++i) {
58+
update(i, 1);
59+
}
60+
}
61+
}
62+
63+
return ans;
64+
};

0 commit comments

Comments
 (0)