From 3553c1915d8b56fe5e9707384d61574faa41b6a9 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Mon, 15 Apr 2024 17:17:31 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.3117 No.3117.Minimum Sum of Values by Dividing Array --- .../README.md | 182 +++++++++++++++++- .../README_EN.md | 182 +++++++++++++++++- .../Solution.cpp | 41 ++++ .../Solution.go | 35 ++++ .../Solution.java | 37 ++++ .../Solution.py | 19 ++ .../Solution.ts | 28 +++ 7 files changed, 516 insertions(+), 8 deletions(-) create mode 100644 solution/3100-3199/3117.Minimum Sum of Values by Dividing Array/Solution.cpp create mode 100644 solution/3100-3199/3117.Minimum Sum of Values by Dividing Array/Solution.go create mode 100644 solution/3100-3199/3117.Minimum Sum of Values by Dividing Array/Solution.java create mode 100644 solution/3100-3199/3117.Minimum Sum of Values by Dividing Array/Solution.py create mode 100644 solution/3100-3199/3117.Minimum Sum of Values by Dividing Array/Solution.ts diff --git a/solution/3100-3199/3117.Minimum Sum of Values by Dividing Array/README.md b/solution/3100-3199/3117.Minimum Sum of Values by Dividing Array/README.md index bfcd57eb066c7..5172f87a4b27e 100644 --- a/solution/3100-3199/3117.Minimum Sum of Values by Dividing Array/README.md +++ b/solution/3100-3199/3117.Minimum Sum of Values by Dividing Array/README.md @@ -84,24 +84,198 @@ ## 解法 -### 方法一 +### 方法一:记忆化搜索 + +我们设计一个函数 $dfs(i, j, a)$,表示从第 $i$ 个元素开始,且当前已经划分了 $j$ 个子数组,且当前待划分的子数组的按位与结果为 $a$ 的情况下,所能得到的可能的最小子数组值之和。那么答案就是 $dfs(0, 0, -1)$。 + +函数 $dfs(i, j, a)$ 的执行过程如下: + +- 如果 $n - i < m - j$,那么说明剩下的元素不足以划分出 $m - j$ 个子数组,返回 $+\infty$。 +- 如果 $j = m$,那么说明已经划分出了 $m$ 个子数组,此时判断 $i = n$ 是否成立,如果成立返回 $0$,否则返回 $+\infty$。 +- 否则,我们将 $a$ 与 $nums[i]$ 进行按位与操作,得到新的 $a$。如果 $a < andValues[j]$,那么说明当前待划分的子数组的按位与结果不满足要求,返回 $+\infty$。否则,我们有两种选择: + - 不划分当前元素,即 $dfs(i + 1, j, a)$。 + - 划分当前元素,即 $dfs(i + 1, j + 1, -1) + nums[i]$。 +- 返回上述两种选择的最小值。 + +为了避免重复计算,我们使用记忆化搜索的方法,将 $dfs(i, j, a)$ 的结果存储在一个哈希表中。 + +时间复杂度 $O(n \times m \times \log M)$,空间复杂度 $O(n \times m \times \log M)$。其中 $n$ 和 $m$ 分别是数组 $nums$ 和 $andValues$ 的长度;而 $M$ 是数组 $nums$ 中的最大值,本题中 $M \leq 10^5$。 ```python - +class Solution: + def minimumValueSum(self, nums: List[int], andValues: List[int]) -> int: + @cache + def dfs(i: int, j: int, a: int) -> int: + if n - i < m - j: + return inf + if j == m: + return 0 if i == n else inf + a &= nums[i] + if a < andValues[j]: + return inf + ans = dfs(i + 1, j, a) + if a == andValues[j]: + ans = min(ans, dfs(i + 1, j + 1, -1) + nums[i]) + return ans + + n, m = len(nums), len(andValues) + ans = dfs(0, 0, -1) + return ans if ans < inf else -1 ``` ```java - +class Solution { + private int[] nums; + private int[] andValues; + private final int inf = 1 << 29; + private Map f = new HashMap<>(); + + public int minimumValueSum(int[] nums, int[] andValues) { + this.nums = nums; + this.andValues = andValues; + int ans = dfs(0, 0, -1); + return ans >= inf ? -1 : ans; + } + + private int dfs(int i, int j, int a) { + if (nums.length - i < andValues.length - j) { + return inf; + } + if (j == andValues.length) { + return i == nums.length ? 0 : inf; + } + a &= nums[i]; + if (a < andValues[j]) { + return inf; + } + long key = (long) i << 36 | (long) j << 32 | a; + if (f.containsKey(key)) { + return f.get(key); + } + + int ans = dfs(i + 1, j, a); + if (a == andValues[j]) { + ans = Math.min(ans, dfs(i + 1, j + 1, -1) + nums[i]); + } + f.put(key, ans); + return ans; + } +} ``` ```cpp - +class Solution { +public: + int minimumValueSum(vector& nums, vector& andValues) { + this->nums = nums; + this->andValues = andValues; + n = nums.size(); + m = andValues.size(); + int ans = dfs(0, 0, -1); + return ans >= inf ? -1 : ans; + } + +private: + vector nums; + vector andValues; + int n; + int m; + const int inf = 1 << 29; + unordered_map f; + + int dfs(int i, int j, int a) { + if (n - i < m - j) { + return inf; + } + if (j == m) { + return i == n ? 0 : inf; + } + a &= nums[i]; + if (a < andValues[j]) { + return inf; + } + long long key = (long long) i << 36 | (long long) j << 32 | a; + if (f.contains(key)) { + return f[key]; + } + int ans = dfs(i + 1, j, a); + if (a == andValues[j]) { + ans = min(ans, dfs(i + 1, j + 1, -1) + nums[i]); + } + return f[key] = ans; + } +}; ``` ```go +func minimumValueSum(nums []int, andValues []int) int { + n, m := len(nums), len(andValues) + f := map[int]int{} + const inf int = 1 << 29 + var dfs func(i, j, a int) int + dfs = func(i, j, a int) int { + if n-i < m-j { + return inf + } + if j == m { + if i == n { + return 0 + } + return inf + } + a &= nums[i] + if a < andValues[j] { + return inf + } + key := i<<36 | j<<32 | a + if v, ok := f[key]; ok { + return v + } + ans := dfs(i+1, j, a) + if a == andValues[j] { + ans = min(ans, dfs(i+1, j+1, -1)+nums[i]) + } + f[key] = ans + return ans + } + if ans := dfs(0, 0, -1); ans < inf { + return ans + } + return -1 +} +``` +```ts +function minimumValueSum(nums: number[], andValues: number[]): number { + const [n, m] = [nums.length, andValues.length]; + const f: Map = new Map(); + const dfs = (i: number, j: number, a: number): number => { + if (n - i < m - j) { + return Infinity; + } + if (j === m) { + return i === n ? 0 : Infinity; + } + a &= nums[i]; + if (a < andValues[j]) { + return Infinity; + } + const key = (BigInt(i) << 36n) | (BigInt(j) << 32n) | BigInt(a); + if (f.has(key)) { + return f.get(key)!; + } + let ans = dfs(i + 1, j, a); + if (a === andValues[j]) { + ans = Math.min(ans, dfs(i + 1, j + 1, -1) + nums[i]); + } + f.set(key, ans); + return ans; + }; + const ans = dfs(0, 0, -1); + return ans >= Infinity ? -1 : ans; +} ``` diff --git a/solution/3100-3199/3117.Minimum Sum of Values by Dividing Array/README_EN.md b/solution/3100-3199/3117.Minimum Sum of Values by Dividing Array/README_EN.md index 3d16cc07730fd..4ee590cb99a09 100644 --- a/solution/3100-3199/3117.Minimum Sum of Values by Dividing Array/README_EN.md +++ b/solution/3100-3199/3117.Minimum Sum of Values by Dividing Array/README_EN.md @@ -80,24 +80,198 @@ ## Solutions -### Solution 1 +### Solution 1: Memoization Search + +We design a function $dfs(i, j, a)$, which represents the possible minimum sum of subarray values that can be obtained starting from the $i$-th element, with $j$ subarrays already divided, and the bitwise AND result of the current subarray to be divided is $a$. The answer is $dfs(0, 0, -1)$. + +The execution process of the function $dfs(i, j, a)$ is as follows: + +- If $n - i < m - j$, it means that the remaining elements are not enough to divide into $m - j$ subarrays, return $+\infty$. +- If $j = m$, it means that $m$ subarrays have been divided. At this time, check whether $i = n$ holds. If it holds, return $0$, otherwise return $+\infty$. +- Otherwise, we perform a bitwise AND operation on $a$ and $nums[i]$ to get a new $a$. If $a < andValues[j]$, it means that the bitwise AND result of the current subarray to be divided does not meet the requirements, return $+\infty$. Otherwise, we have two choices: + - Do not divide the current element, i.e., $dfs(i + 1, j, a)$. + - Divide the current element, i.e., $dfs(i + 1, j + 1, -1) + nums[i]$. +- Return the minimum of the above two choices. + +To avoid repeated calculations, we use the method of memoization search and store the result of $dfs(i, j, a)$ in a hash table. + +The time complexity is $O(n \times m \times \log M)$, and the space complexity is $O(n \times m \times \log M)$. Where $n$ and $m$ are the lengths of the arrays $nums$ and $andValues$ respectively; and $M$ is the maximum value in the array $nums$, in this problem $M \leq 10^5$. ```python - +class Solution: + def minimumValueSum(self, nums: List[int], andValues: List[int]) -> int: + @cache + def dfs(i: int, j: int, a: int) -> int: + if n - i < m - j: + return inf + if j == m: + return 0 if i == n else inf + a &= nums[i] + if a < andValues[j]: + return inf + ans = dfs(i + 1, j, a) + if a == andValues[j]: + ans = min(ans, dfs(i + 1, j + 1, -1) + nums[i]) + return ans + + n, m = len(nums), len(andValues) + ans = dfs(0, 0, -1) + return ans if ans < inf else -1 ``` ```java - +class Solution { + private int[] nums; + private int[] andValues; + private final int inf = 1 << 29; + private Map f = new HashMap<>(); + + public int minimumValueSum(int[] nums, int[] andValues) { + this.nums = nums; + this.andValues = andValues; + int ans = dfs(0, 0, -1); + return ans >= inf ? -1 : ans; + } + + private int dfs(int i, int j, int a) { + if (nums.length - i < andValues.length - j) { + return inf; + } + if (j == andValues.length) { + return i == nums.length ? 0 : inf; + } + a &= nums[i]; + if (a < andValues[j]) { + return inf; + } + long key = (long) i << 36 | (long) j << 32 | a; + if (f.containsKey(key)) { + return f.get(key); + } + + int ans = dfs(i + 1, j, a); + if (a == andValues[j]) { + ans = Math.min(ans, dfs(i + 1, j + 1, -1) + nums[i]); + } + f.put(key, ans); + return ans; + } +} ``` ```cpp - +class Solution { +public: + int minimumValueSum(vector& nums, vector& andValues) { + this->nums = nums; + this->andValues = andValues; + n = nums.size(); + m = andValues.size(); + int ans = dfs(0, 0, -1); + return ans >= inf ? -1 : ans; + } + +private: + vector nums; + vector andValues; + int n; + int m; + const int inf = 1 << 29; + unordered_map f; + + int dfs(int i, int j, int a) { + if (n - i < m - j) { + return inf; + } + if (j == m) { + return i == n ? 0 : inf; + } + a &= nums[i]; + if (a < andValues[j]) { + return inf; + } + long long key = (long long) i << 36 | (long long) j << 32 | a; + if (f.contains(key)) { + return f[key]; + } + int ans = dfs(i + 1, j, a); + if (a == andValues[j]) { + ans = min(ans, dfs(i + 1, j + 1, -1) + nums[i]); + } + return f[key] = ans; + } +}; ``` ```go +func minimumValueSum(nums []int, andValues []int) int { + n, m := len(nums), len(andValues) + f := map[int]int{} + const inf int = 1 << 29 + var dfs func(i, j, a int) int + dfs = func(i, j, a int) int { + if n-i < m-j { + return inf + } + if j == m { + if i == n { + return 0 + } + return inf + } + a &= nums[i] + if a < andValues[j] { + return inf + } + key := i<<36 | j<<32 | a + if v, ok := f[key]; ok { + return v + } + ans := dfs(i+1, j, a) + if a == andValues[j] { + ans = min(ans, dfs(i+1, j+1, -1)+nums[i]) + } + f[key] = ans + return ans + } + if ans := dfs(0, 0, -1); ans < inf { + return ans + } + return -1 +} +``` +```ts +function minimumValueSum(nums: number[], andValues: number[]): number { + const [n, m] = [nums.length, andValues.length]; + const f: Map = new Map(); + const dfs = (i: number, j: number, a: number): number => { + if (n - i < m - j) { + return Infinity; + } + if (j === m) { + return i === n ? 0 : Infinity; + } + a &= nums[i]; + if (a < andValues[j]) { + return Infinity; + } + const key = (BigInt(i) << 36n) | (BigInt(j) << 32n) | BigInt(a); + if (f.has(key)) { + return f.get(key)!; + } + let ans = dfs(i + 1, j, a); + if (a === andValues[j]) { + ans = Math.min(ans, dfs(i + 1, j + 1, -1) + nums[i]); + } + f.set(key, ans); + return ans; + }; + const ans = dfs(0, 0, -1); + return ans >= Infinity ? -1 : ans; +} ``` diff --git a/solution/3100-3199/3117.Minimum Sum of Values by Dividing Array/Solution.cpp b/solution/3100-3199/3117.Minimum Sum of Values by Dividing Array/Solution.cpp new file mode 100644 index 0000000000000..f10ffff887ad9 --- /dev/null +++ b/solution/3100-3199/3117.Minimum Sum of Values by Dividing Array/Solution.cpp @@ -0,0 +1,41 @@ +class Solution { +public: + int minimumValueSum(vector& nums, vector& andValues) { + this->nums = nums; + this->andValues = andValues; + n = nums.size(); + m = andValues.size(); + int ans = dfs(0, 0, -1); + return ans >= inf ? -1 : ans; + } + +private: + vector nums; + vector andValues; + int n; + int m; + const int inf = 1 << 29; + unordered_map f; + + int dfs(int i, int j, int a) { + if (n - i < m - j) { + return inf; + } + if (j == m) { + return i == n ? 0 : inf; + } + a &= nums[i]; + if (a < andValues[j]) { + return inf; + } + long long key = (long long) i << 36 | (long long) j << 32 | a; + if (f.contains(key)) { + return f[key]; + } + int ans = dfs(i + 1, j, a); + if (a == andValues[j]) { + ans = min(ans, dfs(i + 1, j + 1, -1) + nums[i]); + } + return f[key] = ans; + } +}; \ No newline at end of file diff --git a/solution/3100-3199/3117.Minimum Sum of Values by Dividing Array/Solution.go b/solution/3100-3199/3117.Minimum Sum of Values by Dividing Array/Solution.go new file mode 100644 index 0000000000000..c718e9bbc7550 --- /dev/null +++ b/solution/3100-3199/3117.Minimum Sum of Values by Dividing Array/Solution.go @@ -0,0 +1,35 @@ +func minimumValueSum(nums []int, andValues []int) int { + n, m := len(nums), len(andValues) + f := map[int]int{} + const inf int = 1 << 29 + var dfs func(i, j, a int) int + dfs = func(i, j, a int) int { + if n-i < m-j { + return inf + } + if j == m { + if i == n { + return 0 + } + return inf + } + a &= nums[i] + if a < andValues[j] { + return inf + } + key := i<<36 | j<<32 | a + if v, ok := f[key]; ok { + return v + } + ans := dfs(i+1, j, a) + if a == andValues[j] { + ans = min(ans, dfs(i+1, j+1, -1)+nums[i]) + } + f[key] = ans + return ans + } + if ans := dfs(0, 0, -1); ans < inf { + return ans + } + return -1 +} \ No newline at end of file diff --git a/solution/3100-3199/3117.Minimum Sum of Values by Dividing Array/Solution.java b/solution/3100-3199/3117.Minimum Sum of Values by Dividing Array/Solution.java new file mode 100644 index 0000000000000..1be77af40b594 --- /dev/null +++ b/solution/3100-3199/3117.Minimum Sum of Values by Dividing Array/Solution.java @@ -0,0 +1,37 @@ +class Solution { + private int[] nums; + private int[] andValues; + private final int inf = 1 << 29; + private Map f = new HashMap<>(); + + public int minimumValueSum(int[] nums, int[] andValues) { + this.nums = nums; + this.andValues = andValues; + int ans = dfs(0, 0, -1); + return ans >= inf ? -1 : ans; + } + + private int dfs(int i, int j, int a) { + if (nums.length - i < andValues.length - j) { + return inf; + } + if (j == andValues.length) { + return i == nums.length ? 0 : inf; + } + a &= nums[i]; + if (a < andValues[j]) { + return inf; + } + long key = (long) i << 36 | (long) j << 32 | a; + if (f.containsKey(key)) { + return f.get(key); + } + + int ans = dfs(i + 1, j, a); + if (a == andValues[j]) { + ans = Math.min(ans, dfs(i + 1, j + 1, -1) + nums[i]); + } + f.put(key, ans); + return ans; + } +} \ No newline at end of file diff --git a/solution/3100-3199/3117.Minimum Sum of Values by Dividing Array/Solution.py b/solution/3100-3199/3117.Minimum Sum of Values by Dividing Array/Solution.py new file mode 100644 index 0000000000000..4593f84c1e351 --- /dev/null +++ b/solution/3100-3199/3117.Minimum Sum of Values by Dividing Array/Solution.py @@ -0,0 +1,19 @@ +class Solution: + def minimumValueSum(self, nums: List[int], andValues: List[int]) -> int: + @cache + def dfs(i: int, j: int, a: int) -> int: + if n - i < m - j: + return inf + if j == m: + return 0 if i == n else inf + a &= nums[i] + if a < andValues[j]: + return inf + ans = dfs(i + 1, j, a) + if a == andValues[j]: + ans = min(ans, dfs(i + 1, j + 1, -1) + nums[i]) + return ans + + n, m = len(nums), len(andValues) + ans = dfs(0, 0, -1) + return ans if ans < inf else -1 diff --git a/solution/3100-3199/3117.Minimum Sum of Values by Dividing Array/Solution.ts b/solution/3100-3199/3117.Minimum Sum of Values by Dividing Array/Solution.ts new file mode 100644 index 0000000000000..923d0e40e232a --- /dev/null +++ b/solution/3100-3199/3117.Minimum Sum of Values by Dividing Array/Solution.ts @@ -0,0 +1,28 @@ +function minimumValueSum(nums: number[], andValues: number[]): number { + const [n, m] = [nums.length, andValues.length]; + const f: Map = new Map(); + const dfs = (i: number, j: number, a: number): number => { + if (n - i < m - j) { + return Infinity; + } + if (j === m) { + return i === n ? 0 : Infinity; + } + a &= nums[i]; + if (a < andValues[j]) { + return Infinity; + } + const key = (BigInt(i) << 36n) | (BigInt(j) << 32n) | BigInt(a); + if (f.has(key)) { + return f.get(key)!; + } + let ans = dfs(i + 1, j, a); + if (a === andValues[j]) { + ans = Math.min(ans, dfs(i + 1, j + 1, -1) + nums[i]); + } + f.set(key, ans); + return ans; + }; + const ans = dfs(0, 0, -1); + return ans >= Infinity ? -1 : ans; +}