Skip to content
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

feat: add solutions to lc problems: No.3105,3106 #2550

Merged
merged 1 commit into from
Apr 7, 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
Original file line number Diff line number Diff line change
Expand Up @@ -73,24 +73,125 @@

## 解法

### 方法一
### 方法一:两次遍历

我们先进行一次遍历,找出严格递增的最长子数组长度,更新答案。然后再进行一次遍历,找出严格递减的最长子数组长度,再次更新答案。

时间复杂度 $O(n)$,其中 $n$ 是数组的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

```python

class Solution:
def longestMonotonicSubarray(self, nums: List[int]) -> int:
ans = t = 1
for i, x in enumerate(nums[1:]):
if nums[i] < x:
t += 1
ans = max(ans, t)
else:
t = 1
t = 1
for i, x in enumerate(nums[1:]):
if nums[i] > x:
t += 1
ans = max(ans, t)
else:
t = 1
return ans
```

```java

class Solution {
public int longestMonotonicSubarray(int[] nums) {
int ans = 1;
for (int i = 1, t = 1; i < nums.length; ++i) {
if (nums[i - 1] < nums[i]) {
ans = Math.max(ans, ++t);
} else {
t = 1;
}
}
for (int i = 1, t = 1; i < nums.length; ++i) {
if (nums[i - 1] > nums[i]) {
ans = Math.max(ans, ++t);
} else {
t = 1;
}
}
return ans;
}
}
```

```cpp

class Solution {
public:
int longestMonotonicSubarray(vector<int>& nums) {
int ans = 1;
for (int i = 1, t = 1; i < nums.size(); ++i) {
if (nums[i - 1] < nums[i]) {
ans = max(ans, ++t);
} else {
t = 1;
}
}
for (int i = 1, t = 1; i < nums.size(); ++i) {
if (nums[i - 1] > nums[i]) {
ans = max(ans, ++t);
} else {
t = 1;
}
}
return ans;
}
};
```

```go
func longestMonotonicSubarray(nums []int) int {
ans := 1
t := 1
for i, x := range nums[1:] {
if nums[i] < x {
t++
ans = max(ans, t)
} else {
t = 1
}
}
t = 1
for i, x := range nums[1:] {
if nums[i] > x {
t++
ans = max(ans, t)
} else {
t = 1
}
}
return ans
}
```

```ts
function longestMonotonicSubarray(nums: number[]): number {
let ans = 1;
for (let i = 1, t = 1; i < nums.length; ++i) {
if (nums[i - 1] < nums[i]) {
ans = Math.max(ans, ++t);
} else {
t = 1;
}
}
for (let i = 1, t = 1; i < nums.length; ++i) {
if (nums[i - 1] > nums[i]) {
ans = Math.max(ans, ++t);
} else {
t = 1;
}
}
return ans;
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,24 +67,125 @@

## Solutions

### Solution 1
### Solution 1: Two Passes

We first perform a pass to find the length of the longest strictly increasing subarray, and update the answer. Then we perform another pass to find the length of the longest strictly decreasing subarray, and update the answer again.

The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.

<!-- tabs:start -->

```python

class Solution:
def longestMonotonicSubarray(self, nums: List[int]) -> int:
ans = t = 1
for i, x in enumerate(nums[1:]):
if nums[i] < x:
t += 1
ans = max(ans, t)
else:
t = 1
t = 1
for i, x in enumerate(nums[1:]):
if nums[i] > x:
t += 1
ans = max(ans, t)
else:
t = 1
return ans
```

```java

class Solution {
public int longestMonotonicSubarray(int[] nums) {
int ans = 1;
for (int i = 1, t = 1; i < nums.length; ++i) {
if (nums[i - 1] < nums[i]) {
ans = Math.max(ans, ++t);
} else {
t = 1;
}
}
for (int i = 1, t = 1; i < nums.length; ++i) {
if (nums[i - 1] > nums[i]) {
ans = Math.max(ans, ++t);
} else {
t = 1;
}
}
return ans;
}
}
```

```cpp

class Solution {
public:
int longestMonotonicSubarray(vector<int>& nums) {
int ans = 1;
for (int i = 1, t = 1; i < nums.size(); ++i) {
if (nums[i - 1] < nums[i]) {
ans = max(ans, ++t);
} else {
t = 1;
}
}
for (int i = 1, t = 1; i < nums.size(); ++i) {
if (nums[i - 1] > nums[i]) {
ans = max(ans, ++t);
} else {
t = 1;
}
}
return ans;
}
};
```

```go
func longestMonotonicSubarray(nums []int) int {
ans := 1
t := 1
for i, x := range nums[1:] {
if nums[i] < x {
t++
ans = max(ans, t)
} else {
t = 1
}
}
t = 1
for i, x := range nums[1:] {
if nums[i] > x {
t++
ans = max(ans, t)
} else {
t = 1
}
}
return ans
}
```

```ts
function longestMonotonicSubarray(nums: number[]): number {
let ans = 1;
for (let i = 1, t = 1; i < nums.length; ++i) {
if (nums[i - 1] < nums[i]) {
ans = Math.max(ans, ++t);
} else {
t = 1;
}
}
for (let i = 1, t = 1; i < nums.length; ++i) {
if (nums[i - 1] > nums[i]) {
ans = Math.max(ans, ++t);
} else {
t = 1;
}
}
return ans;
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public:
int longestMonotonicSubarray(vector<int>& nums) {
int ans = 1;
for (int i = 1, t = 1; i < nums.size(); ++i) {
if (nums[i - 1] < nums[i]) {
ans = max(ans, ++t);
} else {
t = 1;
}
}
for (int i = 1, t = 1; i < nums.size(); ++i) {
if (nums[i - 1] > nums[i]) {
ans = max(ans, ++t);
} else {
t = 1;
}
}
return ans;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
func longestMonotonicSubarray(nums []int) int {
ans := 1
t := 1
for i, x := range nums[1:] {
if nums[i] < x {
t++
ans = max(ans, t)
} else {
t = 1
}
}
t = 1
for i, x := range nums[1:] {
if nums[i] > x {
t++
ans = max(ans, t)
} else {
t = 1
}
}
return ans
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
public int longestMonotonicSubarray(int[] nums) {
int ans = 1;
for (int i = 1, t = 1; i < nums.length; ++i) {
if (nums[i - 1] < nums[i]) {
ans = Math.max(ans, ++t);
} else {
t = 1;
}
}
for (int i = 1, t = 1; i < nums.length; ++i) {
if (nums[i - 1] > nums[i]) {
ans = Math.max(ans, ++t);
} else {
t = 1;
}
}
return ans;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution:
def longestMonotonicSubarray(self, nums: List[int]) -> int:
ans = t = 1
for i, x in enumerate(nums[1:]):
if nums[i] < x:
t += 1
ans = max(ans, t)
else:
t = 1
t = 1
for i, x in enumerate(nums[1:]):
if nums[i] > x:
t += 1
ans = max(ans, t)
else:
t = 1
return ans
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function longestMonotonicSubarray(nums: number[]): number {
let ans = 1;
for (let i = 1, t = 1; i < nums.length; ++i) {
if (nums[i - 1] < nums[i]) {
ans = Math.max(ans, ++t);
} else {
t = 1;
}
}
for (let i = 1, t = 1; i < nums.length; ++i) {
if (nums[i - 1] > nums[i]) {
ans = Math.max(ans, ++t);
} else {
t = 1;
}
}
return ans;
}
Loading
Loading