-
-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to lc problems: No.3105,3106 (#2550)
* No.3105.Longest Strictly Increasing or Strictly Decreasing Subarray * No.3106.Lexicographically Smallest String After Operations With Constraint
- Loading branch information
Showing
14 changed files
with
551 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
...n/3100-3199/3105.Longest Strictly Increasing or Strictly Decreasing Subarray/Solution.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
22 changes: 22 additions & 0 deletions
22
...on/3100-3199/3105.Longest Strictly Increasing or Strictly Decreasing Subarray/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
20 changes: 20 additions & 0 deletions
20
.../3100-3199/3105.Longest Strictly Increasing or Strictly Decreasing Subarray/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...on/3100-3199/3105.Longest Strictly Increasing or Strictly Decreasing Subarray/Solution.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
18 changes: 18 additions & 0 deletions
18
...on/3100-3199/3105.Longest Strictly Increasing or Strictly Decreasing Subarray/Solution.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.