From 77e1a5c8bea310ec092d8ae8af4568fa492ffb26 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Tue, 9 Jan 2024 12:42:22 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.3003 No.3003.Maximize the Number of Partitions After Operations --- .../README.md | 176 +++++++++++++++++- .../README_EN.md | 176 +++++++++++++++++- .../Solution.cpp | 31 +++ .../Solution.go | 36 ++++ .../Solution.java | 36 ++++ .../Solution.py | 23 +++ .../Solution.ts | 43 +++++ 7 files changed, 515 insertions(+), 6 deletions(-) create mode 100644 solution/3000-3099/3003.Maximize the Number of Partitions After Operations/Solution.cpp create mode 100644 solution/3000-3099/3003.Maximize the Number of Partitions After Operations/Solution.go create mode 100644 solution/3000-3099/3003.Maximize the Number of Partitions After Operations/Solution.java create mode 100644 solution/3000-3099/3003.Maximize the Number of Partitions After Operations/Solution.py create mode 100644 solution/3000-3099/3003.Maximize the Number of Partitions After Operations/Solution.ts diff --git a/solution/3000-3099/3003.Maximize the Number of Partitions After Operations/README.md b/solution/3000-3099/3003.Maximize the Number of Partitions After Operations/README.md index ced61d76f3f95..faa28a7968375 100644 --- a/solution/3000-3099/3003.Maximize the Number of Partitions After Operations/README.md +++ b/solution/3000-3099/3003.Maximize the Number of Partitions After Operations/README.md @@ -90,7 +90,29 @@ s 变为 "xayz"。 ```python - +class Solution: + def maxPartitionsAfterOperations(self, s: str, k: int) -> int: + @cache + def dfs(i: int, cur: int, t: int) -> int: + if i >= n: + return 1 + v = 1 << (ord(s[i]) - ord("a")) + nxt = cur | v + if nxt.bit_count() > k: + ans = dfs(i + 1, v, t) + 1 + else: + ans = dfs(i + 1, nxt, t) + if t: + for j in range(26): + nxt = cur | (1 << j) + if nxt.bit_count() > k: + ans = max(ans, dfs(i + 1, 1 << j, 0) + 1) + else: + ans = max(ans, dfs(i + 1, nxt, 0)) + return ans + + n = len(s) + return dfs(0, 0, 1) ``` ### **Java** @@ -98,19 +120,167 @@ s 变为 "xayz"。 ```java - +class Solution { + private Map, Integer> f = new HashMap<>(); + private String s; + private int k; + + public int maxPartitionsAfterOperations(String s, int k) { + this.s = s; + this.k = k; + return dfs(0, 0, 1); + } + + private int dfs(int i, int cur, int t) { + if (i >= s.length()) { + return 1; + } + var key = List.of(i, cur, t); + if (f.containsKey(key)) { + return f.get(key); + } + int v = 1 << (s.charAt(i) - 'a'); + int nxt = cur | v; + int ans = Integer.bitCount(nxt) > k ? dfs(i + 1, v, t) + 1 : dfs(i + 1, nxt, t); + if (t > 0) { + for (int j = 0; j < 26; ++j) { + nxt = cur | (1 << j); + if (Integer.bitCount(nxt) > k) { + ans = Math.max(ans, dfs(i + 1, 1 << j, 0) + 1); + } else { + ans = Math.max(ans, dfs(i + 1, nxt, 0)); + } + } + } + f.put(key, ans); + return ans; + } +} ``` ### **C++** ```cpp - +class Solution { +public: + int maxPartitionsAfterOperations(string s, int k) { + int n = s.size(); + unordered_map f; + function dfs = [&](int i, int cur, int t) { + if (i >= n) { + return 1; + } + long long key = (long long) i << 32 | cur << 1 | t; + if (f.count(key)) { + return f[key]; + } + int v = 1 << (s[i] - 'a'); + int nxt = cur | v; + int ans = __builtin_popcount(nxt) > k ? dfs(i + 1, v, t) + 1 : dfs(i + 1, nxt, t); + if (t) { + for (int j = 0; j < 26; ++j) { + nxt = cur | (1 << j); + if (__builtin_popcount(nxt) > k) { + ans = max(ans, dfs(i + 1, 1 << j, 0) + 1); + } else { + ans = max(ans, dfs(i + 1, nxt, 0)); + } + } + } + return f[key] = ans; + }; + return dfs(0, 0, 1); + } +}; ``` ### **Go** ```go +func maxPartitionsAfterOperations(s string, k int) int { + n := len(s) + type tuple struct{ i, cur, t int } + f := map[tuple]int{} + var dfs func(i, cur, t int) int + dfs = func(i, cur, t int) int { + if i >= n { + return 1 + } + key := tuple{i, cur, t} + if v, ok := f[key]; ok { + return v + } + v := 1 << (s[i] - 'a') + nxt := cur | v + var ans int + if bits.OnesCount(uint(nxt)) > k { + ans = dfs(i+1, v, t) + 1 + } else { + ans = dfs(i+1, nxt, t) + } + if t > 0 { + for j := 0; j < 26; j++ { + nxt = cur | (1 << j) + if bits.OnesCount(uint(nxt)) > k { + ans = max(ans, dfs(i+1, 1< = new Map(); + const dfs = (i: number, cur: number, t: number): number => { + if (i >= n) { + return 1; + } + const key = (BigInt(i) << 27n) | (BigInt(cur) << 1n) | BigInt(t); + if (f.has(key)) { + return f.get(key)!; + } + const v = 1 << (s.charCodeAt(i) - 97); + let nxt = cur | v; + let ans = 0; + if (bitCount(nxt) > k) { + ans = dfs(i + 1, v, t) + 1; + } else { + ans = dfs(i + 1, nxt, t); + } + if (t) { + for (let j = 0; j < 26; ++j) { + nxt = cur | (1 << j); + if (bitCount(nxt) > k) { + ans = Math.max(ans, dfs(i + 1, 1 << j, 0) + 1); + } else { + ans = Math.max(ans, dfs(i + 1, nxt, 0)); + } + } + } + f.set(key, ans); + return ans; + }; + return dfs(0, 0, 1); +} + +function bitCount(i: number): number { + i = i - ((i >>> 1) & 0x55555555); + i = (i & 0x33333333) + ((i >>> 2) & 0x33333333); + i = (i + (i >>> 4)) & 0x0f0f0f0f; + i = i + (i >>> 8); + i = i + (i >>> 16); + return i & 0x3f; +} ``` ### **...** diff --git a/solution/3000-3099/3003.Maximize the Number of Partitions After Operations/README_EN.md b/solution/3000-3099/3003.Maximize the Number of Partitions After Operations/README_EN.md index b59365eedb191..85c85033c94fb 100644 --- a/solution/3000-3099/3003.Maximize the Number of Partitions After Operations/README_EN.md +++ b/solution/3000-3099/3003.Maximize the Number of Partitions After Operations/README_EN.md @@ -83,25 +83,195 @@ It can be shown that it is not possible to obtain more than 4 partitions. ### **Python3** ```python - +class Solution: + def maxPartitionsAfterOperations(self, s: str, k: int) -> int: + @cache + def dfs(i: int, cur: int, t: int) -> int: + if i >= n: + return 1 + v = 1 << (ord(s[i]) - ord("a")) + nxt = cur | v + if nxt.bit_count() > k: + ans = dfs(i + 1, v, t) + 1 + else: + ans = dfs(i + 1, nxt, t) + if t: + for j in range(26): + nxt = cur | (1 << j) + if nxt.bit_count() > k: + ans = max(ans, dfs(i + 1, 1 << j, 0) + 1) + else: + ans = max(ans, dfs(i + 1, nxt, 0)) + return ans + + n = len(s) + return dfs(0, 0, 1) ``` ### **Java** ```java - +class Solution { + private Map, Integer> f = new HashMap<>(); + private String s; + private int k; + + public int maxPartitionsAfterOperations(String s, int k) { + this.s = s; + this.k = k; + return dfs(0, 0, 1); + } + + private int dfs(int i, int cur, int t) { + if (i >= s.length()) { + return 1; + } + var key = List.of(i, cur, t); + if (f.containsKey(key)) { + return f.get(key); + } + int v = 1 << (s.charAt(i) - 'a'); + int nxt = cur | v; + int ans = Integer.bitCount(nxt) > k ? dfs(i + 1, v, t) + 1 : dfs(i + 1, nxt, t); + if (t > 0) { + for (int j = 0; j < 26; ++j) { + nxt = cur | (1 << j); + if (Integer.bitCount(nxt) > k) { + ans = Math.max(ans, dfs(i + 1, 1 << j, 0) + 1); + } else { + ans = Math.max(ans, dfs(i + 1, nxt, 0)); + } + } + } + f.put(key, ans); + return ans; + } +} ``` ### **C++** ```cpp - +class Solution { +public: + int maxPartitionsAfterOperations(string s, int k) { + int n = s.size(); + unordered_map f; + function dfs = [&](int i, int cur, int t) { + if (i >= n) { + return 1; + } + long long key = (long long) i << 32 | cur << 1 | t; + if (f.count(key)) { + return f[key]; + } + int v = 1 << (s[i] - 'a'); + int nxt = cur | v; + int ans = __builtin_popcount(nxt) > k ? dfs(i + 1, v, t) + 1 : dfs(i + 1, nxt, t); + if (t) { + for (int j = 0; j < 26; ++j) { + nxt = cur | (1 << j); + if (__builtin_popcount(nxt) > k) { + ans = max(ans, dfs(i + 1, 1 << j, 0) + 1); + } else { + ans = max(ans, dfs(i + 1, nxt, 0)); + } + } + } + return f[key] = ans; + }; + return dfs(0, 0, 1); + } +}; ``` ### **Go** ```go +func maxPartitionsAfterOperations(s string, k int) int { + n := len(s) + type tuple struct{ i, cur, t int } + f := map[tuple]int{} + var dfs func(i, cur, t int) int + dfs = func(i, cur, t int) int { + if i >= n { + return 1 + } + key := tuple{i, cur, t} + if v, ok := f[key]; ok { + return v + } + v := 1 << (s[i] - 'a') + nxt := cur | v + var ans int + if bits.OnesCount(uint(nxt)) > k { + ans = dfs(i+1, v, t) + 1 + } else { + ans = dfs(i+1, nxt, t) + } + if t > 0 { + for j := 0; j < 26; j++ { + nxt = cur | (1 << j) + if bits.OnesCount(uint(nxt)) > k { + ans = max(ans, dfs(i+1, 1< = new Map(); + const dfs = (i: number, cur: number, t: number): number => { + if (i >= n) { + return 1; + } + const key = (BigInt(i) << 27n) | (BigInt(cur) << 1n) | BigInt(t); + if (f.has(key)) { + return f.get(key)!; + } + const v = 1 << (s.charCodeAt(i) - 97); + let nxt = cur | v; + let ans = 0; + if (bitCount(nxt) > k) { + ans = dfs(i + 1, v, t) + 1; + } else { + ans = dfs(i + 1, nxt, t); + } + if (t) { + for (let j = 0; j < 26; ++j) { + nxt = cur | (1 << j); + if (bitCount(nxt) > k) { + ans = Math.max(ans, dfs(i + 1, 1 << j, 0) + 1); + } else { + ans = Math.max(ans, dfs(i + 1, nxt, 0)); + } + } + } + f.set(key, ans); + return ans; + }; + return dfs(0, 0, 1); +} + +function bitCount(i: number): number { + i = i - ((i >>> 1) & 0x55555555); + i = (i & 0x33333333) + ((i >>> 2) & 0x33333333); + i = (i + (i >>> 4)) & 0x0f0f0f0f; + i = i + (i >>> 8); + i = i + (i >>> 16); + return i & 0x3f; +} ``` ### **...** diff --git a/solution/3000-3099/3003.Maximize the Number of Partitions After Operations/Solution.cpp b/solution/3000-3099/3003.Maximize the Number of Partitions After Operations/Solution.cpp new file mode 100644 index 0000000000000..bdd700377b40c --- /dev/null +++ b/solution/3000-3099/3003.Maximize the Number of Partitions After Operations/Solution.cpp @@ -0,0 +1,31 @@ +class Solution { +public: + int maxPartitionsAfterOperations(string s, int k) { + int n = s.size(); + unordered_map f; + function dfs = [&](int i, int cur, int t) { + if (i >= n) { + return 1; + } + long long key = (long long) i << 32 | cur << 1 | t; + if (f.count(key)) { + return f[key]; + } + int v = 1 << (s[i] - 'a'); + int nxt = cur | v; + int ans = __builtin_popcount(nxt) > k ? dfs(i + 1, v, t) + 1 : dfs(i + 1, nxt, t); + if (t) { + for (int j = 0; j < 26; ++j) { + nxt = cur | (1 << j); + if (__builtin_popcount(nxt) > k) { + ans = max(ans, dfs(i + 1, 1 << j, 0) + 1); + } else { + ans = max(ans, dfs(i + 1, nxt, 0)); + } + } + } + return f[key] = ans; + }; + return dfs(0, 0, 1); + } +}; \ No newline at end of file diff --git a/solution/3000-3099/3003.Maximize the Number of Partitions After Operations/Solution.go b/solution/3000-3099/3003.Maximize the Number of Partitions After Operations/Solution.go new file mode 100644 index 0000000000000..921e1544952d2 --- /dev/null +++ b/solution/3000-3099/3003.Maximize the Number of Partitions After Operations/Solution.go @@ -0,0 +1,36 @@ +func maxPartitionsAfterOperations(s string, k int) int { + n := len(s) + type tuple struct{ i, cur, t int } + f := map[tuple]int{} + var dfs func(i, cur, t int) int + dfs = func(i, cur, t int) int { + if i >= n { + return 1 + } + key := tuple{i, cur, t} + if v, ok := f[key]; ok { + return v + } + v := 1 << (s[i] - 'a') + nxt := cur | v + var ans int + if bits.OnesCount(uint(nxt)) > k { + ans = dfs(i+1, v, t) + 1 + } else { + ans = dfs(i+1, nxt, t) + } + if t > 0 { + for j := 0; j < 26; j++ { + nxt = cur | (1 << j) + if bits.OnesCount(uint(nxt)) > k { + ans = max(ans, dfs(i+1, 1<, Integer> f = new HashMap<>(); + private String s; + private int k; + + public int maxPartitionsAfterOperations(String s, int k) { + this.s = s; + this.k = k; + return dfs(0, 0, 1); + } + + private int dfs(int i, int cur, int t) { + if (i >= s.length()) { + return 1; + } + var key = List.of(i, cur, t); + if (f.containsKey(key)) { + return f.get(key); + } + int v = 1 << (s.charAt(i) - 'a'); + int nxt = cur | v; + int ans = Integer.bitCount(nxt) > k ? dfs(i + 1, v, t) + 1 : dfs(i + 1, nxt, t); + if (t > 0) { + for (int j = 0; j < 26; ++j) { + nxt = cur | (1 << j); + if (Integer.bitCount(nxt) > k) { + ans = Math.max(ans, dfs(i + 1, 1 << j, 0) + 1); + } else { + ans = Math.max(ans, dfs(i + 1, nxt, 0)); + } + } + } + f.put(key, ans); + return ans; + } +} \ No newline at end of file diff --git a/solution/3000-3099/3003.Maximize the Number of Partitions After Operations/Solution.py b/solution/3000-3099/3003.Maximize the Number of Partitions After Operations/Solution.py new file mode 100644 index 0000000000000..b5958e0542986 --- /dev/null +++ b/solution/3000-3099/3003.Maximize the Number of Partitions After Operations/Solution.py @@ -0,0 +1,23 @@ +class Solution: + def maxPartitionsAfterOperations(self, s: str, k: int) -> int: + @cache + def dfs(i: int, cur: int, t: int) -> int: + if i >= n: + return 1 + v = 1 << (ord(s[i]) - ord("a")) + nxt = cur | v + if nxt.bit_count() > k: + ans = dfs(i + 1, v, t) + 1 + else: + ans = dfs(i + 1, nxt, t) + if t: + for j in range(26): + nxt = cur | (1 << j) + if nxt.bit_count() > k: + ans = max(ans, dfs(i + 1, 1 << j, 0) + 1) + else: + ans = max(ans, dfs(i + 1, nxt, 0)) + return ans + + n = len(s) + return dfs(0, 0, 1) diff --git a/solution/3000-3099/3003.Maximize the Number of Partitions After Operations/Solution.ts b/solution/3000-3099/3003.Maximize the Number of Partitions After Operations/Solution.ts new file mode 100644 index 0000000000000..7c70f33cc6542 --- /dev/null +++ b/solution/3000-3099/3003.Maximize the Number of Partitions After Operations/Solution.ts @@ -0,0 +1,43 @@ +function maxPartitionsAfterOperations(s: string, k: number): number { + const n = s.length; + const f: Map = new Map(); + const dfs = (i: number, cur: number, t: number): number => { + if (i >= n) { + return 1; + } + const key = (BigInt(i) << 27n) | (BigInt(cur) << 1n) | BigInt(t); + if (f.has(key)) { + return f.get(key)!; + } + const v = 1 << (s.charCodeAt(i) - 97); + let nxt = cur | v; + let ans = 0; + if (bitCount(nxt) > k) { + ans = dfs(i + 1, v, t) + 1; + } else { + ans = dfs(i + 1, nxt, t); + } + if (t) { + for (let j = 0; j < 26; ++j) { + nxt = cur | (1 << j); + if (bitCount(nxt) > k) { + ans = Math.max(ans, dfs(i + 1, 1 << j, 0) + 1); + } else { + ans = Math.max(ans, dfs(i + 1, nxt, 0)); + } + } + } + f.set(key, ans); + return ans; + }; + return dfs(0, 0, 1); +} + +function bitCount(i: number): number { + i = i - ((i >>> 1) & 0x55555555); + i = (i & 0x33333333) + ((i >>> 2) & 0x33333333); + i = (i + (i >>> 4)) & 0x0f0f0f0f; + i = i + (i >>> 8); + i = i + (i >>> 16); + return i & 0x3f; +}