Skip to content

feat: add solutions to lc problem: No.3187 #3119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions solution/2200-2299/2288.Apply Discount to Prices/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,14 @@ func discountPrices(sentence string, discount int) string {
```ts
function discountPrices(sentence: string, discount: number): string {
const sell = (100 - discount) / 100;
let reg = new RegExp(/^(\$)(([1-9]\d*\.?\d*)|(0\.\d*))$/g);
let arr = sentence.split(' ').map(d => {
const reg = new RegExp(/^(\$)(([1-9]\d*\.?\d*)|(0\.\d*))$/g);
const words = sentence.split(' ').map(d => {
if (!reg.test(d)) return d;
return d.replace(reg, (s, $1, $2) => {
return `$${(sell * $2).toFixed(2)}`;
});
});
return arr.join(' ');
return words.join(' ');
}
```

Expand Down
12 changes: 8 additions & 4 deletions solution/2200-2299/2288.Apply Discount to Prices/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ Each of them is replaced by "$0.00".

<!-- solution:start -->

### Solution 1
### Solution 1: Simulation

We can split the sentence into an array of words by spaces, then iterate through the array of words. For each word, if it represents a price, we update it to the price after applying the discount. Finally, we concatenate the updated array of words into a space-separated string.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the string `sentence`.

<!-- tabs:start -->

