diff --git a/solution/3100-3199/3105.Longest Strictly Increasing or Strictly Decreasing Subarray/README.md b/solution/3100-3199/3105.Longest Strictly Increasing or Strictly Decreasing Subarray/README.md index 006c0066875b8..83eeddbcca9ec 100644 --- a/solution/3100-3199/3105.Longest Strictly Increasing or Strictly Decreasing Subarray/README.md +++ b/solution/3100-3199/3105.Longest Strictly Increasing or Strictly Decreasing Subarray/README.md @@ -73,24 +73,125 @@ ## 解法 -### 方法一 +### 方法一:两次遍历 + +我们先进行一次遍历,找出严格递增的最长子数组长度,更新答案。然后再进行一次遍历,找出严格递减的最长子数组长度,再次更新答案。 + +时间复杂度 $O(n)$,其中 $n$ 是数组的长度。空间复杂度 $O(1)$。 ```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& 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; +} ``` diff --git a/solution/3100-3199/3105.Longest Strictly Increasing or Strictly Decreasing Subarray/README_EN.md b/solution/3100-3199/3105.Longest Strictly Increasing or Strictly Decreasing Subarray/README_EN.md index 1ac1c1ce55173..ccbe478d9d454 100644 --- a/solution/3100-3199/3105.Longest Strictly Increasing or Strictly Decreasing Subarray/README_EN.md +++ b/solution/3100-3199/3105.Longest Strictly Increasing or Strictly Decreasing Subarray/README_EN.md @@ -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)$. ```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& 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; +} ``` diff --git a/solution/3100-3199/3105.Longest Strictly Increasing or Strictly Decreasing Subarray/Solution.cpp b/solution/3100-3199/3105.Longest Strictly Increasing or Strictly Decreasing Subarray/Solution.cpp new file mode 100644 index 0000000000000..b2e9a4d18a9c8 --- /dev/null +++ b/solution/3100-3199/3105.Longest Strictly Increasing or Strictly Decreasing Subarray/Solution.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + int longestMonotonicSubarray(vector& 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; + } +}; \ No newline at end of file diff --git a/solution/3100-3199/3105.Longest Strictly Increasing or Strictly Decreasing Subarray/Solution.go b/solution/3100-3199/3105.Longest Strictly Increasing or Strictly Decreasing Subarray/Solution.go new file mode 100644 index 0000000000000..9179f39cb096e --- /dev/null +++ b/solution/3100-3199/3105.Longest Strictly Increasing or Strictly Decreasing Subarray/Solution.go @@ -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 +} \ No newline at end of file diff --git a/solution/3100-3199/3105.Longest Strictly Increasing or Strictly Decreasing Subarray/Solution.java b/solution/3100-3199/3105.Longest Strictly Increasing or Strictly Decreasing Subarray/Solution.java new file mode 100644 index 0000000000000..8e56fe292dfe6 --- /dev/null +++ b/solution/3100-3199/3105.Longest Strictly Increasing or Strictly Decreasing Subarray/Solution.java @@ -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; + } +} \ No newline at end of file diff --git a/solution/3100-3199/3105.Longest Strictly Increasing or Strictly Decreasing Subarray/Solution.py b/solution/3100-3199/3105.Longest Strictly Increasing or Strictly Decreasing Subarray/Solution.py new file mode 100644 index 0000000000000..ee82ceb38d9cf --- /dev/null +++ b/solution/3100-3199/3105.Longest Strictly Increasing or Strictly Decreasing Subarray/Solution.py @@ -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 diff --git a/solution/3100-3199/3105.Longest Strictly Increasing or Strictly Decreasing Subarray/Solution.ts b/solution/3100-3199/3105.Longest Strictly Increasing or Strictly Decreasing Subarray/Solution.ts new file mode 100644 index 0000000000000..d65a189f524ab --- /dev/null +++ b/solution/3100-3199/3105.Longest Strictly Increasing or Strictly Decreasing Subarray/Solution.ts @@ -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; +} diff --git a/solution/3100-3199/3106.Lexicographically Smallest String After Operations With Constraint/README.md b/solution/3100-3199/3106.Lexicographically Smallest String After Operations With Constraint/README.md index 89385a462b1ad..61ade8a2a4374 100644 --- a/solution/3100-3199/3106.Lexicographically Smallest String After Operations With Constraint/README.md +++ b/solution/3100-3199/3106.Lexicographically Smallest String After Operations With Constraint/README.md @@ -71,24 +71,104 @@ ## 解法 -### 方法一 +### 方法一:枚举 + +我们可以遍历字符串 $s$ 的每个位置,对于每个位置,我们枚举所有小于当前字符的字符,计算改变到这个字符的代价 $d$,如果 $d \leq k$,我们就将当前位置的字符改为这个字符,并将 $k$ 减去 $d$,然后结束枚举,继续遍历下一个位置。 + +遍历结束后,我们就得到了一个满足条件的字符串。 + +时间复杂度 $O(n \times |\Sigma|)$,空间复杂度 $O(n)$。其中 $n$ 是字符串 $s$ 的长度;而 $|\Sigma|$ 是字符集的大小,本题中 $|\Sigma| \leq 26$。 ```python - +class Solution: + def getSmallestString(self, s: str, k: int) -> str: + cs = list(s) + for i, c1 in enumerate(s): + for c2 in ascii_lowercase: + if c2 >= c1: + break + d = min(ord(c1) - ord(c2), 26 - ord(c1) + ord(c2)) + if d <= k: + cs[i] = c2 + k -= d + break + return "".join(cs) ``` ```java - +class Solution { + public String getSmallestString(String s, int k) { + char[] cs = s.toCharArray(); + for (int i = 0; i < cs.length; ++i) { + char c1 = cs[i]; + for (char c2 = 'a'; c2 < c1; ++c2) { + int d = Math.min(c1 - c2, 26 - c1 + c2); + if (d <= k) { + cs[i] = c2; + k -= d; + break; + } + } + } + return new String(cs); + } +} ``` ```cpp - +class Solution { +public: + string getSmallestString(string s, int k) { + for (int i = 0; i < s.size(); ++i) { + char c1 = s[i]; + for (char c2 = 'a'; c2 < c1; ++c2) { + int d = min(c1 - c2, 26 - c1 + c2); + if (d <= k) { + s[i] = c2; + k -= d; + break; + } + } + } + return s; + } +}; ``` ```go +func getSmallestString(s string, k int) string { + cs := []byte(s) + for i, c1 := range cs { + for c2 := byte('a'); c2 < c1; c2++ { + d := int(min(c1-c2, 26-c1+c2)) + if d <= k { + cs[i] = c2 + k -= d + break + } + } + } + return string(cs) +} +``` +```ts +function getSmallestString(s: string, k: number): string { + const cs: string[] = s.split(''); + for (let i = 0; i < s.length; ++i) { + for (let j = 97; j < s[i].charCodeAt(0); ++j) { + const d = Math.min(s[i].charCodeAt(0) - j, 26 - s[i].charCodeAt(0) + j); + if (d <= k) { + cs[i] = String.fromCharCode(j); + k -= d; + break; + } + } + } + return cs.join(''); +} ``` diff --git a/solution/3100-3199/3106.Lexicographically Smallest String After Operations With Constraint/README_EN.md b/solution/3100-3199/3106.Lexicographically Smallest String After Operations With Constraint/README_EN.md index 7091dd28c5979..f7b692b2d4da3 100644 --- a/solution/3100-3199/3106.Lexicographically Smallest String After Operations With Constraint/README_EN.md +++ b/solution/3100-3199/3106.Lexicographically Smallest String After Operations With Constraint/README_EN.md @@ -68,24 +68,104 @@ ## Solutions -### Solution 1 +### Solution 1: Enumeration + +We can traverse each position of the string $s$. For each position, we enumerate all characters less than the current character, calculate the cost $d$ to change to this character. If $d \leq k$, we change the current character to this character, subtract $d$ from $k$, end the enumeration, and continue to the next position. + +After the traversal, we get a string that meets the conditions. + +The time complexity is $O(n \times |\Sigma|)$, and the space complexity is $O(n)$. Here, $n$ is the length of the string $s$, and $|\Sigma|$ is the size of the character set. In this problem, $|\Sigma| \leq 26$. ```python - +class Solution: + def getSmallestString(self, s: str, k: int) -> str: + cs = list(s) + for i, c1 in enumerate(s): + for c2 in ascii_lowercase: + if c2 >= c1: + break + d = min(ord(c1) - ord(c2), 26 - ord(c1) + ord(c2)) + if d <= k: + cs[i] = c2 + k -= d + break + return "".join(cs) ``` ```java - +class Solution { + public String getSmallestString(String s, int k) { + char[] cs = s.toCharArray(); + for (int i = 0; i < cs.length; ++i) { + char c1 = cs[i]; + for (char c2 = 'a'; c2 < c1; ++c2) { + int d = Math.min(c1 - c2, 26 - c1 + c2); + if (d <= k) { + cs[i] = c2; + k -= d; + break; + } + } + } + return new String(cs); + } +} ``` ```cpp - +class Solution { +public: + string getSmallestString(string s, int k) { + for (int i = 0; i < s.size(); ++i) { + char c1 = s[i]; + for (char c2 = 'a'; c2 < c1; ++c2) { + int d = min(c1 - c2, 26 - c1 + c2); + if (d <= k) { + s[i] = c2; + k -= d; + break; + } + } + } + return s; + } +}; ``` ```go +func getSmallestString(s string, k int) string { + cs := []byte(s) + for i, c1 := range cs { + for c2 := byte('a'); c2 < c1; c2++ { + d := int(min(c1-c2, 26-c1+c2)) + if d <= k { + cs[i] = c2 + k -= d + break + } + } + } + return string(cs) +} +``` +```ts +function getSmallestString(s: string, k: number): string { + const cs: string[] = s.split(''); + for (let i = 0; i < s.length; ++i) { + for (let j = 97; j < s[i].charCodeAt(0); ++j) { + const d = Math.min(s[i].charCodeAt(0) - j, 26 - s[i].charCodeAt(0) + j); + if (d <= k) { + cs[i] = String.fromCharCode(j); + k -= d; + break; + } + } + } + return cs.join(''); +} ``` diff --git a/solution/3100-3199/3106.Lexicographically Smallest String After Operations With Constraint/Solution.cpp b/solution/3100-3199/3106.Lexicographically Smallest String After Operations With Constraint/Solution.cpp new file mode 100644 index 0000000000000..c0c1263b4ef4e --- /dev/null +++ b/solution/3100-3199/3106.Lexicographically Smallest String After Operations With Constraint/Solution.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + string getSmallestString(string s, int k) { + for (int i = 0; i < s.size(); ++i) { + char c1 = s[i]; + for (char c2 = 'a'; c2 < c1; ++c2) { + int d = min(c1 - c2, 26 - c1 + c2); + if (d <= k) { + s[i] = c2; + k -= d; + break; + } + } + } + return s; + } +}; \ No newline at end of file diff --git a/solution/3100-3199/3106.Lexicographically Smallest String After Operations With Constraint/Solution.go b/solution/3100-3199/3106.Lexicographically Smallest String After Operations With Constraint/Solution.go new file mode 100644 index 0000000000000..9a92558f54351 --- /dev/null +++ b/solution/3100-3199/3106.Lexicographically Smallest String After Operations With Constraint/Solution.go @@ -0,0 +1,14 @@ +func getSmallestString(s string, k int) string { + cs := []byte(s) + for i, c1 := range cs { + for c2 := byte('a'); c2 < c1; c2++ { + d := int(min(c1-c2, 26-c1+c2)) + if d <= k { + cs[i] = c2 + k -= d + break + } + } + } + return string(cs) +} \ No newline at end of file diff --git a/solution/3100-3199/3106.Lexicographically Smallest String After Operations With Constraint/Solution.java b/solution/3100-3199/3106.Lexicographically Smallest String After Operations With Constraint/Solution.java new file mode 100644 index 0000000000000..ba3231e4264ff --- /dev/null +++ b/solution/3100-3199/3106.Lexicographically Smallest String After Operations With Constraint/Solution.java @@ -0,0 +1,17 @@ +class Solution { + public String getSmallestString(String s, int k) { + char[] cs = s.toCharArray(); + for (int i = 0; i < cs.length; ++i) { + char c1 = cs[i]; + for (char c2 = 'a'; c2 < c1; ++c2) { + int d = Math.min(c1 - c2, 26 - c1 + c2); + if (d <= k) { + cs[i] = c2; + k -= d; + break; + } + } + } + return new String(cs); + } +} \ No newline at end of file diff --git a/solution/3100-3199/3106.Lexicographically Smallest String After Operations With Constraint/Solution.py b/solution/3100-3199/3106.Lexicographically Smallest String After Operations With Constraint/Solution.py new file mode 100644 index 0000000000000..15cad339ff79f --- /dev/null +++ b/solution/3100-3199/3106.Lexicographically Smallest String After Operations With Constraint/Solution.py @@ -0,0 +1,13 @@ +class Solution: + def getSmallestString(self, s: str, k: int) -> str: + cs = list(s) + for i, c1 in enumerate(s): + for c2 in ascii_lowercase: + if c2 >= c1: + break + d = min(ord(c1) - ord(c2), 26 - ord(c1) + ord(c2)) + if d <= k: + cs[i] = c2 + k -= d + break + return "".join(cs) diff --git a/solution/3100-3199/3106.Lexicographically Smallest String After Operations With Constraint/Solution.ts b/solution/3100-3199/3106.Lexicographically Smallest String After Operations With Constraint/Solution.ts new file mode 100644 index 0000000000000..5079e8f92827f --- /dev/null +++ b/solution/3100-3199/3106.Lexicographically Smallest String After Operations With Constraint/Solution.ts @@ -0,0 +1,14 @@ +function getSmallestString(s: string, k: number): string { + const cs: string[] = s.split(''); + for (let i = 0; i < s.length; ++i) { + for (let j = 97; j < s[i].charCodeAt(0); ++j) { + const d = Math.min(s[i].charCodeAt(0) - j, 26 - s[i].charCodeAt(0) + j); + if (d <= k) { + cs[i] = String.fromCharCode(j); + k -= d; + break; + } + } + } + return cs.join(''); +}