From 3931c2252a539e7e166d1d4fdddaaa65e8399b70 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sun, 30 Mar 2025 11:26:09 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.3499 No.3499.Maximize Active Section with Trade I --- .../README.md | 131 +++++++++++++++++- .../README_EN.md | 131 +++++++++++++++++- .../Solution.cpp | 26 ++++ .../Solution.go | 22 +++ .../Solution.java | 25 ++++ .../Solution.py | 18 +++ .../Solution.ts | 23 +++ 7 files changed, 368 insertions(+), 8 deletions(-) create mode 100644 solution/3400-3499/3499.Maximize Active Section with Trade I/Solution.cpp create mode 100644 solution/3400-3499/3499.Maximize Active Section with Trade I/Solution.go create mode 100644 solution/3400-3499/3499.Maximize Active Section with Trade I/Solution.java create mode 100644 solution/3400-3499/3499.Maximize Active Section with Trade I/Solution.py create mode 100644 solution/3400-3499/3499.Maximize Active Section with Trade I/Solution.ts diff --git a/solution/3400-3499/3499.Maximize Active Section with Trade I/README.md b/solution/3400-3499/3499.Maximize Active Section with Trade I/README.md index 4fc1f099dfb03..38d2e933099f5 100644 --- a/solution/3400-3499/3499.Maximize Active Section with Trade I/README.md +++ b/solution/3400-3499/3499.Maximize Active Section with Trade I/README.md @@ -109,32 +109,155 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3499.Ma -### 方法一 +### 方法一:贪心 + 双指针 + +题目实际上等价于求字符串 $\textit{s}$ 中,字符 `'1'` 的数量,再加上相邻两个连续的字符 `'0'` 串中 ``'0'` 的最大数量。 + +因此,我们可以使用双指针来遍历字符串 $\textit{s}$,用一个变量 $\textit{mx}$ 来记录相邻两个连续的字符 `'0'` 串中 `'0'` 的最大数量。我们还需要一个变量 $\textit{pre}$ 来记录上一个连续的字符 `'0'` 串的数量。 + +每一次,我们统计当前连续相同字符的数量 $\textit{cnt}$,如果当前字符为 `'1'`,则将 $\textit{cnt}$ 加入到答案中;如果当前字符为 `'0'`,则将 $\textit{mx}$ 更新为 $\textit{mx} = \max(\textit{mx}, \textit{pre} + \textit{cnt})$,并将 $\textit{pre}$ 更新为 $\textit{cnt}$。最后,我们将答案加上 $\textit{mx}$ 即可。 + +时间复杂度 $O(n)$,其中 $n$ 为字符串 $\textit{s}$ 的长度。空间复杂度 $O(1)$。 #### Python3 ```python - +class Solution: + def maxActiveSectionsAfterTrade(self, s: str) -> int: + n = len(s) + ans = i = 0 + pre, mx = -inf, 0 + while i < n: + j = i + 1 + while j < n and s[j] == s[i]: + j += 1 + cur = j - i + if s[i] == "1": + ans += cur + else: + mx = max(mx, pre + cur) + pre = cur + i = j + ans += mx + return ans ``` #### Java ```java - +class Solution { + public int maxActiveSectionsAfterTrade(String s) { + int n = s.length(); + int ans = 0, i = 0; + int pre = Integer.MIN_VALUE, mx = 0; + + while (i < n) { + int j = i + 1; + while (j < n && s.charAt(j) == s.charAt(i)) { + j++; + } + int cur = j - i; + if (s.charAt(i) == '1') { + ans += cur; + } else { + mx = Math.max(mx, pre + cur); + pre = cur; + } + i = j; + } + + ans += mx; + return ans; + } +} ``` #### C++ ```cpp - +class Solution { +public: + int maxActiveSectionsAfterTrade(std::string s) { + int n = s.length(); + int ans = 0, i = 0; + int pre = INT_MIN, mx = 0; + + while (i < n) { + int j = i + 1; + while (j < n && s[j] == s[i]) { + j++; + } + int cur = j - i; + if (s[i] == '1') { + ans += cur; + } else { + mx = std::max(mx, pre + cur); + pre = cur; + } + i = j; + } + + ans += mx; + return ans; + } +}; ``` #### Go ```go +func maxActiveSectionsAfterTrade(s string) (ans int) { + n := len(s) + pre, mx := math.MinInt, 0 + + for i := 0; i < n; { + j := i + 1 + for j < n && s[j] == s[i] { + j++ + } + cur := j - i + if s[i] == '1' { + ans += cur + } else { + mx = max(mx, pre+cur) + pre = cur + } + i = j + } + + ans += mx + return +} +``` +#### TypeScript + +```ts +function maxActiveSectionsAfterTrade(s: string): number { + let n = s.length; + let [ans, mx] = [0, 0]; + let pre = Number.MIN_SAFE_INTEGER; + + for (let i = 0; i < n; ) { + let j = i + 1; + while (j < n && s[j] === s[i]) { + j++; + } + let cur = j - i; + if (s[i] === '1') { + ans += cur; + } else { + mx = Math.max(mx, pre + cur); + pre = cur; + } + i = j; + } + + ans += mx; + return ans; +} ``` diff --git a/solution/3400-3499/3499.Maximize Active Section with Trade I/README_EN.md b/solution/3400-3499/3499.Maximize Active Section with Trade I/README_EN.md index a8a51b5d77ce2..316b5c450e549 100644 --- a/solution/3400-3499/3499.Maximize Active Section with Trade I/README_EN.md +++ b/solution/3400-3499/3499.Maximize Active Section with Trade I/README_EN.md @@ -107,32 +107,155 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3499.Ma -### Solution 1 +### Solution 1: Greedy + Two Pointers + +The problem is essentially equivalent to finding the number of `'1'` characters in the string $\textit{s}$, plus the maximum number of `'0'` characters in two adjacent consecutive `'0'` segments. + +Thus, we can use two pointers to traverse the string $\textit{s}$. Use a variable $\textit{mx}$ to record the maximum number of `'0'` characters in two adjacent consecutive `'0'` segments. We also need a variable $\textit{pre}$ to record the number of `'0'` characters in the previous consecutive `'0'` segment. + +Each time, we count the number of consecutive identical characters $\textit{cnt}$. If the current character is `'1'`, add $\textit{cnt}$ to the answer. If the current character is `'0'`, update $\textit{mx}$ as $\textit{mx} = \max(\textit{mx}, \textit{pre} + \textit{cnt})$, and update $\textit{pre}$ to $\textit{cnt}$. Finally, add $\textit{mx}$ to the answer. + +Time complexity is $O(n)$, where $n$ is the length of the string $\textit{s}$. Space complexity is $O(1)$. #### Python3 ```python - +class Solution: + def maxActiveSectionsAfterTrade(self, s: str) -> int: + n = len(s) + ans = i = 0 + pre, mx = -inf, 0 + while i < n: + j = i + 1 + while j < n and s[j] == s[i]: + j += 1 + cur = j - i + if s[i] == "1": + ans += cur + else: + mx = max(mx, pre + cur) + pre = cur + i = j + ans += mx + return ans ``` #### Java ```java - +class Solution { + public int maxActiveSectionsAfterTrade(String s) { + int n = s.length(); + int ans = 0, i = 0; + int pre = Integer.MIN_VALUE, mx = 0; + + while (i < n) { + int j = i + 1; + while (j < n && s.charAt(j) == s.charAt(i)) { + j++; + } + int cur = j - i; + if (s.charAt(i) == '1') { + ans += cur; + } else { + mx = Math.max(mx, pre + cur); + pre = cur; + } + i = j; + } + + ans += mx; + return ans; + } +} ``` #### C++ ```cpp - +class Solution { +public: + int maxActiveSectionsAfterTrade(std::string s) { + int n = s.length(); + int ans = 0, i = 0; + int pre = INT_MIN, mx = 0; + + while (i < n) { + int j = i + 1; + while (j < n && s[j] == s[i]) { + j++; + } + int cur = j - i; + if (s[i] == '1') { + ans += cur; + } else { + mx = std::max(mx, pre + cur); + pre = cur; + } + i = j; + } + + ans += mx; + return ans; + } +}; ``` #### Go ```go +func maxActiveSectionsAfterTrade(s string) (ans int) { + n := len(s) + pre, mx := math.MinInt, 0 + + for i := 0; i < n; { + j := i + 1 + for j < n && s[j] == s[i] { + j++ + } + cur := j - i + if s[i] == '1' { + ans += cur + } else { + mx = max(mx, pre+cur) + pre = cur + } + i = j + } + + ans += mx + return +} +``` +#### TypeScript + +```ts +function maxActiveSectionsAfterTrade(s: string): number { + let n = s.length; + let [ans, mx] = [0, 0]; + let pre = Number.MIN_SAFE_INTEGER; + + for (let i = 0; i < n; ) { + let j = i + 1; + while (j < n && s[j] === s[i]) { + j++; + } + let cur = j - i; + if (s[i] === '1') { + ans += cur; + } else { + mx = Math.max(mx, pre + cur); + pre = cur; + } + i = j; + } + + ans += mx; + return ans; +} ``` diff --git a/solution/3400-3499/3499.Maximize Active Section with Trade I/Solution.cpp b/solution/3400-3499/3499.Maximize Active Section with Trade I/Solution.cpp new file mode 100644 index 0000000000000..d1f50d66118e8 --- /dev/null +++ b/solution/3400-3499/3499.Maximize Active Section with Trade I/Solution.cpp @@ -0,0 +1,26 @@ +class Solution { +public: + int maxActiveSectionsAfterTrade(std::string s) { + int n = s.length(); + int ans = 0, i = 0; + int pre = INT_MIN, mx = 0; + + while (i < n) { + int j = i + 1; + while (j < n && s[j] == s[i]) { + j++; + } + int cur = j - i; + if (s[i] == '1') { + ans += cur; + } else { + mx = std::max(mx, pre + cur); + pre = cur; + } + i = j; + } + + ans += mx; + return ans; + } +}; diff --git a/solution/3400-3499/3499.Maximize Active Section with Trade I/Solution.go b/solution/3400-3499/3499.Maximize Active Section with Trade I/Solution.go new file mode 100644 index 0000000000000..5ff332fa824ae --- /dev/null +++ b/solution/3400-3499/3499.Maximize Active Section with Trade I/Solution.go @@ -0,0 +1,22 @@ +func maxActiveSectionsAfterTrade(s string) (ans int) { + n := len(s) + pre, mx := math.MinInt, 0 + + for i := 0; i < n; { + j := i + 1 + for j < n && s[j] == s[i] { + j++ + } + cur := j - i + if s[i] == '1' { + ans += cur + } else { + mx = max(mx, pre+cur) + pre = cur + } + i = j + } + + ans += mx + return +} \ No newline at end of file diff --git a/solution/3400-3499/3499.Maximize Active Section with Trade I/Solution.java b/solution/3400-3499/3499.Maximize Active Section with Trade I/Solution.java new file mode 100644 index 0000000000000..1925280e8441f --- /dev/null +++ b/solution/3400-3499/3499.Maximize Active Section with Trade I/Solution.java @@ -0,0 +1,25 @@ +class Solution { + public int maxActiveSectionsAfterTrade(String s) { + int n = s.length(); + int ans = 0, i = 0; + int pre = Integer.MIN_VALUE, mx = 0; + + while (i < n) { + int j = i + 1; + while (j < n && s.charAt(j) == s.charAt(i)) { + j++; + } + int cur = j - i; + if (s.charAt(i) == '1') { + ans += cur; + } else { + mx = Math.max(mx, pre + cur); + pre = cur; + } + i = j; + } + + ans += mx; + return ans; + } +} \ No newline at end of file diff --git a/solution/3400-3499/3499.Maximize Active Section with Trade I/Solution.py b/solution/3400-3499/3499.Maximize Active Section with Trade I/Solution.py new file mode 100644 index 0000000000000..36244f75a177b --- /dev/null +++ b/solution/3400-3499/3499.Maximize Active Section with Trade I/Solution.py @@ -0,0 +1,18 @@ +class Solution: + def maxActiveSectionsAfterTrade(self, s: str) -> int: + n = len(s) + ans = i = 0 + pre, mx = -inf, 0 + while i < n: + j = i + 1 + while j < n and s[j] == s[i]: + j += 1 + cur = j - i + if s[i] == "1": + ans += cur + else: + mx = max(mx, pre + cur) + pre = cur + i = j + ans += mx + return ans diff --git a/solution/3400-3499/3499.Maximize Active Section with Trade I/Solution.ts b/solution/3400-3499/3499.Maximize Active Section with Trade I/Solution.ts new file mode 100644 index 0000000000000..60ee5300bf6e3 --- /dev/null +++ b/solution/3400-3499/3499.Maximize Active Section with Trade I/Solution.ts @@ -0,0 +1,23 @@ +function maxActiveSectionsAfterTrade(s: string): number { + let n = s.length; + let [ans, mx] = [0, 0]; + let pre = Number.MIN_SAFE_INTEGER; + + for (let i = 0; i < n; ) { + let j = i + 1; + while (j < n && s[j] === s[i]) { + j++; + } + let cur = j - i; + if (s[i] === '1') { + ans += cur; + } else { + mx = Math.max(mx, pre + cur); + pre = cur; + } + i = j; + } + + ans += mx; + return ans; +}