diff --git a/solution/3300-3399/3349.Adjacent Increasing Subarrays Detection I/README.md b/solution/3300-3399/3349.Adjacent Increasing Subarrays Detection I/README.md index daae5e8b3a6c7..ddcbe6c69c113 100644 --- a/solution/3300-3399/3349.Adjacent Increasing Subarrays Detection I/README.md +++ b/solution/3300-3399/3349.Adjacent Increasing Subarrays Detection I/README.md @@ -71,7 +71,17 @@ tags: -### 方法一 +### 方法一:一次遍历 + +根据题目描述,我们只需要找到最大的相邻递增子数组长度 $\textit{mx}$,如果 $\textit{mx} \ge k$,则说明存在两个相邻且长度为 $k$ 的严格递增子数组。 + +我们可以使用一次遍历来计算 $\textit{mx}$。具体来说,我们维护三个变量,其中 $\textit{cur}$ 和 $\textit{pre}$ 分别表示当前递增子数组的长度和前一个递增子数组的长度,而 $\textit{mx}$ 表示最大的相邻递增子数组长度。 + +每当遇到一个非递增的位置时,我们就更新 $\textit{mx}$,并将 $\textit{cur}$ 赋值给 $\textit{pre}$,然后将 $\textit{cur}$ 重置为 $0$,其中 $\textit{mx}$ 的更新方式为 $\textit{mx} = \max(\textit{mx}, \lfloor \frac{\textit{cur}}{2} \rfloor, \min(\textit{pre}, \text{cur}))$,即相邻递增子数组来自于当前递增子数组的一半长度,或者前一个递增子数组和当前递增子数组的较小值。 + +最后,我们只需要判断 $\textit{mx}$ 是否大于等于 $k$ 即可。 + +时间复杂度 $O(n)$,其中 $n$ 是数组的长度。空间复杂度 $O(1)$。 @@ -163,6 +173,51 @@ function hasIncreasingSubarrays(nums: number[], k: number): boolean { } ``` +#### Rust + +```rust +impl Solution { + pub fn has_increasing_subarrays(nums: Vec, k: i32) -> bool { + let n = nums.len(); + let (mut mx, mut pre, mut cur) = (0, 0, 0); + + for i in 0..n { + cur += 1; + if i == n - 1 || nums[i] >= nums[i + 1] { + mx = mx.max(cur / 2).max(pre.min(cur)); + pre = cur; + cur = 0; + } + } + + mx >= k + } +} +``` + +#### JavaScript + +```js +/** + * @param {number[]} nums + * @param {number} k + * @return {boolean} + */ +var hasIncreasingSubarrays = function (nums, k) { + const n = nums.length; + let [mx, pre, cur] = [0, 0, 0]; + for (let i = 0; i < n; ++i) { + ++cur; + if (i === n - 1 || nums[i] >= nums[i + 1]) { + mx = Math.max(mx, cur >> 1, Math.min(pre, cur)); + pre = cur; + cur = 0; + } + } + return mx >= k; +}; +``` + diff --git a/solution/3300-3399/3349.Adjacent Increasing Subarrays Detection I/README_EN.md b/solution/3300-3399/3349.Adjacent Increasing Subarrays Detection I/README_EN.md index 3ecae871148df..45c10cd992653 100644 --- a/solution/3300-3399/3349.Adjacent Increasing Subarrays Detection I/README_EN.md +++ b/solution/3300-3399/3349.Adjacent Increasing Subarrays Detection I/README_EN.md @@ -67,7 +67,17 @@ tags: -### Solution 1 +### Solution 1: Single Pass + +According to the problem description, we only need to find the maximum length of adjacent increasing subarrays $\textit{mx}$. If $\textit{mx} \ge k$, then there exist two adjacent strictly increasing subarrays of length $k$. + +We can use a single pass to calculate $\textit{mx}$. Specifically, we maintain three variables: $\textit{cur}$ and $\textit{pre}$ represent the length of the current increasing subarray and the previous increasing subarray respectively, while $\textit{mx}$ represents the maximum length of adjacent increasing subarrays. + +Whenever we encounter a non-increasing position, we update $\textit{mx}$, assign $\textit{cur}$ to $\textit{pre}$, and reset $\textit{cur}$ to $0$. The update formula for $\textit{mx}$ is $\textit{mx} = \max(\textit{mx}, \lfloor \frac{\textit{cur}}{2} \rfloor, \min(\textit{pre}, \textit{cur}))$, meaning the adjacent increasing subarrays come from either half the length of the current increasing subarray, or the smaller value between the previous increasing subarray and the current increasing subarray. + +Finally, we just need to check whether $\textit{mx}$ is greater than or equal to $k$. + +The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$. @@ -159,6 +169,51 @@ function hasIncreasingSubarrays(nums: number[], k: number): boolean { } ``` +#### Rust + +```rust +impl Solution { + pub fn has_increasing_subarrays(nums: Vec, k: i32) -> bool { + let n = nums.len(); + let (mut mx, mut pre, mut cur) = (0, 0, 0); + + for i in 0..n { + cur += 1; + if i == n - 1 || nums[i] >= nums[i + 1] { + mx = mx.max(cur / 2).max(pre.min(cur)); + pre = cur; + cur = 0; + } + } + + mx >= k + } +} +``` + +#### JavaScript + +```js +/** + * @param {number[]} nums + * @param {number} k + * @return {boolean} + */ +var hasIncreasingSubarrays = function (nums, k) { + const n = nums.length; + let [mx, pre, cur] = [0, 0, 0]; + for (let i = 0; i < n; ++i) { + ++cur; + if (i === n - 1 || nums[i] >= nums[i + 1]) { + mx = Math.max(mx, cur >> 1, Math.min(pre, cur)); + pre = cur; + cur = 0; + } + } + return mx >= k; +}; +``` + diff --git a/solution/3300-3399/3349.Adjacent Increasing Subarrays Detection I/Solution.js b/solution/3300-3399/3349.Adjacent Increasing Subarrays Detection I/Solution.js new file mode 100644 index 0000000000000..15750115f32d5 --- /dev/null +++ b/solution/3300-3399/3349.Adjacent Increasing Subarrays Detection I/Solution.js @@ -0,0 +1,18 @@ +/** + * @param {number[]} nums + * @param {number} k + * @return {boolean} + */ +var hasIncreasingSubarrays = function (nums, k) { + const n = nums.length; + let [mx, pre, cur] = [0, 0, 0]; + for (let i = 0; i < n; ++i) { + ++cur; + if (i === n - 1 || nums[i] >= nums[i + 1]) { + mx = Math.max(mx, cur >> 1, Math.min(pre, cur)); + pre = cur; + cur = 0; + } + } + return mx >= k; +}; diff --git a/solution/3300-3399/3349.Adjacent Increasing Subarrays Detection I/Solution.rs b/solution/3300-3399/3349.Adjacent Increasing Subarrays Detection I/Solution.rs new file mode 100644 index 0000000000000..cf13ab9c2f93b --- /dev/null +++ b/solution/3300-3399/3349.Adjacent Increasing Subarrays Detection I/Solution.rs @@ -0,0 +1,17 @@ +impl Solution { + pub fn has_increasing_subarrays(nums: Vec, k: i32) -> bool { + let n = nums.len(); + let (mut mx, mut pre, mut cur) = (0, 0, 0); + + for i in 0..n { + cur += 1; + if i == n - 1 || nums[i] >= nums[i + 1] { + mx = mx.max(cur / 2).max(pre.min(cur)); + pre = cur; + cur = 0; + } + } + + mx >= k + } +} diff --git a/solution/3300-3399/3350.Adjacent Increasing Subarrays Detection II/README.md b/solution/3300-3399/3350.Adjacent Increasing Subarrays Detection II/README.md index 22616aafdbd20..98e8f8d7ab035 100644 --- a/solution/3300-3399/3350.Adjacent Increasing Subarrays Detection II/README.md +++ b/solution/3300-3399/3350.Adjacent Increasing Subarrays Detection II/README.md @@ -79,7 +79,15 @@ tags: -### 方法一 +### 方法一:一次遍历 + +我们可以使用一次遍历来计算最大的相邻递增子数组长度 $\textit{ans}$。具体地,我们维护三个变量 $\textit{cur}$ 和 $\textit{pre}$ 分别表示当前递增子数组和上一个递增子数组的长度,而 $\textit{ans}$ 表示最大的相邻递增子数组长度。 + +每当遇到一个非递增的位置时,我们就更新 $\textit{ans}$,将 $\textit{cur}$ 赋值给 $\textit{pre}$,并将 $\textit{cur}$ 重置为 $0$。更新 $\textit{ans}$ 的公式为 $\textit{ans} = \max(\textit{ans}, \lfloor \frac{\textit{cur}}{2} \rfloor, \min(\textit{pre}, \textit{cur}))$,表示相邻递增子数组要么来自当前递增子数组长度的一半,要么来自前一个递增子数组和当前递增子数组的较小值。 + +最后我们只需要返回 $\textit{ans}$ 即可。 + +时间复杂度 $O(n)$,其中 $n$ 是数组的长度。空间复杂度 $O(1)$。 @@ -171,6 +179,49 @@ function maxIncreasingSubarrays(nums: number[]): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn max_increasing_subarrays(nums: Vec) -> i32 { + let n = nums.len(); + let (mut ans, mut pre, mut cur) = (0, 0, 0); + + for i in 0..n { + cur += 1; + if i == n - 1 || nums[i] >= nums[i + 1] { + ans = ans.max(cur / 2).max(pre.min(cur)); + pre = cur; + cur = 0; + } + } + + ans + } +} +``` + +#### JavaScript + +```js +/** + * @param {number[]} nums + * @return {number} + */ +var maxIncreasingSubarrays = function (nums) { + let [ans, pre, cur] = [0, 0, 0]; + const n = nums.length; + for (let i = 0; i < n; ++i) { + ++cur; + if (i === n - 1 || nums[i] >= nums[i + 1]) { + ans = Math.max(ans, cur >> 1, Math.min(pre, cur)); + [pre, cur] = [cur, 0]; + } + } + return ans; +}; +``` + diff --git a/solution/3300-3399/3350.Adjacent Increasing Subarrays Detection II/README_EN.md b/solution/3300-3399/3350.Adjacent Increasing Subarrays Detection II/README_EN.md index 5139bb2be829e..d2ca19ca3f529 100644 --- a/solution/3300-3399/3350.Adjacent Increasing Subarrays Detection II/README_EN.md +++ b/solution/3300-3399/3350.Adjacent Increasing Subarrays Detection II/README_EN.md @@ -77,7 +77,15 @@ tags: -### Solution 1 +### Solution 1: Single Pass + +We can use a single pass to calculate the maximum length of adjacent increasing subarrays $\textit{ans}$. Specifically, we maintain three variables: $\textit{cur}$ and $\textit{pre}$ represent the length of the current increasing subarray and the previous increasing subarray respectively, while $\textit{ans}$ represents the maximum length of adjacent increasing subarrays. + +Whenever we encounter a non-increasing position, we update $\textit{ans}$, assign $\textit{cur}$ to $\textit{pre}$, and reset $\textit{cur}$ to $0$. The update formula for $\textit{ans}$ is $\textit{ans} = \max(\textit{ans}, \lfloor \frac{\textit{cur}}{2} \rfloor, \min(\textit{pre}, \textit{cur}))$, meaning the adjacent increasing subarrays come from either half the length of the current increasing subarray, or the smaller value between the previous increasing subarray and the current increasing subarray. + +Finally, we just need to return $\textit{ans}$. + +The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$. @@ -169,6 +177,49 @@ function maxIncreasingSubarrays(nums: number[]): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn max_increasing_subarrays(nums: Vec) -> i32 { + let n = nums.len(); + let (mut ans, mut pre, mut cur) = (0, 0, 0); + + for i in 0..n { + cur += 1; + if i == n - 1 || nums[i] >= nums[i + 1] { + ans = ans.max(cur / 2).max(pre.min(cur)); + pre = cur; + cur = 0; + } + } + + ans + } +} +``` + +#### JavaScript + +```js +/** + * @param {number[]} nums + * @return {number} + */ +var maxIncreasingSubarrays = function (nums) { + let [ans, pre, cur] = [0, 0, 0]; + const n = nums.length; + for (let i = 0; i < n; ++i) { + ++cur; + if (i === n - 1 || nums[i] >= nums[i + 1]) { + ans = Math.max(ans, cur >> 1, Math.min(pre, cur)); + [pre, cur] = [cur, 0]; + } + } + return ans; +}; +``` + diff --git a/solution/3300-3399/3350.Adjacent Increasing Subarrays Detection II/Solution.js b/solution/3300-3399/3350.Adjacent Increasing Subarrays Detection II/Solution.js new file mode 100644 index 0000000000000..6f46d2ee60fc3 --- /dev/null +++ b/solution/3300-3399/3350.Adjacent Increasing Subarrays Detection II/Solution.js @@ -0,0 +1,16 @@ +/** + * @param {number[]} nums + * @return {number} + */ +var maxIncreasingSubarrays = function (nums) { + let [ans, pre, cur] = [0, 0, 0]; + const n = nums.length; + for (let i = 0; i < n; ++i) { + ++cur; + if (i === n - 1 || nums[i] >= nums[i + 1]) { + ans = Math.max(ans, cur >> 1, Math.min(pre, cur)); + [pre, cur] = [cur, 0]; + } + } + return ans; +}; diff --git a/solution/3300-3399/3350.Adjacent Increasing Subarrays Detection II/Solution.rs b/solution/3300-3399/3350.Adjacent Increasing Subarrays Detection II/Solution.rs new file mode 100644 index 0000000000000..c2eec977f8509 --- /dev/null +++ b/solution/3300-3399/3350.Adjacent Increasing Subarrays Detection II/Solution.rs @@ -0,0 +1,17 @@ +impl Solution { + pub fn max_increasing_subarrays(nums: Vec) -> i32 { + let n = nums.len(); + let (mut ans, mut pre, mut cur) = (0, 0, 0); + + for i in 0..n { + cur += 1; + if i == n - 1 || nums[i] >= nums[i + 1] { + ans = ans.max(cur / 2).max(pre.min(cur)); + pre = cur; + cur = 0; + } + } + + ans + } +}