From 4d400e690f6bfa98469447a3d6e68f0755379fae Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 18 Dec 2025 21:11:00 +0800 Subject: [PATCH] feat: add solutions to lc problems: No.3714,3717 --- .../README.md | 363 +++++++++++++++++- .../README_EN.md | 363 +++++++++++++++++- .../Solution.cpp | 78 ++++ .../Solution.go | 81 ++++ .../Solution.java | 75 ++++ .../Solution.py | 47 +++ .../Solution.ts | 75 ++++ .../README.md | 172 ++++++++- .../README_EN.md | 170 +++++++- .../Solution.cpp | 30 ++ .../Solution.go | 25 ++ .../Solution.java | 32 ++ .../Solution.py | 13 + .../Solution.rs | 34 ++ .../Solution.ts | 24 ++ 15 files changed, 1569 insertions(+), 13 deletions(-) create mode 100644 solution/3700-3799/3714.Longest Balanced Substring II/Solution.cpp create mode 100644 solution/3700-3799/3714.Longest Balanced Substring II/Solution.go create mode 100644 solution/3700-3799/3714.Longest Balanced Substring II/Solution.java create mode 100644 solution/3700-3799/3714.Longest Balanced Substring II/Solution.py create mode 100644 solution/3700-3799/3714.Longest Balanced Substring II/Solution.ts create mode 100644 solution/3700-3799/3717.Minimum Operations to Make the Array Beautiful/Solution.cpp create mode 100644 solution/3700-3799/3717.Minimum Operations to Make the Array Beautiful/Solution.go create mode 100644 solution/3700-3799/3717.Minimum Operations to Make the Array Beautiful/Solution.java create mode 100644 solution/3700-3799/3717.Minimum Operations to Make the Array Beautiful/Solution.py create mode 100644 solution/3700-3799/3717.Minimum Operations to Make the Array Beautiful/Solution.rs create mode 100644 solution/3700-3799/3717.Minimum Operations to Make the Array Beautiful/Solution.ts diff --git a/solution/3700-3799/3714.Longest Balanced Substring II/README.md b/solution/3700-3799/3714.Longest Balanced Substring II/README.md index fbb2687ea263b..be36fa2b97a1d 100644 --- a/solution/3700-3799/3714.Longest Balanced Substring II/README.md +++ b/solution/3700-3799/3714.Longest Balanced Substring II/README.md @@ -89,25 +89,382 @@ tags: #### Python3 ```python - +class Solution: + def longestBalanced(self, s: str) -> int: + def calc1(s: str) -> int: + res = 0 + i, n = 0, len(s) + while i < n: + j = i + 1 + while j < n and s[j] == s[i]: + j += 1 + res = max(res, j - i) + i = j + return res + + def calc2(s: str, a: str, b: str) -> int: + res = 0 + i, n = 0, len(s) + while i < n: + while i < n and s[i] not in (a, b): + i += 1 + pos = {0: i - 1} + d = 0 + while i < n and s[i] in (a, b): + d += 1 if s[i] == a else -1 + if d in pos: + res = max(res, i - pos[d]) + else: + pos[d] = i + i += 1 + return res + + def calc3(s: str) -> int: + pos = {(0, 0): -1} + cnt = Counter() + res = 0 + for i, c in enumerate(s): + cnt[c] += 1 + k = (cnt["a"] - cnt["b"], cnt["b"] - cnt["c"]) + if k in pos: + res = max(res, i - pos[k]) + else: + pos[k] = i + return res + + x = calc1(s) + y = max(calc2(s, "a", "b"), calc2(s, "b", "c"), calc2(s, "a", "c")) + z = calc3(s) + return max(x, y, z) ``` #### Java ```java - +class Solution { + public int longestBalanced(String s) { + char[] cs = s.toCharArray(); + int x = calc1(cs); + int y = Math.max(calc2(cs, 'a', 'b'), Math.max(calc2(cs, 'b', 'c'), calc2(cs, 'a', 'c'))); + int z = calc3(cs); + return Math.max(x, Math.max(y, z)); + } + + private int calc1(char[] s) { + int res = 0; + int i = 0, n = s.length; + while (i < n) { + int j = i + 1; + while (j < n && s[j] == s[i]) { + j++; + } + res = Math.max(res, j - i); + i = j; + } + return res; + } + + private int calc2(char[] s, char a, char b) { + int res = 0; + int i = 0, n = s.length; + while (i < n) { + while (i < n && s[i] != a && s[i] != b) { + i++; + } + Map pos = new HashMap<>(); + pos.put(0, i - 1); + int d = 0; + while (i < n && (s[i] == a || s[i] == b)) { + d += (s[i] == a) ? 1 : -1; + Integer prev = pos.get(d); + if (prev != null) { + res = Math.max(res, i - prev); + } else { + pos.put(d, i); + } + i++; + } + } + return res; + } + + private int calc3(char[] s) { + Map pos = new HashMap<>(); + pos.put(f(0, 0), -1); + + int[] cnt = new int[3]; + int res = 0; + + for (int i = 0; i < s.length; i++) { + char c = s[i]; + ++cnt[c - 'a']; + int x = cnt[0] - cnt[1]; + int y = cnt[1] - cnt[2]; + long k = f(x, y); + + Integer prev = pos.get(k); + if (prev != null) { + res = Math.max(res, i - prev); + } else { + pos.put(k, i); + } + } + return res; + } + + private long f(int x, int y) { + return (x + 100000) << 20 | (y + 100000); + } +} ``` #### C++ ```cpp - +class Solution { +public: + int longestBalanced(string s) { + int x = calc1(s); + int y = max({calc2(s, 'a', 'b'), calc2(s, 'b', 'c'), calc2(s, 'a', 'c')}); + int z = calc3(s); + return max({x, y, z}); + } + +private: + int calc1(const string& s) { + int res = 0; + int i = 0, n = s.size(); + while (i < n) { + int j = i + 1; + while (j < n && s[j] == s[i]) { + ++j; + } + res = max(res, j - i); + i = j; + } + return res; + } + + int calc2(const string& s, char a, char b) { + int res = 0; + int i = 0, n = s.size(); + while (i < n) { + while (i < n && s[i] != a && s[i] != b) { + ++i; + } + + unordered_map pos; + pos[0] = i - 1; + + int d = 0; + while (i < n && (s[i] == a || s[i] == b)) { + d += (s[i] == a) ? 1 : -1; + auto it = pos.find(d); + if (it != pos.end()) { + res = max(res, i - it->second); + } else { + pos[d] = i; + } + i++; + } + } + return res; + } + + static long long f(int x, int y) { + return ((long long) (x + 100000) << 20) | (long long) (y + 100000); + } + + int calc3(const string& s) { + unordered_map pos; + pos[f(0, 0)] = -1; + + int cnt[3] = {0, 0, 0}; + int res = 0; + + for (int i = 0; i < (int) s.size(); i++) { + char c = s[i]; + ++cnt[c - 'a']; + int x = cnt[0] - cnt[1]; + int y = cnt[1] - cnt[2]; + long long k = f(x, y); + + auto it = pos.find(k); + if (it != pos.end()) { + res = max(res, i - it->second); + } else { + pos[k] = i; + } + } + return res; + } +}; ``` #### Go ```go +func longestBalanced(s string) int { + x := calc1(s) + y := max(calc2(s, 'a', 'b'), calc2(s, 'b', 'c'), calc2(s, 'a', 'c')) + z := calc3(s) + return max(x, max(y, z)) +} + +func calc1(s string) int { + res := 0 + n := len(s) + i := 0 + for i < n { + j := i + 1 + for j < n && s[j] == s[i] { + j++ + } + if j-i > res { + res = j - i + } + i = j + } + return res +} + +func calc2(s string, a, b byte) int { + res := 0 + n := len(s) + i := 0 + for i < n { + for i < n && s[i] != a && s[i] != b { + i++ + } + pos := map[int]int{0: i - 1} + d := 0 + for i < n && (s[i] == a || s[i] == b) { + if s[i] == a { + d++ + } else { + d-- + } + if prev, ok := pos[d]; ok { + if i-prev > res { + res = i - prev + } + } else { + pos[d] = i + } + i++ + } + } + return res +} + +type key struct { + x, y int +} + +func calc3(s string) int { + pos := make(map[key]int) + pos[key{0, 0}] = -1 + + cnt := [3]int{} + res := 0 + + for i := 0; i < len(s); i++ { + c := s[i] + cnt[c-'a']++ + x := cnt[0] - cnt[1] + y := cnt[1] - cnt[2] + k := key{x, y} + + if j, ok := pos[k]; ok { + if i-j > res { + res = i - j + } + } else { + pos[k] = i + } + } + return res +} +``` +#### TypeScript + +```ts +function longestBalanced(s: string): number { + const x = calc1(s); + const y = Math.max(calc2(s, 'a', 'b'), calc2(s, 'b', 'c'), calc2(s, 'a', 'c')); + const z = calc3(s); + return Math.max(x, y, z); +} + +function calc1(s: string): number { + let res = 0; + const n = s.length; + let i = 0; + while (i < n) { + let j = i + 1; + while (j < n && s[j] === s[i]) j++; + res = Math.max(res, j - i); + i = j; + } + return res; +} + +function calc2(s: string, a: string, b: string): number { + let res = 0; + const n = s.length; + let i = 0; + + while (i < n) { + while (i < n && s[i] !== a && s[i] !== b) i++; + + const pos = new Map(); + pos.set(0, i - 1); + + let d = 0; + while (i < n && (s[i] === a || s[i] === b)) { + d += s[i] === a ? 1 : -1; + + const prev = pos.get(d); + if (prev !== undefined) { + res = Math.max(res, i - prev); + } else { + pos.set(d, i); + } + i++; + } + } + return res; +} + +function calc3(s: string): number { + const pos = new Map(); + pos.set(key(0, 0), -1); + + const cnt = [0, 0, 0]; + let res = 0; + + for (let i = 0; i < s.length; i++) { + const c = s.charCodeAt(i) - 97; + cnt[c]++; + + const x = cnt[0] - cnt[1]; + const y = cnt[1] - cnt[2]; + const k = key(x, y); + + const prev = pos.get(k); + if (prev !== undefined) { + res = Math.max(res, i - prev); + } else { + pos.set(k, i); + } + } + return res; +} + +function key(x: number, y: number): string { + return x + '#' + y; +} ``` diff --git a/solution/3700-3799/3714.Longest Balanced Substring II/README_EN.md b/solution/3700-3799/3714.Longest Balanced Substring II/README_EN.md index f5bfb848cfd4c..a60766c491db3 100644 --- a/solution/3700-3799/3714.Longest Balanced Substring II/README_EN.md +++ b/solution/3700-3799/3714.Longest Balanced Substring II/README_EN.md @@ -84,25 +84,382 @@ tags: #### Python3 ```python - +class Solution: + def longestBalanced(self, s: str) -> int: + def calc1(s: str) -> int: + res = 0 + i, n = 0, len(s) + while i < n: + j = i + 1 + while j < n and s[j] == s[i]: + j += 1 + res = max(res, j - i) + i = j + return res + + def calc2(s: str, a: str, b: str) -> int: + res = 0 + i, n = 0, len(s) + while i < n: + while i < n and s[i] not in (a, b): + i += 1 + pos = {0: i - 1} + d = 0 + while i < n and s[i] in (a, b): + d += 1 if s[i] == a else -1 + if d in pos: + res = max(res, i - pos[d]) + else: + pos[d] = i + i += 1 + return res + + def calc3(s: str) -> int: + pos = {(0, 0): -1} + cnt = Counter() + res = 0 + for i, c in enumerate(s): + cnt[c] += 1 + k = (cnt["a"] - cnt["b"], cnt["b"] - cnt["c"]) + if k in pos: + res = max(res, i - pos[k]) + else: + pos[k] = i + return res + + x = calc1(s) + y = max(calc2(s, "a", "b"), calc2(s, "b", "c"), calc2(s, "a", "c")) + z = calc3(s) + return max(x, y, z) ``` #### Java ```java - +class Solution { + public int longestBalanced(String s) { + char[] cs = s.toCharArray(); + int x = calc1(cs); + int y = Math.max(calc2(cs, 'a', 'b'), Math.max(calc2(cs, 'b', 'c'), calc2(cs, 'a', 'c'))); + int z = calc3(cs); + return Math.max(x, Math.max(y, z)); + } + + private int calc1(char[] s) { + int res = 0; + int i = 0, n = s.length; + while (i < n) { + int j = i + 1; + while (j < n && s[j] == s[i]) { + j++; + } + res = Math.max(res, j - i); + i = j; + } + return res; + } + + private int calc2(char[] s, char a, char b) { + int res = 0; + int i = 0, n = s.length; + while (i < n) { + while (i < n && s[i] != a && s[i] != b) { + i++; + } + Map pos = new HashMap<>(); + pos.put(0, i - 1); + int d = 0; + while (i < n && (s[i] == a || s[i] == b)) { + d += (s[i] == a) ? 1 : -1; + Integer prev = pos.get(d); + if (prev != null) { + res = Math.max(res, i - prev); + } else { + pos.put(d, i); + } + i++; + } + } + return res; + } + + private int calc3(char[] s) { + Map pos = new HashMap<>(); + pos.put(f(0, 0), -1); + + int[] cnt = new int[3]; + int res = 0; + + for (int i = 0; i < s.length; i++) { + char c = s[i]; + ++cnt[c - 'a']; + int x = cnt[0] - cnt[1]; + int y = cnt[1] - cnt[2]; + long k = f(x, y); + + Integer prev = pos.get(k); + if (prev != null) { + res = Math.max(res, i - prev); + } else { + pos.put(k, i); + } + } + return res; + } + + private long f(int x, int y) { + return (x + 100000) << 20 | (y + 100000); + } +} ``` #### C++ ```cpp - +class Solution { +public: + int longestBalanced(string s) { + int x = calc1(s); + int y = max({calc2(s, 'a', 'b'), calc2(s, 'b', 'c'), calc2(s, 'a', 'c')}); + int z = calc3(s); + return max({x, y, z}); + } + +private: + int calc1(const string& s) { + int res = 0; + int i = 0, n = s.size(); + while (i < n) { + int j = i + 1; + while (j < n && s[j] == s[i]) { + ++j; + } + res = max(res, j - i); + i = j; + } + return res; + } + + int calc2(const string& s, char a, char b) { + int res = 0; + int i = 0, n = s.size(); + while (i < n) { + while (i < n && s[i] != a && s[i] != b) { + ++i; + } + + unordered_map pos; + pos[0] = i - 1; + + int d = 0; + while (i < n && (s[i] == a || s[i] == b)) { + d += (s[i] == a) ? 1 : -1; + auto it = pos.find(d); + if (it != pos.end()) { + res = max(res, i - it->second); + } else { + pos[d] = i; + } + i++; + } + } + return res; + } + + static long long f(int x, int y) { + return ((long long) (x + 100000) << 20) | (long long) (y + 100000); + } + + int calc3(const string& s) { + unordered_map pos; + pos[f(0, 0)] = -1; + + int cnt[3] = {0, 0, 0}; + int res = 0; + + for (int i = 0; i < (int) s.size(); i++) { + char c = s[i]; + ++cnt[c - 'a']; + int x = cnt[0] - cnt[1]; + int y = cnt[1] - cnt[2]; + long long k = f(x, y); + + auto it = pos.find(k); + if (it != pos.end()) { + res = max(res, i - it->second); + } else { + pos[k] = i; + } + } + return res; + } +}; ``` #### Go ```go +func longestBalanced(s string) int { + x := calc1(s) + y := max(calc2(s, 'a', 'b'), calc2(s, 'b', 'c'), calc2(s, 'a', 'c')) + z := calc3(s) + return max(x, max(y, z)) +} + +func calc1(s string) int { + res := 0 + n := len(s) + i := 0 + for i < n { + j := i + 1 + for j < n && s[j] == s[i] { + j++ + } + if j-i > res { + res = j - i + } + i = j + } + return res +} + +func calc2(s string, a, b byte) int { + res := 0 + n := len(s) + i := 0 + for i < n { + for i < n && s[i] != a && s[i] != b { + i++ + } + pos := map[int]int{0: i - 1} + d := 0 + for i < n && (s[i] == a || s[i] == b) { + if s[i] == a { + d++ + } else { + d-- + } + if prev, ok := pos[d]; ok { + if i-prev > res { + res = i - prev + } + } else { + pos[d] = i + } + i++ + } + } + return res +} + +type key struct { + x, y int +} + +func calc3(s string) int { + pos := make(map[key]int) + pos[key{0, 0}] = -1 + + cnt := [3]int{} + res := 0 + + for i := 0; i < len(s); i++ { + c := s[i] + cnt[c-'a']++ + x := cnt[0] - cnt[1] + y := cnt[1] - cnt[2] + k := key{x, y} + + if j, ok := pos[k]; ok { + if i-j > res { + res = i - j + } + } else { + pos[k] = i + } + } + return res +} +``` +#### TypeScript + +```ts +function longestBalanced(s: string): number { + const x = calc1(s); + const y = Math.max(calc2(s, 'a', 'b'), calc2(s, 'b', 'c'), calc2(s, 'a', 'c')); + const z = calc3(s); + return Math.max(x, y, z); +} + +function calc1(s: string): number { + let res = 0; + const n = s.length; + let i = 0; + while (i < n) { + let j = i + 1; + while (j < n && s[j] === s[i]) j++; + res = Math.max(res, j - i); + i = j; + } + return res; +} + +function calc2(s: string, a: string, b: string): number { + let res = 0; + const n = s.length; + let i = 0; + + while (i < n) { + while (i < n && s[i] !== a && s[i] !== b) i++; + + const pos = new Map(); + pos.set(0, i - 1); + + let d = 0; + while (i < n && (s[i] === a || s[i] === b)) { + d += s[i] === a ? 1 : -1; + + const prev = pos.get(d); + if (prev !== undefined) { + res = Math.max(res, i - prev); + } else { + pos.set(d, i); + } + i++; + } + } + return res; +} + +function calc3(s: string): number { + const pos = new Map(); + pos.set(key(0, 0), -1); + + const cnt = [0, 0, 0]; + let res = 0; + + for (let i = 0; i < s.length; i++) { + const c = s.charCodeAt(i) - 97; + cnt[c]++; + + const x = cnt[0] - cnt[1]; + const y = cnt[1] - cnt[2]; + const k = key(x, y); + + const prev = pos.get(k); + if (prev !== undefined) { + res = Math.max(res, i - prev); + } else { + pos.set(k, i); + } + } + return res; +} + +function key(x: number, y: number): string { + return x + '#' + y; +} ``` diff --git a/solution/3700-3799/3714.Longest Balanced Substring II/Solution.cpp b/solution/3700-3799/3714.Longest Balanced Substring II/Solution.cpp new file mode 100644 index 0000000000000..a5efd5dda6ca2 --- /dev/null +++ b/solution/3700-3799/3714.Longest Balanced Substring II/Solution.cpp @@ -0,0 +1,78 @@ +class Solution { +public: + int longestBalanced(string s) { + int x = calc1(s); + int y = max({calc2(s, 'a', 'b'), calc2(s, 'b', 'c'), calc2(s, 'a', 'c')}); + int z = calc3(s); + return max({x, y, z}); + } + +private: + int calc1(const string& s) { + int res = 0; + int i = 0, n = s.size(); + while (i < n) { + int j = i + 1; + while (j < n && s[j] == s[i]) { + ++j; + } + res = max(res, j - i); + i = j; + } + return res; + } + + int calc2(const string& s, char a, char b) { + int res = 0; + int i = 0, n = s.size(); + while (i < n) { + while (i < n && s[i] != a && s[i] != b) { + ++i; + } + + unordered_map pos; + pos[0] = i - 1; + + int d = 0; + while (i < n && (s[i] == a || s[i] == b)) { + d += (s[i] == a) ? 1 : -1; + auto it = pos.find(d); + if (it != pos.end()) { + res = max(res, i - it->second); + } else { + pos[d] = i; + } + i++; + } + } + return res; + } + + static long long f(int x, int y) { + return ((long long) (x + 100000) << 20) | (long long) (y + 100000); + } + + int calc3(const string& s) { + unordered_map pos; + pos[f(0, 0)] = -1; + + int cnt[3] = {0, 0, 0}; + int res = 0; + + for (int i = 0; i < (int) s.size(); i++) { + char c = s[i]; + ++cnt[c - 'a']; + int x = cnt[0] - cnt[1]; + int y = cnt[1] - cnt[2]; + long long k = f(x, y); + + auto it = pos.find(k); + if (it != pos.end()) { + res = max(res, i - it->second); + } else { + pos[k] = i; + } + } + return res; + } +}; diff --git a/solution/3700-3799/3714.Longest Balanced Substring II/Solution.go b/solution/3700-3799/3714.Longest Balanced Substring II/Solution.go new file mode 100644 index 0000000000000..897d0805cf504 --- /dev/null +++ b/solution/3700-3799/3714.Longest Balanced Substring II/Solution.go @@ -0,0 +1,81 @@ +func longestBalanced(s string) int { + x := calc1(s) + y := max(calc2(s, 'a', 'b'), calc2(s, 'b', 'c'), calc2(s, 'a', 'c')) + z := calc3(s) + return max(x, max(y, z)) +} + +func calc1(s string) int { + res := 0 + n := len(s) + i := 0 + for i < n { + j := i + 1 + for j < n && s[j] == s[i] { + j++ + } + if j-i > res { + res = j - i + } + i = j + } + return res +} + +func calc2(s string, a, b byte) int { + res := 0 + n := len(s) + i := 0 + for i < n { + for i < n && s[i] != a && s[i] != b { + i++ + } + pos := map[int]int{0: i - 1} + d := 0 + for i < n && (s[i] == a || s[i] == b) { + if s[i] == a { + d++ + } else { + d-- + } + if prev, ok := pos[d]; ok { + if i-prev > res { + res = i - prev + } + } else { + pos[d] = i + } + i++ + } + } + return res +} + +type key struct { + x, y int +} + +func calc3(s string) int { + pos := make(map[key]int) + pos[key{0, 0}] = -1 + + cnt := [3]int{} + res := 0 + + for i := 0; i < len(s); i++ { + c := s[i] + cnt[c-'a']++ + x := cnt[0] - cnt[1] + y := cnt[1] - cnt[2] + k := key{x, y} + + if j, ok := pos[k]; ok { + if i-j > res { + res = i - j + } + } else { + pos[k] = i + } + } + return res +} diff --git a/solution/3700-3799/3714.Longest Balanced Substring II/Solution.java b/solution/3700-3799/3714.Longest Balanced Substring II/Solution.java new file mode 100644 index 0000000000000..28af1fb661b25 --- /dev/null +++ b/solution/3700-3799/3714.Longest Balanced Substring II/Solution.java @@ -0,0 +1,75 @@ +class Solution { + public int longestBalanced(String s) { + char[] cs = s.toCharArray(); + int x = calc1(cs); + int y = Math.max(calc2(cs, 'a', 'b'), Math.max(calc2(cs, 'b', 'c'), calc2(cs, 'a', 'c'))); + int z = calc3(cs); + return Math.max(x, Math.max(y, z)); + } + + private int calc1(char[] s) { + int res = 0; + int i = 0, n = s.length; + while (i < n) { + int j = i + 1; + while (j < n && s[j] == s[i]) { + j++; + } + res = Math.max(res, j - i); + i = j; + } + return res; + } + + private int calc2(char[] s, char a, char b) { + int res = 0; + int i = 0, n = s.length; + while (i < n) { + while (i < n && s[i] != a && s[i] != b) { + i++; + } + Map pos = new HashMap<>(); + pos.put(0, i - 1); + int d = 0; + while (i < n && (s[i] == a || s[i] == b)) { + d += (s[i] == a) ? 1 : -1; + Integer prev = pos.get(d); + if (prev != null) { + res = Math.max(res, i - prev); + } else { + pos.put(d, i); + } + i++; + } + } + return res; + } + + private int calc3(char[] s) { + Map pos = new HashMap<>(); + pos.put(f(0, 0), -1); + + int[] cnt = new int[3]; + int res = 0; + + for (int i = 0; i < s.length; i++) { + char c = s[i]; + ++cnt[c - 'a']; + int x = cnt[0] - cnt[1]; + int y = cnt[1] - cnt[2]; + long k = f(x, y); + + Integer prev = pos.get(k); + if (prev != null) { + res = Math.max(res, i - prev); + } else { + pos.put(k, i); + } + } + return res; + } + + private long f(int x, int y) { + return (x + 100000) << 20 | (y + 100000); + } +} diff --git a/solution/3700-3799/3714.Longest Balanced Substring II/Solution.py b/solution/3700-3799/3714.Longest Balanced Substring II/Solution.py new file mode 100644 index 0000000000000..48214851457c9 --- /dev/null +++ b/solution/3700-3799/3714.Longest Balanced Substring II/Solution.py @@ -0,0 +1,47 @@ +class Solution: + def longestBalanced(self, s: str) -> int: + def calc1(s: str) -> int: + res = 0 + i, n = 0, len(s) + while i < n: + j = i + 1 + while j < n and s[j] == s[i]: + j += 1 + res = max(res, j - i) + i = j + return res + + def calc2(s: str, a: str, b: str) -> int: + res = 0 + i, n = 0, len(s) + while i < n: + while i < n and s[i] not in (a, b): + i += 1 + pos = {0: i - 1} + d = 0 + while i < n and s[i] in (a, b): + d += 1 if s[i] == a else -1 + if d in pos: + res = max(res, i - pos[d]) + else: + pos[d] = i + i += 1 + return res + + def calc3(s: str) -> int: + pos = {(0, 0): -1} + cnt = Counter() + res = 0 + for i, c in enumerate(s): + cnt[c] += 1 + k = (cnt["a"] - cnt["b"], cnt["b"] - cnt["c"]) + if k in pos: + res = max(res, i - pos[k]) + else: + pos[k] = i + return res + + x = calc1(s) + y = max(calc2(s, "a", "b"), calc2(s, "b", "c"), calc2(s, "a", "c")) + z = calc3(s) + return max(x, y, z) diff --git a/solution/3700-3799/3714.Longest Balanced Substring II/Solution.ts b/solution/3700-3799/3714.Longest Balanced Substring II/Solution.ts new file mode 100644 index 0000000000000..77800b064ddc1 --- /dev/null +++ b/solution/3700-3799/3714.Longest Balanced Substring II/Solution.ts @@ -0,0 +1,75 @@ +function longestBalanced(s: string): number { + const x = calc1(s); + const y = Math.max(calc2(s, 'a', 'b'), calc2(s, 'b', 'c'), calc2(s, 'a', 'c')); + const z = calc3(s); + return Math.max(x, y, z); +} + +function calc1(s: string): number { + let res = 0; + const n = s.length; + let i = 0; + while (i < n) { + let j = i + 1; + while (j < n && s[j] === s[i]) j++; + res = Math.max(res, j - i); + i = j; + } + return res; +} + +function calc2(s: string, a: string, b: string): number { + let res = 0; + const n = s.length; + let i = 0; + + while (i < n) { + while (i < n && s[i] !== a && s[i] !== b) i++; + + const pos = new Map(); + pos.set(0, i - 1); + + let d = 0; + while (i < n && (s[i] === a || s[i] === b)) { + d += s[i] === a ? 1 : -1; + + const prev = pos.get(d); + if (prev !== undefined) { + res = Math.max(res, i - prev); + } else { + pos.set(d, i); + } + i++; + } + } + return res; +} + +function calc3(s: string): number { + const pos = new Map(); + pos.set(key(0, 0), -1); + + const cnt = [0, 0, 0]; + let res = 0; + + for (let i = 0; i < s.length; i++) { + const c = s.charCodeAt(i) - 97; + cnt[c]++; + + const x = cnt[0] - cnt[1]; + const y = cnt[1] - cnt[2]; + const k = key(x, y); + + const prev = pos.get(k); + if (prev !== undefined) { + res = Math.max(res, i - prev); + } else { + pos.set(k, i); + } + } + return res; +} + +function key(x: number, y: number): string { + return x + '#' + y; +} diff --git a/solution/3700-3799/3717.Minimum Operations to Make the Array Beautiful/README.md b/solution/3700-3799/3717.Minimum Operations to Make the Array Beautiful/README.md index be609cf1a4d8e..d466b52061d69 100644 --- a/solution/3700-3799/3717.Minimum Operations to Make the Array Beautiful/README.md +++ b/solution/3700-3799/3717.Minimum Operations to Make the Array Beautiful/README.md @@ -78,32 +78,196 @@ tags: -### 方法一 +### 方法一:动态规划 + 枚举 #### Python3 ```python - +class Solution: + def minOperations(self, nums: List[int]) -> int: + f = {nums[0]: 0} + for x in nums[1:]: + g = {} + for pre, s in f.items(): + cur = (x + pre - 1) // pre * pre + while cur <= 100: + if cur not in g or g[cur] > s + cur - x: + g[cur] = s + cur - x + cur += pre + f = g + return min(f.values()) ``` #### Java ```java - +class Solution { + public int minOperations(int[] nums) { + Map f = new HashMap<>(); + f.put(nums[0], 0); + + for (int i = 1; i < nums.length; i++) { + int x = nums[i]; + Map g = new HashMap<>(); + + for (var entry : f.entrySet()) { + int pre = entry.getKey(); + int s = entry.getValue(); + + int cur = (x + pre - 1) / pre * pre; + while (cur <= 100) { + int val = s + (cur - x); + if (!g.containsKey(cur) || g.get(cur) > val) { + g.put(cur, val); + } + cur += pre; + } + } + f = g; + } + + int ans = Integer.MAX_VALUE; + for (int v : f.values()) { + ans = Math.min(ans, v); + } + return ans; + } +} ``` #### C++ ```cpp - +class Solution { +public: + int minOperations(vector& nums) { + unordered_map f; + f[nums[0]] = 0; + + for (int i = 1; i < nums.size(); i++) { + int x = nums[i]; + unordered_map g; + for (auto [pre, s] : f) { + int cur = (x + pre - 1) / pre * pre; + while (cur <= 100) { + int val = s + (cur - x); + auto jt = g.find(cur); + if (jt == g.end() || jt->second > val) { + g[cur] = val; + } + cur += pre; + } + } + f = move(g); + } + + int ans = INT_MAX; + for (auto& it : f) { + ans = min(ans, it.second); + } + return ans; + } +}; ``` #### Go ```go +func minOperations(nums []int) int { + f := map[int]int{nums[0]: 0} + + for i := 1; i < len(nums); i++ { + x := nums[i] + g := make(map[int]int) + for pre, s := range f { + cur := (x + pre - 1) / pre * pre + for cur <= 100 { + val := s + (cur - x) + if old, ok := g[cur]; !ok || old > val { + g[cur] = val + } + cur += pre + } + } + f = g + } + + ans := math.MaxInt32 + for _, v := range f { + ans = min(ans, v) + } + return ans +} +``` + +#### TypeScript + +```ts +function minOperations(nums: number[]): number { + let f = new Map(); + f.set(nums[0], 0); + + for (let i = 1; i < nums.length; i++) { + const x = nums[i]; + const g = new Map(); + + for (const [pre, s] of f.entries()) { + let cur = Math.floor((x + pre - 1) / pre) * pre; + while (cur <= 100) { + const val = s + (cur - x); + const old = g.get(cur); + if (old === undefined || old > val) { + g.set(cur, val); + } + cur += pre; + } + } + f = g; + } + + return Math.min(...f.values()); +} +``` +#### Rust + +```rust +use std::collections::HashMap; + +impl Solution { + pub fn min_operations(nums: Vec) -> i32 { + let mut f: HashMap = HashMap::new(); + f.insert(nums[0], 0); + + for i in 1..nums.len() { + let x = nums[i]; + let mut g: HashMap = HashMap::new(); + + for (&pre, &s) in f.iter() { + let mut cur = ((x + pre - 1) / pre) * pre; + while cur <= 100 { + let val = s + (cur - x); + match g.get(&cur) { + None => { + g.insert(cur, val); + } + Some(&old) => { + if val < old { + g.insert(cur, val); + } + } + } + cur += pre; + } + } + f = g; + } + + *f.values().min().unwrap() + } +} ``` diff --git a/solution/3700-3799/3717.Minimum Operations to Make the Array Beautiful/README_EN.md b/solution/3700-3799/3717.Minimum Operations to Make the Array Beautiful/README_EN.md index 9865fc532c0c8..412ef0404428d 100644 --- a/solution/3700-3799/3717.Minimum Operations to Make the Array Beautiful/README_EN.md +++ b/solution/3700-3799/3717.Minimum Operations to Make the Array Beautiful/README_EN.md @@ -83,25 +83,189 @@ tags: #### Python3 ```python - +class Solution: + def minOperations(self, nums: List[int]) -> int: + f = {nums[0]: 0} + for x in nums[1:]: + g = {} + for pre, s in f.items(): + cur = (x + pre - 1) // pre * pre + while cur <= 100: + if cur not in g or g[cur] > s + cur - x: + g[cur] = s + cur - x + cur += pre + f = g + return min(f.values()) ``` #### Java ```java - +class Solution { + public int minOperations(int[] nums) { + Map f = new HashMap<>(); + f.put(nums[0], 0); + + for (int i = 1; i < nums.length; i++) { + int x = nums[i]; + Map g = new HashMap<>(); + + for (var entry : f.entrySet()) { + int pre = entry.getKey(); + int s = entry.getValue(); + + int cur = (x + pre - 1) / pre * pre; + while (cur <= 100) { + int val = s + (cur - x); + if (!g.containsKey(cur) || g.get(cur) > val) { + g.put(cur, val); + } + cur += pre; + } + } + f = g; + } + + int ans = Integer.MAX_VALUE; + for (int v : f.values()) { + ans = Math.min(ans, v); + } + return ans; + } +} ``` #### C++ ```cpp - +class Solution { +public: + int minOperations(vector& nums) { + unordered_map f; + f[nums[0]] = 0; + + for (int i = 1; i < nums.size(); i++) { + int x = nums[i]; + unordered_map g; + for (auto [pre, s] : f) { + int cur = (x + pre - 1) / pre * pre; + while (cur <= 100) { + int val = s + (cur - x); + auto jt = g.find(cur); + if (jt == g.end() || jt->second > val) { + g[cur] = val; + } + cur += pre; + } + } + f = move(g); + } + + int ans = INT_MAX; + for (auto& it : f) { + ans = min(ans, it.second); + } + return ans; + } +}; ``` #### Go ```go +func minOperations(nums []int) int { + f := map[int]int{nums[0]: 0} + + for i := 1; i < len(nums); i++ { + x := nums[i] + g := make(map[int]int) + for pre, s := range f { + cur := (x + pre - 1) / pre * pre + for cur <= 100 { + val := s + (cur - x) + if old, ok := g[cur]; !ok || old > val { + g[cur] = val + } + cur += pre + } + } + f = g + } + + ans := math.MaxInt32 + for _, v := range f { + ans = min(ans, v) + } + return ans +} +``` + +#### TypeScript + +```ts +function minOperations(nums: number[]): number { + let f = new Map(); + f.set(nums[0], 0); + + for (let i = 1; i < nums.length; i++) { + const x = nums[i]; + const g = new Map(); + + for (const [pre, s] of f.entries()) { + let cur = Math.floor((x + pre - 1) / pre) * pre; + while (cur <= 100) { + const val = s + (cur - x); + const old = g.get(cur); + if (old === undefined || old > val) { + g.set(cur, val); + } + cur += pre; + } + } + f = g; + } + + return Math.min(...f.values()); +} +``` +#### Rust + +```rust +use std::collections::HashMap; + +impl Solution { + pub fn min_operations(nums: Vec) -> i32 { + let mut f: HashMap = HashMap::new(); + f.insert(nums[0], 0); + + for i in 1..nums.len() { + let x = nums[i]; + let mut g: HashMap = HashMap::new(); + + for (&pre, &s) in f.iter() { + let mut cur = ((x + pre - 1) / pre) * pre; + while cur <= 100 { + let val = s + (cur - x); + match g.get(&cur) { + None => { + g.insert(cur, val); + } + Some(&old) => { + if val < old { + g.insert(cur, val); + } + } + } + cur += pre; + } + } + f = g; + } + + *f.values().min().unwrap() + } +} ``` diff --git a/solution/3700-3799/3717.Minimum Operations to Make the Array Beautiful/Solution.cpp b/solution/3700-3799/3717.Minimum Operations to Make the Array Beautiful/Solution.cpp new file mode 100644 index 0000000000000..a8627b31b690c --- /dev/null +++ b/solution/3700-3799/3717.Minimum Operations to Make the Array Beautiful/Solution.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + int minOperations(vector& nums) { + unordered_map f; + f[nums[0]] = 0; + + for (int i = 1; i < nums.size(); i++) { + int x = nums[i]; + unordered_map g; + for (auto [pre, s] : f) { + int cur = (x + pre - 1) / pre * pre; + while (cur <= 100) { + int val = s + (cur - x); + auto jt = g.find(cur); + if (jt == g.end() || jt->second > val) { + g[cur] = val; + } + cur += pre; + } + } + f = move(g); + } + + int ans = INT_MAX; + for (auto& it : f) { + ans = min(ans, it.second); + } + return ans; + } +}; diff --git a/solution/3700-3799/3717.Minimum Operations to Make the Array Beautiful/Solution.go b/solution/3700-3799/3717.Minimum Operations to Make the Array Beautiful/Solution.go new file mode 100644 index 0000000000000..bc616e841bfbd --- /dev/null +++ b/solution/3700-3799/3717.Minimum Operations to Make the Array Beautiful/Solution.go @@ -0,0 +1,25 @@ +func minOperations(nums []int) int { + f := map[int]int{nums[0]: 0} + + for i := 1; i < len(nums); i++ { + x := nums[i] + g := make(map[int]int) + for pre, s := range f { + cur := (x + pre - 1) / pre * pre + for cur <= 100 { + val := s + (cur - x) + if old, ok := g[cur]; !ok || old > val { + g[cur] = val + } + cur += pre + } + } + f = g + } + + ans := math.MaxInt32 + for _, v := range f { + ans = min(ans, v) + } + return ans +} diff --git a/solution/3700-3799/3717.Minimum Operations to Make the Array Beautiful/Solution.java b/solution/3700-3799/3717.Minimum Operations to Make the Array Beautiful/Solution.java new file mode 100644 index 0000000000000..2e78f56c0c482 --- /dev/null +++ b/solution/3700-3799/3717.Minimum Operations to Make the Array Beautiful/Solution.java @@ -0,0 +1,32 @@ +class Solution { + public int minOperations(int[] nums) { + Map f = new HashMap<>(); + f.put(nums[0], 0); + + for (int i = 1; i < nums.length; i++) { + int x = nums[i]; + Map g = new HashMap<>(); + + for (var entry : f.entrySet()) { + int pre = entry.getKey(); + int s = entry.getValue(); + + int cur = (x + pre - 1) / pre * pre; + while (cur <= 100) { + int val = s + (cur - x); + if (!g.containsKey(cur) || g.get(cur) > val) { + g.put(cur, val); + } + cur += pre; + } + } + f = g; + } + + int ans = Integer.MAX_VALUE; + for (int v : f.values()) { + ans = Math.min(ans, v); + } + return ans; + } +} diff --git a/solution/3700-3799/3717.Minimum Operations to Make the Array Beautiful/Solution.py b/solution/3700-3799/3717.Minimum Operations to Make the Array Beautiful/Solution.py new file mode 100644 index 0000000000000..eaa7d332e713d --- /dev/null +++ b/solution/3700-3799/3717.Minimum Operations to Make the Array Beautiful/Solution.py @@ -0,0 +1,13 @@ +class Solution: + def minOperations(self, nums: List[int]) -> int: + f = {nums[0]: 0} + for x in nums[1:]: + g = {} + for pre, s in f.items(): + cur = (x + pre - 1) // pre * pre + while cur <= 100: + if cur not in g or g[cur] > s + cur - x: + g[cur] = s + cur - x + cur += pre + f = g + return min(f.values()) diff --git a/solution/3700-3799/3717.Minimum Operations to Make the Array Beautiful/Solution.rs b/solution/3700-3799/3717.Minimum Operations to Make the Array Beautiful/Solution.rs new file mode 100644 index 0000000000000..314d47e9bf535 --- /dev/null +++ b/solution/3700-3799/3717.Minimum Operations to Make the Array Beautiful/Solution.rs @@ -0,0 +1,34 @@ +use std::collections::HashMap; + +impl Solution { + pub fn min_operations(nums: Vec) -> i32 { + let mut f: HashMap = HashMap::new(); + f.insert(nums[0], 0); + + for i in 1..nums.len() { + let x = nums[i]; + let mut g: HashMap = HashMap::new(); + + for (&pre, &s) in f.iter() { + let mut cur = ((x + pre - 1) / pre) * pre; + while cur <= 100 { + let val = s + (cur - x); + match g.get(&cur) { + None => { + g.insert(cur, val); + } + Some(&old) => { + if val < old { + g.insert(cur, val); + } + } + } + cur += pre; + } + } + f = g; + } + + *f.values().min().unwrap() + } +} diff --git a/solution/3700-3799/3717.Minimum Operations to Make the Array Beautiful/Solution.ts b/solution/3700-3799/3717.Minimum Operations to Make the Array Beautiful/Solution.ts new file mode 100644 index 0000000000000..329d2c762796f --- /dev/null +++ b/solution/3700-3799/3717.Minimum Operations to Make the Array Beautiful/Solution.ts @@ -0,0 +1,24 @@ +function minOperations(nums: number[]): number { + let f = new Map(); + f.set(nums[0], 0); + + for (let i = 1; i < nums.length; i++) { + const x = nums[i]; + const g = new Map(); + + for (const [pre, s] of f.entries()) { + let cur = Math.floor((x + pre - 1) / pre) * pre; + while (cur <= 100) { + const val = s + (cur - x); + const old = g.get(cur); + if (old === undefined || old > val) { + g.set(cur, val); + } + cur += pre; + } + } + f = g; + } + + return Math.min(...f.values()); +}