Skip to content

Commit

Permalink
feat: add solutions to lc problems: No.3105,3106 (#2550)
Browse files Browse the repository at this point in the history
* No.3105.Longest Strictly Increasing or Strictly Decreasing Subarray
* No.3106.Lexicographically Smallest String After Operations With
  Constraint
  • Loading branch information
yanglbme authored Apr 7, 2024
1 parent d7593ed commit 05da8f0
Show file tree
Hide file tree
Showing 14 changed files with 551 additions and 16 deletions.
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

0 comments on commit 05da8f0

Please sign in to comment.