diff --git a/solution/2200-2299/2216.Minimum Deletions to Make Array Beautiful/README.md b/solution/2200-2299/2216.Minimum Deletions to Make Array Beautiful/README.md index d828dc2f66fdd..dcc72856052d4 100644 --- a/solution/2200-2299/2216.Minimum Deletions to Make Array Beautiful/README.md +++ b/solution/2200-2299/2216.Minimum Deletions to Make Array Beautiful/README.md @@ -47,23 +47,15 @@ -不需要修改数组,只统计不符合规则的元素数量即可。 - -```txt -COUNT(A){ - n = A.length - i = 0 - r = 0 - while i < n - 1 - if nums[i] == nums[i + 1] - r += 1 - i += 1 - else - i += 2 - return r -``` +**方法一:贪心** + +根据题目描述,我们知道,一个美丽数组有偶数个元素,且如果我们把这个数组中每相邻两个元素划分为一组,那么每一组中的两个元素都不相等。这意味着,组内的元素不能重复,但组与组之间的元素可以重复。 + +因此,我们考虑从左到右遍历数组,只要遇到相邻两个元素相等,我们就将其中的一个元素删除,即删除数加一;否则,我们可以保留这两个元素。 + +最后,我们判断删除后的数组长度是否为偶数,如果不是,则说明我们需要再删除一个元素,使得最终的数组长度为偶数。 -完成统计后,计算删除元素之后的数组长度是否为奇数,若为奇数,还需要进行一次删除(返回值 + 1)。 +时间复杂度 $O(n)$,其中 $n$ 是数组的长度。我们只需要遍历数组一次。空间复杂度 $O(1)$。 @@ -82,8 +74,22 @@ class Solution: i += 1 else: i += 2 - if (n - ans) % 2: - ans += 1 + ans += (n - ans) % 2 + return ans +``` + +```python +class Solution: + def minDeletion(self, nums: List[int]) -> int: + n = len(nums) + ans = i = 0 + while i < n: + j = i + 1 + while j < n and nums[j] == nums[i]: + j += 1 + ans += 1 + i = j + 1 + ans += (n - ans) % 2 return ans ``` @@ -103,56 +109,27 @@ class Solution { ++i; } } - if ((n - ans) % 2 == 1) { - ++ans; - } + ans += (n - ans) % 2; return ans; } } ``` -### **TypeScript** - -```ts -function minDeletion(nums: number[]): number { - const n = nums.length; - let res = 0; - let i = 0; - while (i < n - 1) { - if (nums[i] === nums[i + 1]) { - i++; - res++; - } else { - i += 2; - } - } - if ((n - res) % 2 === 1) { - res++; - } - return res; -} -``` - -### **Rust** - -```rust -impl Solution { - pub fn min_deletion(nums: Vec) -> i32 { - let n = nums.len(); - let mut res = 0; - let mut i = 0; - while i < n - 1 { - if nums[i] == nums[i + 1] { - res += 1; - i += 1; - } else { - i += 2; +```java +class Solution { + public int minDeletion(int[] nums) { + int n = nums.length; + int ans = 0; + for (int i = 0; i < n;) { + int j = i + 1; + while (j < n && nums[j] == nums[i]) { + ++j; + ++ans; } + i = j + 1; } - if (n - res) % 2 == 1 { - res += 1; - } - res as i32 + ans += (n - ans) % 2; + return ans; } } ``` @@ -172,7 +149,27 @@ public: ++i; } } - if ((n - ans) % 2) ++ans; + ans += (n - ans) % 2; + return ans; + } +}; +``` + +```cpp +class Solution { +public: + int minDeletion(vector& nums) { + int n = nums.size(); + int ans = 0; + for (int i = 0; i < n;) { + int j = i + 1; + while (j < n && nums[j] == nums[i]) { + ++j; + ++ans; + } + i = j + 1; + } + ans += (n - ans) % 2; return ans; } }; @@ -181,9 +178,8 @@ public: ### **Go** ```go -func minDeletion(nums []int) int { +func minDeletion(nums []int) (ans int) { n := len(nums) - ans := 0 for i := 0; i < n-1; i++ { if nums[i] == nums[i+1] { ans++ @@ -191,10 +187,99 @@ func minDeletion(nums []int) int { i++ } } - if (n-ans)%2 == 1 { - ans++ + ans += (n - ans) % 2 + return +} +``` + +```go +func minDeletion(nums []int) (ans int) { + n := len(nums) + for i := 0; i < n; { + j := i + 1 + for ; j < n && nums[j] == nums[i]; j++ { + ans++ + } + i = j + 1 } - return ans + ans += (n - ans) % 2 + return +} +``` + +### **TypeScript** + +```ts +function minDeletion(nums: number[]): number { + const n = nums.length; + let ans = 0; + for (let i = 0; i < n - 1; ++i) { + if (nums[i] === nums[i + 1]) { + ++ans; + } else { + ++i; + } + } + ans += (n - ans) % 2; + return ans; +} +``` + +```ts +function minDeletion(nums: number[]): number { + const n = nums.length; + let ans = 0; + for (let i = 0; i < n; ) { + let j = i + 1; + for (; j < n && nums[j] === nums[i]; ++j) { + ++ans; + } + i = j + 1; + } + ans += (n - ans) % 2; + return ans; +} +``` + +### **Rust** + +```rust +impl Solution { + pub fn min_deletion(nums: Vec) -> i32 { + let n = nums.len(); + let mut ans = 0; + let mut i = 0; + while i < n - 1 { + if nums[i] == nums[i + 1] { + ans += 1; + i += 1; + } else { + i += 2; + } + } + ans += (n - ans) % 2; + ans as i32 + } +} +``` + +```rust +impl Solution { + pub fn min_deletion(nums: Vec) -> i32 { + let n = nums.len(); + let mut ans = 0; + let mut i = 0; + while i < n { + let mut j = i + 1; + while j < n && nums[j] == nums[i] { + ans += 1; + j += 1; + } + i = j + 1; + } + ans += (n - ans) % 2; + ans as i32 + } } ``` diff --git a/solution/2200-2299/2216.Minimum Deletions to Make Array Beautiful/README_EN.md b/solution/2200-2299/2216.Minimum Deletions to Make Array Beautiful/README_EN.md index 07d16968abb6f..75cd53558c621 100644 --- a/solution/2200-2299/2216.Minimum Deletions to Make Array Beautiful/README_EN.md +++ b/solution/2200-2299/2216.Minimum Deletions to Make Array Beautiful/README_EN.md @@ -44,6 +44,16 @@ ## Solutions +**Solution 1: Greedy** + +According to the problem description, we know that a beautiful array has an even number of elements, and if we divide every two adjacent elements in this array into a group, then the two elements in each group are not equal. This means that the elements within a group cannot be repeated, but the elements between groups can be repeated. + +Therefore, we consider traversing the array from left to right. As long as we encounter two adjacent elements that are equal, we delete one of them, that is, the deletion count increases by one; otherwise, we can keep these two elements. + +Finally, we check whether the length of the array after deletion is even. If not, it means that we need to delete one more element to make the final array length even. + +The time complexity is $O(n)$, where $n$ is the length of the array. We only need to traverse the array once. The space complexity is $O(1)$. + ### **Python3** @@ -59,8 +69,22 @@ class Solution: i += 1 else: i += 2 - if (n - ans) % 2: - ans += 1 + ans += (n - ans) % 2 + return ans +``` + +```python +class Solution: + def minDeletion(self, nums: List[int]) -> int: + n = len(nums) + ans = i = 0 + while i < n: + j = i + 1 + while j < n and nums[j] == nums[i]: + j += 1 + ans += 1 + i = j + 1 + ans += (n - ans) % 2 return ans ``` @@ -78,56 +102,27 @@ class Solution { ++i; } } - if ((n - ans) % 2 == 1) { - ++ans; - } + ans += (n - ans) % 2; return ans; } } ``` -### **TypeScript** - -```ts -function minDeletion(nums: number[]): number { - const n = nums.length; - let res = 0; - let i = 0; - while (i < n - 1) { - if (nums[i] === nums[i + 1]) { - i++; - res++; - } else { - i += 2; - } - } - if ((n - res) % 2 === 1) { - res++; - } - return res; -} -``` - -### **Rust** - -```rust -impl Solution { - pub fn min_deletion(nums: Vec) -> i32 { - let n = nums.len(); - let mut res = 0; - let mut i = 0; - while i < n - 1 { - if nums[i] == nums[i + 1] { - res += 1; - i += 1; - } else { - i += 2; +```java +class Solution { + public int minDeletion(int[] nums) { + int n = nums.length; + int ans = 0; + for (int i = 0; i < n;) { + int j = i + 1; + while (j < n && nums[j] == nums[i]) { + ++j; + ++ans; } + i = j + 1; } - if (n - res) % 2 == 1 { - res += 1; - } - res as i32 + ans += (n - ans) % 2; + return ans; } } ``` @@ -147,7 +142,27 @@ public: ++i; } } - if ((n - ans) % 2) ++ans; + ans += (n - ans) % 2; + return ans; + } +}; +``` + +```cpp +class Solution { +public: + int minDeletion(vector& nums) { + int n = nums.size(); + int ans = 0; + for (int i = 0; i < n;) { + int j = i + 1; + while (j < n && nums[j] == nums[i]) { + ++j; + ++ans; + } + i = j + 1; + } + ans += (n - ans) % 2; return ans; } }; @@ -156,9 +171,8 @@ public: ### **Go** ```go -func minDeletion(nums []int) int { +func minDeletion(nums []int) (ans int) { n := len(nums) - ans := 0 for i := 0; i < n-1; i++ { if nums[i] == nums[i+1] { ans++ @@ -166,10 +180,99 @@ func minDeletion(nums []int) int { i++ } } - if (n-ans)%2 == 1 { - ans++ + ans += (n - ans) % 2 + return +} +``` + +```go +func minDeletion(nums []int) (ans int) { + n := len(nums) + for i := 0; i < n; { + j := i + 1 + for ; j < n && nums[j] == nums[i]; j++ { + ans++ + } + i = j + 1 } - return ans + ans += (n - ans) % 2 + return +} +``` + +### **TypeScript** + +```ts +function minDeletion(nums: number[]): number { + const n = nums.length; + let ans = 0; + for (let i = 0; i < n - 1; ++i) { + if (nums[i] === nums[i + 1]) { + ++ans; + } else { + ++i; + } + } + ans += (n - ans) % 2; + return ans; +} +``` + +```ts +function minDeletion(nums: number[]): number { + const n = nums.length; + let ans = 0; + for (let i = 0; i < n; ) { + let j = i + 1; + for (; j < n && nums[j] === nums[i]; ++j) { + ++ans; + } + i = j + 1; + } + ans += (n - ans) % 2; + return ans; +} +``` + +### **Rust** + +```rust +impl Solution { + pub fn min_deletion(nums: Vec) -> i32 { + let n = nums.len(); + let mut ans = 0; + let mut i = 0; + while i < n - 1 { + if nums[i] == nums[i + 1] { + ans += 1; + i += 1; + } else { + i += 2; + } + } + ans += (n - ans) % 2; + ans as i32 + } +} +``` + +```rust +impl Solution { + pub fn min_deletion(nums: Vec) -> i32 { + let n = nums.len(); + let mut ans = 0; + let mut i = 0; + while i < n { + let mut j = i + 1; + while j < n && nums[j] == nums[i] { + ans += 1; + j += 1; + } + i = j + 1; + } + ans += (n - ans) % 2; + ans as i32 + } } ``` diff --git a/solution/2200-2299/2216.Minimum Deletions to Make Array Beautiful/Solution.cpp b/solution/2200-2299/2216.Minimum Deletions to Make Array Beautiful/Solution.cpp index 349416f8ffa62..764e5c5339136 100644 --- a/solution/2200-2299/2216.Minimum Deletions to Make Array Beautiful/Solution.cpp +++ b/solution/2200-2299/2216.Minimum Deletions to Make Array Beautiful/Solution.cpp @@ -10,7 +10,7 @@ class Solution { ++i; } } - if ((n - ans) % 2) ++ans; + ans += (n - ans) % 2; return ans; } }; \ No newline at end of file diff --git a/solution/2200-2299/2216.Minimum Deletions to Make Array Beautiful/Solution.go b/solution/2200-2299/2216.Minimum Deletions to Make Array Beautiful/Solution.go index c24298854fd9c..c01b47bfe491c 100644 --- a/solution/2200-2299/2216.Minimum Deletions to Make Array Beautiful/Solution.go +++ b/solution/2200-2299/2216.Minimum Deletions to Make Array Beautiful/Solution.go @@ -1,6 +1,5 @@ -func minDeletion(nums []int) int { +func minDeletion(nums []int) (ans int) { n := len(nums) - ans := 0 for i := 0; i < n-1; i++ { if nums[i] == nums[i+1] { ans++ @@ -8,8 +7,6 @@ func minDeletion(nums []int) int { i++ } } - if (n-ans)%2 == 1 { - ans++ - } - return ans + ans += (n - ans) % 2 + return } \ No newline at end of file diff --git a/solution/2200-2299/2216.Minimum Deletions to Make Array Beautiful/Solution.java b/solution/2200-2299/2216.Minimum Deletions to Make Array Beautiful/Solution.java index f27863d00dada..b9ed2507f8fb8 100644 --- a/solution/2200-2299/2216.Minimum Deletions to Make Array Beautiful/Solution.java +++ b/solution/2200-2299/2216.Minimum Deletions to Make Array Beautiful/Solution.java @@ -9,9 +9,7 @@ public int minDeletion(int[] nums) { ++i; } } - if ((n - ans) % 2 == 1) { - ++ans; - } + ans += (n - ans) % 2; return ans; } } \ No newline at end of file diff --git a/solution/2200-2299/2216.Minimum Deletions to Make Array Beautiful/Solution.py b/solution/2200-2299/2216.Minimum Deletions to Make Array Beautiful/Solution.py index f3b9c8ccf3c5b..084aaf015c4b1 100644 --- a/solution/2200-2299/2216.Minimum Deletions to Make Array Beautiful/Solution.py +++ b/solution/2200-2299/2216.Minimum Deletions to Make Array Beautiful/Solution.py @@ -8,6 +8,5 @@ def minDeletion(self, nums: List[int]) -> int: i += 1 else: i += 2 - if (n - ans) % 2: - ans += 1 + ans += (n - ans) % 2 return ans diff --git a/solution/2200-2299/2216.Minimum Deletions to Make Array Beautiful/Solution.rs b/solution/2200-2299/2216.Minimum Deletions to Make Array Beautiful/Solution.rs index 00adbc855420b..fc64c415af260 100644 --- a/solution/2200-2299/2216.Minimum Deletions to Make Array Beautiful/Solution.rs +++ b/solution/2200-2299/2216.Minimum Deletions to Make Array Beautiful/Solution.rs @@ -1,19 +1,17 @@ impl Solution { pub fn min_deletion(nums: Vec) -> i32 { let n = nums.len(); - let mut res = 0; + let mut ans = 0; let mut i = 0; while i < n - 1 { if nums[i] == nums[i + 1] { - res += 1; + ans += 1; i += 1; } else { i += 2; } } - if (n - res) % 2 == 1 { - res += 1; - } - res as i32 + ans += (n - ans) % 2; + ans as i32 } } diff --git a/solution/2200-2299/2216.Minimum Deletions to Make Array Beautiful/Solution.ts b/solution/2200-2299/2216.Minimum Deletions to Make Array Beautiful/Solution.ts index f9cafc3ba2c04..ec0db9aac7553 100644 --- a/solution/2200-2299/2216.Minimum Deletions to Make Array Beautiful/Solution.ts +++ b/solution/2200-2299/2216.Minimum Deletions to Make Array Beautiful/Solution.ts @@ -1,17 +1,13 @@ function minDeletion(nums: number[]): number { const n = nums.length; - let res = 0; - let i = 0; - while (i < n - 1) { + let ans = 0; + for (let i = 0; i < n - 1; ++i) { if (nums[i] === nums[i + 1]) { - i++; - res++; + ++ans; } else { - i += 2; + ++i; } } - if ((n - res) % 2 === 1) { - res++; - } - return res; + ans += (n - ans) % 2; + return ans; } diff --git a/solution/2700-2799/2760.Longest Even Odd Subarray With Threshold/README.md b/solution/2700-2799/2760.Longest Even Odd Subarray With Threshold/README.md index ad73866cb2a45..0ae6a20cda2ea 100644 --- a/solution/2700-2799/2760.Longest Even Odd Subarray With Threshold/README.md +++ b/solution/2700-2799/2760.Longest Even Odd Subarray With Threshold/README.md @@ -63,7 +63,7 @@ **方法一:枚举** -我们在 $[0,..n-1]$ 范围内枚举所有 $l$,如果 $nums[l]$ 满足 $nums[l] \bmod 2 = 0$ 并且 $nums[l] \leq threshold$,那么我们就从 $l+1$ 开始,查找最大的满足条件的 $r$,那么此时以 $nums[l]$ 作为左端点的最长奇偶子数组的长度为 $r - l$,取所有 $r - l$ 的最大值作为答案即可。 +我们在 $[0,..n-1]$ 范围内枚举所有 $l$,如果 $nums[l]$ 满足 $nums[l] \bmod 2 = 0$ 并且 $nums[l] \leq threshold$,那么我们就从 $l+1$ 开始,查找第一个不满足条件的 $r$,那么此时以 $nums[l]$ 作为左端点的最长奇偶子数组的长度为 $r - l$,取所有 $r - l$ 的最大值作为答案即可。 时间复杂度 $O(n^2)$,其中 $n$ 是数组 $nums$ 的长度。空间复杂度 $O(1)$。 diff --git a/solution/2900-2999/2936.Number of Equal Numbers Blocks/README.md b/solution/2900-2999/2936.Number of Equal Numbers Blocks/README.md index 7fabdf9a2609a..7b9839afaea9d 100644 --- a/solution/2900-2999/2936.Number of Equal Numbers Blocks/README.md +++ b/solution/2900-2999/2936.Number of Equal Numbers Blocks/README.md @@ -67,6 +67,18 @@ So the answer would be 5. +**方法一:二分查找** + +我们可以使用二分查找来找到每个块的右边界。具体地,我们从左到右遍历数组,对于每个下标 $i$,我们使用二分查找找到最小的下标 $j$,使得 $[i,j)$ 之间的所有元素都等于 $nums[i]$。然后我们将 $i$ 更新为 $j$,并继续遍历数组,直到 $i$ 大于等于数组的长度。 + +时间复杂度 $O(m \times \log n)$,其中 $m$ 是数组 $num$ 中不同元素的个数,而 $n$ 是数组 $num$ 的长度。空间复杂度 $O(1)$。 + +**方法二:分治** + +我们可以使用分治的方法来计算答案。具体地,我们将数组分成两个子数组,递归地计算每个子数组的答案,然后将答案合并起来。如果第一个子数组的最后一个元素和第二个子数组的第一个元素相等,那么我们需要将答案减一。 + +时间复杂度 $O(\log n)$,空间复杂度 $O(\log n)$。其中 $n$ 是数组 $num$ 的长度。 + ### **Python3** @@ -74,7 +86,24 @@ So the answer would be 5. ```python - +# Definition for BigArray. +# class BigArray: +# def at(self, index: long) -> int: +# pass +# def size(self) -> long: +# pass +class Solution(object): + def countBlocks(self, nums: Optional["BigArray"]) -> int: + i, n = 0, nums.size() + ans = 0 + while i < n: + ans += 1 + x = nums.at(i) + if i + 1 < n and nums.at(i + 1) != x: + i += 1 + else: + i += bisect_left(range(i, n), True, key=lambda j: nums.at(j) != x) + return ans ``` ### **Java** @@ -82,13 +111,188 @@ So the answer would be 5. ```java +/** + * Definition for BigArray. + * class BigArray { + * public BigArray(int[] elements); + * public int at(long index); + * public long size(); + * } + */ +class Solution { + public int countBlocks(BigArray nums) { + int ans = 0; + for (long i = 0, n = nums.size(); i < n; ++ans) { + i = search(nums, i, n); + } + return ans; + } + + private long search(BigArray nums, long l, long n) { + long r = n; + int x = nums.at(l); + while (l < r) { + long mid = (l + r) >> 1; + if (nums.at(mid) != x) { + r = mid; + } else { + l = mid + 1; + } + } + return l; + } +} +``` +```java +/** + * Definition for BigArray. + * class BigArray { + * public BigArray(int[] elements); + * public int at(long index); + * public long size(); + * } + */ +class Solution { + public int countBlocks(BigArray nums) { + return f(nums, 0, nums.size() - 1); + } + + private int f(BigArray nums, long l, long r) { + if (nums.at(l) == nums.at(r)) { + return 1; + } + long mid = (l + r) >> 1; + int a = f(nums, l, mid); + int b = f(nums, mid + 1, r); + return a + b - (nums.at(mid) == nums.at(mid + 1) ? 1 : 0); + } +} ``` ### **C++** ```cpp +/** + * Definition for BigArray. + * class BigArray { + * public: + * BigArray(vector elements); + * int at(long long index); + * long long size(); + * }; + */ +class Solution { +public: + int countBlocks(BigArray* nums) { + int ans = 0; + using ll = long long; + ll n = nums->size(); + auto search = [&](ll l) { + ll r = n; + int x = nums->at(l); + while (l < r) { + ll mid = (l + r) >> 1; + if (nums->at(mid) != x) { + r = mid; + } else { + l = mid + 1; + } + } + return l; + }; + for (long long i = 0; i < n; ++ans) { + i = search(i); + } + return ans; + } +}; +``` + +```cpp +/** + * Definition for BigArray. + * class BigArray { + * public: + * BigArray(vector elements); + * int at(long long index); + * long long size(); + * }; + */ +class Solution { +public: + int countBlocks(BigArray* nums) { + using ll = long long; + function f = [&](ll l, ll r) { + if (nums->at(l) == nums->at(r)) { + return 1; + } + ll mid = (l + r) >> 1; + int a = f(l, mid); + int b = f(mid + 1, r); + return a + b - (nums->at(mid) == nums->at(mid + 1)); + }; + return f(0, nums->size() - 1); + } +}; +``` + +### **TypeScript** + +```ts +/** + * Definition for BigArray. + * class BigArray { + * constructor(elements: number[]); + * public at(index: number): number; + * public size(): number; + * } + */ +function countBlocks(nums: BigArray | null): number { + const n = nums.size(); + const search = (l: number): number => { + let r = n; + const x = nums.at(l); + while (l < r) { + const mid = l + Math.floor((r - l) / 2); + if (nums.at(mid) !== x) { + r = mid; + } else { + l = mid + 1; + } + } + return l; + }; + + let ans = 0; + for (let i = 0; i < n; ++ans) { + i = search(i); + } + return ans; +} +``` +```ts +/** + * Definition for BigArray. + * class BigArray { + * constructor(elements: number[]); + * public at(index: number): number; + * public size(): number; + * } + */ +function countBlocks(nums: BigArray | null): number { + const f = (l: number, r: number): number => { + if (nums.at(l) === nums.at(r)) { + return 1; + } + const mid = l + Math.floor((r - l) / 2); + const a = f(l, mid); + const b = f(mid + 1, r); + return a + b - (nums.at(mid) === nums.at(mid + 1) ? 1 : 0); + }; + return f(0, nums.size() - 1); +} ``` ### **Go** diff --git a/solution/2900-2999/2936.Number of Equal Numbers Blocks/README_EN.md b/solution/2900-2999/2936.Number of Equal Numbers Blocks/README_EN.md index b7cdf04b440ce..9abc8c3620418 100644 --- a/solution/2900-2999/2936.Number of Equal Numbers Blocks/README_EN.md +++ b/solution/2900-2999/2936.Number of Equal Numbers Blocks/README_EN.md @@ -63,24 +63,228 @@ So the answer would be 5. ## Solutions +**Solution 1: Binary Search** + +We can use binary search to find the right boundary of each block. Specifically, we traverse the array from left to right. For each index $i$, we use binary search to find the smallest index $j$ such that all elements between $[i,j)$ are equal to $nums[i]$. Then we update $i$ to $j$ and continue to traverse the array until $i$ is greater than or equal to the length of the array. + +The time complexity is $O(m \times \log n)$, where $m$ is the number of different elements in the array $num$, and $n$ is the length of the array $num$. The space complexity is $O(1)$. + +**Solution 2: Divide and Conquer** + +We can use the divide and conquer method to calculate the answer. Specifically, we divide the array into two subarrays, recursively calculate the answer for each subarray, and then merge the answers. If the last element of the first subarray is equal to the first element of the second subarray, then we need to subtract one from the answer. + +The time complexity is $O(\log n)$, and the space complexity is $O(\log n)$. Here, $n$ is the length of the array $num$. + ### **Python3** ```python - +# Definition for BigArray. +# class BigArray: +# def at(self, index: long) -> int: +# pass +# def size(self) -> long: +# pass +class Solution(object): + def countBlocks(self, nums: Optional["BigArray"]) -> int: + i, n = 0, nums.size() + ans = 0 + while i < n: + ans += 1 + x = nums.at(i) + if i + 1 < n and nums.at(i + 1) != x: + i += 1 + else: + i += bisect_left(range(i, n), True, key=lambda j: nums.at(j) != x) + return ans ``` ### **Java** ```java +/** + * Definition for BigArray. + * class BigArray { + * public BigArray(int[] elements); + * public int at(long index); + * public long size(); + * } + */ +class Solution { + public int countBlocks(BigArray nums) { + int ans = 0; + for (long i = 0, n = nums.size(); i < n; ++ans) { + i = search(nums, i, n); + } + return ans; + } + + private long search(BigArray nums, long l, long n) { + long r = n; + int x = nums.at(l); + while (l < r) { + long mid = (l + r) >> 1; + if (nums.at(mid) != x) { + r = mid; + } else { + l = mid + 1; + } + } + return l; + } +} +``` +```java +/** + * Definition for BigArray. + * class BigArray { + * public BigArray(int[] elements); + * public int at(long index); + * public long size(); + * } + */ +class Solution { + public int countBlocks(BigArray nums) { + return f(nums, 0, nums.size() - 1); + } + + private int f(BigArray nums, long l, long r) { + if (nums.at(l) == nums.at(r)) { + return 1; + } + long mid = (l + r) >> 1; + int a = f(nums, l, mid); + int b = f(nums, mid + 1, r); + return a + b - (nums.at(mid) == nums.at(mid + 1) ? 1 : 0); + } +} ``` ### **C++** ```cpp +/** + * Definition for BigArray. + * class BigArray { + * public: + * BigArray(vector elements); + * int at(long long index); + * long long size(); + * }; + */ +class Solution { +public: + int countBlocks(BigArray* nums) { + int ans = 0; + using ll = long long; + ll n = nums->size(); + auto search = [&](ll l) { + ll r = n; + int x = nums->at(l); + while (l < r) { + ll mid = (l + r) >> 1; + if (nums->at(mid) != x) { + r = mid; + } else { + l = mid + 1; + } + } + return l; + }; + for (long long i = 0; i < n; ++ans) { + i = search(i); + } + return ans; + } +}; +``` + +```cpp +/** + * Definition for BigArray. + * class BigArray { + * public: + * BigArray(vector elements); + * int at(long long index); + * long long size(); + * }; + */ +class Solution { +public: + int countBlocks(BigArray* nums) { + using ll = long long; + function f = [&](ll l, ll r) { + if (nums->at(l) == nums->at(r)) { + return 1; + } + ll mid = (l + r) >> 1; + int a = f(l, mid); + int b = f(mid + 1, r); + return a + b - (nums->at(mid) == nums->at(mid + 1)); + }; + return f(0, nums->size() - 1); + } +}; +``` + +### **TypeScript** + +```ts +/** + * Definition for BigArray. + * class BigArray { + * constructor(elements: number[]); + * public at(index: number): number; + * public size(): number; + * } + */ +function countBlocks(nums: BigArray | null): number { + const n = nums.size(); + const search = (l: number): number => { + let r = n; + const x = nums.at(l); + while (l < r) { + const mid = l + Math.floor((r - l) / 2); + if (nums.at(mid) !== x) { + r = mid; + } else { + l = mid + 1; + } + } + return l; + }; + + let ans = 0; + for (let i = 0; i < n; ++ans) { + i = search(i); + } + return ans; +} +``` +```ts +/** + * Definition for BigArray. + * class BigArray { + * constructor(elements: number[]); + * public at(index: number): number; + * public size(): number; + * } + */ +function countBlocks(nums: BigArray | null): number { + const f = (l: number, r: number): number => { + if (nums.at(l) === nums.at(r)) { + return 1; + } + const mid = l + Math.floor((r - l) / 2); + const a = f(l, mid); + const b = f(mid + 1, r); + return a + b - (nums.at(mid) === nums.at(mid + 1) ? 1 : 0); + }; + return f(0, nums.size() - 1); +} ``` ### **Go** diff --git a/solution/2900-2999/2936.Number of Equal Numbers Blocks/Solution.cpp b/solution/2900-2999/2936.Number of Equal Numbers Blocks/Solution.cpp new file mode 100644 index 0000000000000..cc55273b462dd --- /dev/null +++ b/solution/2900-2999/2936.Number of Equal Numbers Blocks/Solution.cpp @@ -0,0 +1,34 @@ +/** + * Definition for BigArray. + * class BigArray { + * public: + * BigArray(vector elements); + * int at(long long index); + * long long size(); + * }; + */ +class Solution { +public: + int countBlocks(BigArray* nums) { + int ans = 0; + using ll = long long; + ll n = nums->size(); + auto search = [&](ll l) { + ll r = n; + int x = nums->at(l); + while (l < r) { + ll mid = (l + r) >> 1; + if (nums->at(mid) != x) { + r = mid; + } else { + l = mid + 1; + } + } + return l; + }; + for (long long i = 0; i < n; ++ans) { + i = search(i); + } + return ans; + } +}; \ No newline at end of file diff --git a/solution/2900-2999/2936.Number of Equal Numbers Blocks/Solution.java b/solution/2900-2999/2936.Number of Equal Numbers Blocks/Solution.java new file mode 100644 index 0000000000000..397989c40257f --- /dev/null +++ b/solution/2900-2999/2936.Number of Equal Numbers Blocks/Solution.java @@ -0,0 +1,31 @@ +/** + * Definition for BigArray. + * class BigArray { + * public BigArray(int[] elements); + * public int at(long index); + * public long size(); + * } + */ +class Solution { + public int countBlocks(BigArray nums) { + int ans = 0; + for (long i = 0, n = nums.size(); i < n; ++ans) { + i = search(nums, i, n); + } + return ans; + } + + private long search(BigArray nums, long l, long n) { + long r = n; + int x = nums.at(l); + while (l < r) { + long mid = (l + r) >> 1; + if (nums.at(mid) != x) { + r = mid; + } else { + l = mid + 1; + } + } + return l; + } +} \ No newline at end of file diff --git a/solution/2900-2999/2936.Number of Equal Numbers Blocks/Solution.py b/solution/2900-2999/2936.Number of Equal Numbers Blocks/Solution.py new file mode 100644 index 0000000000000..01f1576e6cc8f --- /dev/null +++ b/solution/2900-2999/2936.Number of Equal Numbers Blocks/Solution.py @@ -0,0 +1,18 @@ +# Definition for BigArray. +# class BigArray: +# def at(self, index: long) -> int: +# pass +# def size(self) -> long: +# pass +class Solution(object): + def countBlocks(self, nums: Optional["BigArray"]) -> int: + i, n = 0, nums.size() + ans = 0 + while i < n: + ans += 1 + x = nums.at(i) + if i + 1 < n and nums.at(i + 1) != x: + i += 1 + else: + i += bisect_left(range(i, n), True, key=lambda j: nums.at(j) != x) + return ans diff --git a/solution/2900-2999/2936.Number of Equal Numbers Blocks/Solution.ts b/solution/2900-2999/2936.Number of Equal Numbers Blocks/Solution.ts new file mode 100644 index 0000000000000..a0984e21ee9b8 --- /dev/null +++ b/solution/2900-2999/2936.Number of Equal Numbers Blocks/Solution.ts @@ -0,0 +1,30 @@ +/** + * Definition for BigArray. + * class BigArray { + * constructor(elements: number[]); + * public at(index: number): number; + * public size(): number; + * } + */ +function countBlocks(nums: BigArray | null): number { + const n = nums.size(); + const search = (l: number): number => { + let r = n; + const x = nums.at(l); + while (l < r) { + const mid = l + Math.floor((r - l) / 2); + if (nums.at(mid) !== x) { + r = mid; + } else { + l = mid + 1; + } + } + return l; + }; + + let ans = 0; + for (let i = 0; i < n; ++ans) { + i = search(i); + } + return ans; +}