Expand Down Expand Up @@ -176,14 +180,14 @@ func discountPrices(sentence string, discount int) string {
```ts
function discountPrices(sentence: string, discount: number): string {
const sell = (100 - discount) / 100;
let reg = new RegExp(/^(\$)(([1-9]\d*\.?\d*)|(0\.\d*))$/g);
let arr = sentence.split(' ').map(d => {
const reg = new RegExp(/^(\$)(([1-9]\d*\.?\d*)|(0\.\d*))$/g);
const words = sentence.split(' ').map(d => {
if (!reg.test(d)) return d;
return d.replace(reg, (s, $1, $2) => {
return `$${(sell * $2).toFixed(2)}`;
});
});
return arr.join(' ');
return words.join(' ');
}
```

Expand Down
6 changes: 3 additions & 3 deletions solution/2200-2299/2288.Apply Discount to Prices/Solution.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
function discountPrices(sentence: string, discount: number): string {
const sell = (100 - discount) / 100;
let reg = new RegExp(/^(\$)(([1-9]\d*\.?\d*)|(0\.\d*))$/g);
let arr = sentence.split(' ').map(d => {
const reg = new RegExp(/^(\$)(([1-9]\d*\.?\d*)|(0\.\d*))$/g);
const words = sentence.split(' ').map(d => {
if (!reg.test(d)) return d;
return d.replace(reg, (s, $1, $2) => {
return `$${(sell * $2).toFixed(2)}`;
});
});
return arr.join(' ');
return words.join(' ');
}
297 changes: 293 additions & 4 deletions solution/3100-3199/3187.Peaks in Array/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,32 +88,321 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3187.Pe

<!-- solution:start -->

### 方法一
### 方法一:树状数组

根据题目描述,对于 $0 < i < n - 1$,如果满足 $nums[i - 1] < nums[i]$ 且 $nums[i] > nums[i + 1]$,我们可以将 $nums[i]$ 视为 $1$,否则视为 $0$。这样,对于操作 $1$,即查询子数组 $nums[l..r]$ 中的峰值元素个数,相当于查询区间 $[l + 1, r - 1]$ 中 $1$ 的个数。我们可以使用树状数组来维护区间 $[1, n - 1]$ 中 $1$ 的个数。

而对于操作 $1$,即把 $nums[idx]$ 更新为 $val$,只会影响到 $idx - 1$、$idx$、$idx + 1$ 这三个位置的值,因此我们只需要更新这三个位置即可。具体地,我们可以先把这三个位置的峰值元素去掉,然后更新 $nums[idx]$ 的值,最后再把这三个位置的峰值元素加回来。

时间复杂度 $O((n + q) \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 和 $q$ 分别是数组 `nums` 的长度和查询数组 `queries` 的长度。

<!-- tabs:start -->

#### Python3

```python

class BinaryIndexedTree:
__slots__ = "n", "c"

def __init__(self, n: int):
self.n = n
self.c = [0] * (n + 1)

def update(self, x: int, delta: int) -> None:
while x <= self.n:
self.c[x] += delta
x += x & -x

def query(self, x: int) -> int:
s = 0
while x:
s += self.c[x]
x -= x & -x
return s


class Solution:
def countOfPeaks(self, nums: List[int], queries: List[List[int]]) -> List[int]:
def update(i: int, val: int):
if i <= 0 or i >= n - 1:
return
if nums[i - 1] < nums[i] and nums[i] > nums[i + 1]:
tree.update(i, val)

n = len(nums)
tree = BinaryIndexedTree(n - 1)
for i in range(1, n - 1):
update(i, 1)
ans = []
for q in queries:
if q[0] == 1:
l, r = q[1] + 1, q[2] - 1
ans.append(0 if l > r else tree.query(r) - tree.query(l - 1))
else:
idx, val = q[1:]
for i in range(idx - 1, idx + 2):
update(i, -1)
nums[idx] = val
for i in range(idx - 1, idx + 2):
update(i, 1)
return ans
```

#### Java

```java

class BinaryIndexedTree {
private int n;
private int[] c;

public BinaryIndexedTree(int n) {
this.n = n;
this.c = new int[n + 1];
}

public void update(int x, int delta) {
for (; x <= n; x += x & -x) {
c[x] += delta;
}
}

public int query(int x) {
int s = 0;
for (; x > 0; x -= x & -x) {
s += c[x];
}
return s;
}
}

class Solution {
private BinaryIndexedTree tree;
private int[] nums;

public List<Integer> countOfPeaks(int[] nums, int[][] queries) {
int n = nums.length;
this.nums = nums;
tree = new BinaryIndexedTree(n - 1);
for (int i = 1; i < n - 1; ++i) {
update(i, 1);
}
List<Integer> ans = new ArrayList<>();
for (var q : queries) {
if (q[0] == 1) {
int l = q[1] + 1, r = q[2] - 1;
ans.add(l > r ? 0 : tree.query(r) - tree.query(l - 1));
} else {
int idx = q[1], val = q[2];
for (int i = idx - 1; i <= idx + 1; ++i) {
update(i, -1);
}
nums[idx] = val;
for (int i = idx - 1; i <= idx + 1; ++i) {
update(i, 1);
}
}
}
return ans;
}

private void update(int i, int val) {
if (i <= 0 || i >= nums.length - 1) {
return;
}
if (nums[i - 1] < nums[i] && nums[i] > nums[i + 1]) {
tree.update(i, val);
}
}
}
```

#### C++

```cpp

class BinaryIndexedTree {
private:
int n;
vector<int> c;

public:
BinaryIndexedTree(int n)
: n(n)
, c(n + 1) {}

void update(int x, int delta) {
for (; x <= n; x += x & -x) {
c[x] += delta;
}
}

int query(int x) {
int s = 0;
for (; x > 0; x -= x & -x) {
s += c[x];
}
return s;
}
};

class Solution {
public:
vector<int> countOfPeaks(vector<int>& nums, vector<vector<int>>& queries) {
int n = nums.size();
BinaryIndexedTree tree(n - 1);
auto update = [&](int i, int val) {
if (i <= 0 || i >= n - 1) {
return;
}
if (nums[i - 1] < nums[i] && nums[i] > nums[i + 1]) {
tree.update(i, val);
}
};
for (int i = 1; i < n - 1; ++i) {
update(i, 1);
}
vector<int> ans;
for (auto& q : queries) {
if (q[0] == 1) {
int l = q[1] + 1, r = q[2] - 1;
ans.push_back(l > r ? 0 : tree.query(r) - tree.query(l - 1));
} else {
int idx = q[1], val = q[2];
for (int i = idx - 1; i <= idx + 1; ++i) {
update(i, -1);
}
nums[idx] = val;
for (int i = idx - 1; i <= idx + 1; ++i) {
update(i, 1);
}
}
}
return ans;
}
};
```

#### Go

```go
type BinaryIndexedTree struct {
n int
c []int
}

func NewBinaryIndexedTree(n int) *BinaryIndexedTree {
return &BinaryIndexedTree{n: n, c: make([]int, n+1)}
}

func (bit *BinaryIndexedTree) update(x, delta int) {
for ; x <= bit.n; x += x & -x {
bit.c[x] += delta
}
}

func (bit *BinaryIndexedTree) query(x int) int {
s := 0
for ; x > 0; x -= x & -x {
s += bit.c[x]
}
return s
}

func countOfPeaks(nums []int, queries [][]int) (ans []int) {
n := len(nums)
tree := NewBinaryIndexedTree(n - 1)
update := func(i, val int) {
if i <= 0 || i >= n-1 {
return
}
if nums[i-1] < nums[i] && nums[i] > nums[i+1] {
tree.update(i, val)
}
}
for i := 1; i < n-1; i++ {
update(i, 1)
}
for _, q := range queries {
if q[0] == 1 {
l, r := q[1]+1, q[2]-1
t := 0
if l <= r {
t = tree.query(r) - tree.query(l-1)
}
ans = append(ans, t)
} else {
idx, val := q[1], q[2]
for i := idx - 1; i <= idx+1; i++ {
update(i, -1)
}
nums[idx] = val
for i := idx - 1; i <= idx+1; i++ {
update(i, 1)
}
}
}
return
}
```

#### TypeScript

```ts
class BinaryIndexedTree {
private n: number;
private c: number[];

constructor(n: number) {
this.n = n;
this.c = Array(n + 1).fill(0);
}

update(x: number, delta: number): void {
for (; x <= this.n; x += x & -x) {
this.c[x] += delta;
}
}

query(x: number): number {
let s = 0;
for (; x > 0; x -= x & -x) {
s += this.c[x];
}
return s;
}
}

function countOfPeaks(nums: number[], queries: number[][]): number[] {
const n = nums.length;
const tree = new BinaryIndexedTree(n - 1);
const update = (i: number, val: number): void => {
if (i <= 0 || i >= n - 1) {
return;
}
if (nums[i - 1] < nums[i] && nums[i] > nums[i + 1]) {
tree.update(i, val);
}
};
for (let i = 1; i < n - 1; ++i) {
update(i, 1);
}
const ans: number[] = [];
for (const q of queries) {
if (q[0] === 1) {
const [l, r] = [q[1] + 1, q[2] - 1];
ans.push(l > r ? 0 : tree.query(r) - tree.query(l - 1));
} else {
const [idx, val] = [q[1], q[2]];
for (let i = idx - 1; i <= idx + 1; ++i) {
update(i, -1);
}
nums[idx] = val;
for (let i = idx - 1; i <= idx + 1; ++i) {
update(i, 1);
}
}
}
return ans;
}
```

<!-- tabs:end -->
Expand Down
Loading
Loading