diff --git a/solution/1500-1599/1546.Maximum Number of Non-Overlapping Subarrays With Sum Equals Target/README.md b/solution/1500-1599/1546.Maximum Number of Non-Overlapping Subarrays With Sum Equals Target/README.md index ff4714888c0be..1793d4125d072 100644 --- a/solution/1500-1599/1546.Maximum Number of Non-Overlapping Subarrays With Sum Equals Target/README.md +++ b/solution/1500-1599/1546.Maximum Number of Non-Overlapping Subarrays With Sum Equals Target/README.md @@ -52,9 +52,11 @@ -贪心 + 前缀和。ans 表示结果,初始值为 0。 +**方法一:贪心 + 前缀和 + 哈希表** -贪心:当我们发现以下标 i 结尾的子数组和为 target 时,ans++,然后继续往后查找。 +我们遍历数组 $nums$,利用前缀和 + 哈希表的方法,寻找和为 $target$ 的子数组,若找到,则答案加一,然后我们将前缀和置为 $0$,继续遍历数组 $nums$,直到遍历完整个数组。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $nums$ 的长度。 @@ -65,18 +67,18 @@ ```python class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: - i, n = 0, len(nums) ans = 0 + i, n = 0, len(nums) while i < n: s = 0 - seen = {0} + vis = {0} while i < n: s += nums[i] - if s - target in seen: + if s - target in vis: ans += 1 break i += 1 - seen.add(s) + vis.add(s) i += 1 return ans ``` @@ -88,22 +90,20 @@ class Solution: ```java class Solution { public int maxNonOverlapping(int[] nums, int target) { - int i = 0, n = nums.length; - int ans = 0; - while (i < n) { + int ans = 0, n = nums.length; + for (int i = 0; i < n; ++i) { + Set vis = new HashSet<>(); int s = 0; - Set seen = new HashSet<>(); - seen.add(0); + vis.add(0); while (i < n) { s += nums[i]; - if (seen.contains(s - target)) { + if (vis.contains(s - target)) { ++ans; break; } ++i; - seen.add(s); + vis.add(s); } - ++i; } return ans; } @@ -116,22 +116,19 @@ class Solution { class Solution { public: int maxNonOverlapping(vector& nums, int target) { - int i = 0, n = nums.size(); - int ans = 0; - while (i < n) { + int ans = 0, n = nums.size(); + for (int i = 0; i < n; ++i) { + unordered_set vis{{0}}; int s = 0; - unordered_set seen; - seen.insert(0); while (i < n) { s += nums[i]; - if (seen.count(s - target)) { + if (vis.count(s - target)) { ++ans; break; } ++i; - seen.insert(s); + vis.insert(s); } - ++i; } return ans; } @@ -141,23 +138,44 @@ public: ### **Go** ```go -func maxNonOverlapping(nums []int, target int) int { - i, n, ans := 0, len(nums), 0 - for i < n { +func maxNonOverlapping(nums []int, target int) (ans int) { + n := len(nums) + for i := 0; i < n; i++ { s := 0 - seen := map[int]bool{0: true} - for i < n { + vis := map[int]bool{0: true} + for ; i < n; i++ { s += nums[i] - if seen[s-target] { + if vis[s-target] { ans++ break } - seen[s] = true - i++ + vis[s] = true } - i++ } - return ans + return +} +``` + +### **TypeScript** + +```ts +function maxNonOverlapping(nums: number[], target: number): number { + const n = nums.length; + let ans = 0; + for (let i = 0; i < n; ++i) { + let s = 0; + const vis: Set = new Set(); + vis.add(0); + for (; i < n; ++i) { + s += nums[i]; + if (vis.has(s - target)) { + ++ans; + break; + } + vis.add(s); + } + } + return ans; } ``` diff --git a/solution/1500-1599/1546.Maximum Number of Non-Overlapping Subarrays With Sum Equals Target/README_EN.md b/solution/1500-1599/1546.Maximum Number of Non-Overlapping Subarrays With Sum Equals Target/README_EN.md index bc8798d6d239a..d437b392235a2 100644 --- a/solution/1500-1599/1546.Maximum Number of Non-Overlapping Subarrays With Sum Equals Target/README_EN.md +++ b/solution/1500-1599/1546.Maximum Number of Non-Overlapping Subarrays With Sum Equals Target/README_EN.md @@ -35,6 +35,12 @@ ## Solutions +**Solution 1: Greedy + Prefix Sum + Hash Table** + +We traverse the array $nums$, using the method of prefix sum + hash table, to find subarrays with a sum of $target$. If found, we increment the answer by one, then we set the prefix sum to $0$ and continue to traverse the array $nums$ until the entire array is traversed. + +The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $nums$. + ### **Python3** @@ -42,18 +48,18 @@ ```python class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: - i, n = 0, len(nums) ans = 0 + i, n = 0, len(nums) while i < n: s = 0 - seen = {0} + vis = {0} while i < n: s += nums[i] - if s - target in seen: + if s - target in vis: ans += 1 break i += 1 - seen.add(s) + vis.add(s) i += 1 return ans ``` @@ -63,22 +69,20 @@ class Solution: ```java class Solution { public int maxNonOverlapping(int[] nums, int target) { - int i = 0, n = nums.length; - int ans = 0; - while (i < n) { + int ans = 0, n = nums.length; + for (int i = 0; i < n; ++i) { + Set vis = new HashSet<>(); int s = 0; - Set seen = new HashSet<>(); - seen.add(0); + vis.add(0); while (i < n) { s += nums[i]; - if (seen.contains(s - target)) { + if (vis.contains(s - target)) { ++ans; break; } ++i; - seen.add(s); + vis.add(s); } - ++i; } return ans; } @@ -91,22 +95,19 @@ class Solution { class Solution { public: int maxNonOverlapping(vector& nums, int target) { - int i = 0, n = nums.size(); - int ans = 0; - while (i < n) { + int ans = 0, n = nums.size(); + for (int i = 0; i < n; ++i) { + unordered_set vis{{0}}; int s = 0; - unordered_set seen; - seen.insert(0); while (i < n) { s += nums[i]; - if (seen.count(s - target)) { + if (vis.count(s - target)) { ++ans; break; } ++i; - seen.insert(s); + vis.insert(s); } - ++i; } return ans; } @@ -116,23 +117,44 @@ public: ### **Go** ```go -func maxNonOverlapping(nums []int, target int) int { - i, n, ans := 0, len(nums), 0 - for i < n { +func maxNonOverlapping(nums []int, target int) (ans int) { + n := len(nums) + for i := 0; i < n; i++ { s := 0 - seen := map[int]bool{0: true} - for i < n { + vis := map[int]bool{0: true} + for ; i < n; i++ { s += nums[i] - if seen[s-target] { + if vis[s-target] { ans++ break } - seen[s] = true - i++ + vis[s] = true } - i++ } - return ans + return +} +``` + +### **TypeScript** + +```ts +function maxNonOverlapping(nums: number[], target: number): number { + const n = nums.length; + let ans = 0; + for (let i = 0; i < n; ++i) { + let s = 0; + const vis: Set = new Set(); + vis.add(0); + for (; i < n; ++i) { + s += nums[i]; + if (vis.has(s - target)) { + ++ans; + break; + } + vis.add(s); + } + } + return ans; } ``` diff --git a/solution/1500-1599/1546.Maximum Number of Non-Overlapping Subarrays With Sum Equals Target/Solution.cpp b/solution/1500-1599/1546.Maximum Number of Non-Overlapping Subarrays With Sum Equals Target/Solution.cpp index 80e4c1a9cc9ab..8456f320be078 100644 --- a/solution/1500-1599/1546.Maximum Number of Non-Overlapping Subarrays With Sum Equals Target/Solution.cpp +++ b/solution/1500-1599/1546.Maximum Number of Non-Overlapping Subarrays With Sum Equals Target/Solution.cpp @@ -1,23 +1,20 @@ -class Solution { -public: - int maxNonOverlapping(vector& nums, int target) { - int i = 0, n = nums.size(); - int ans = 0; - while (i < n) { - int s = 0; - unordered_set seen; - seen.insert(0); - while (i < n) { - s += nums[i]; - if (seen.count(s - target)) { - ++ans; - break; - } - ++i; - seen.insert(s); - } - ++i; - } - return ans; - } +class Solution { +public: + int maxNonOverlapping(vector& nums, int target) { + int ans = 0, n = nums.size(); + for (int i = 0; i < n; ++i) { + unordered_set vis{{0}}; + int s = 0; + while (i < n) { + s += nums[i]; + if (vis.count(s - target)) { + ++ans; + break; + } + ++i; + vis.insert(s); + } + } + return ans; + } }; \ No newline at end of file diff --git a/solution/1500-1599/1546.Maximum Number of Non-Overlapping Subarrays With Sum Equals Target/Solution.go b/solution/1500-1599/1546.Maximum Number of Non-Overlapping Subarrays With Sum Equals Target/Solution.go index 97d29203d378a..514a44862379c 100644 --- a/solution/1500-1599/1546.Maximum Number of Non-Overlapping Subarrays With Sum Equals Target/Solution.go +++ b/solution/1500-1599/1546.Maximum Number of Non-Overlapping Subarrays With Sum Equals Target/Solution.go @@ -1,18 +1,16 @@ -func maxNonOverlapping(nums []int, target int) int { - i, n, ans := 0, len(nums), 0 - for i < n { +func maxNonOverlapping(nums []int, target int) (ans int) { + n := len(nums) + for i := 0; i < n; i++ { s := 0 - seen := map[int]bool{0: true} - for i < n { + vis := map[int]bool{0: true} + for ; i < n; i++ { s += nums[i] - if seen[s-target] { + if vis[s-target] { ans++ break } - seen[s] = true - i++ + vis[s] = true } - i++ } - return ans + return } \ No newline at end of file diff --git a/solution/1500-1599/1546.Maximum Number of Non-Overlapping Subarrays With Sum Equals Target/Solution.java b/solution/1500-1599/1546.Maximum Number of Non-Overlapping Subarrays With Sum Equals Target/Solution.java index 43cdf67bed7ff..ba02bbaa3c53d 100644 --- a/solution/1500-1599/1546.Maximum Number of Non-Overlapping Subarrays With Sum Equals Target/Solution.java +++ b/solution/1500-1599/1546.Maximum Number of Non-Overlapping Subarrays With Sum Equals Target/Solution.java @@ -1,22 +1,20 @@ -class Solution { - public int maxNonOverlapping(int[] nums, int target) { - int i = 0, n = nums.length; - int ans = 0; - while (i < n) { - int s = 0; - Set seen = new HashSet<>(); - seen.add(0); - while (i < n) { - s += nums[i]; - if (seen.contains(s - target)) { - ++ans; - break; - } - ++i; - seen.add(s); - } - ++i; - } - return ans; - } +class Solution { + public int maxNonOverlapping(int[] nums, int target) { + int ans = 0, n = nums.length; + for (int i = 0; i < n; ++i) { + Set vis = new HashSet<>(); + int s = 0; + vis.add(0); + while (i < n) { + s += nums[i]; + if (vis.contains(s - target)) { + ++ans; + break; + } + ++i; + vis.add(s); + } + } + return ans; + } } \ No newline at end of file diff --git a/solution/1500-1599/1546.Maximum Number of Non-Overlapping Subarrays With Sum Equals Target/Solution.py b/solution/1500-1599/1546.Maximum Number of Non-Overlapping Subarrays With Sum Equals Target/Solution.py index 5d4276a245cb5..8370cab581940 100644 --- a/solution/1500-1599/1546.Maximum Number of Non-Overlapping Subarrays With Sum Equals Target/Solution.py +++ b/solution/1500-1599/1546.Maximum Number of Non-Overlapping Subarrays With Sum Equals Target/Solution.py @@ -1,16 +1,16 @@ -class Solution: - def maxNonOverlapping(self, nums: List[int], target: int) -> int: - i, n = 0, len(nums) - ans = 0 - while i < n: - s = 0 - seen = {0} - while i < n: - s += nums[i] - if s - target in seen: - ans += 1 - break - i += 1 - seen.add(s) - i += 1 - return ans +class Solution: + def maxNonOverlapping(self, nums: List[int], target: int) -> int: + ans = 0 + i, n = 0, len(nums) + while i < n: + s = 0 + vis = {0} + while i < n: + s += nums[i] + if s - target in vis: + ans += 1 + break + i += 1 + vis.add(s) + i += 1 + return ans diff --git a/solution/1500-1599/1546.Maximum Number of Non-Overlapping Subarrays With Sum Equals Target/Solution.ts b/solution/1500-1599/1546.Maximum Number of Non-Overlapping Subarrays With Sum Equals Target/Solution.ts new file mode 100644 index 0000000000000..fd6724b39a2b5 --- /dev/null +++ b/solution/1500-1599/1546.Maximum Number of Non-Overlapping Subarrays With Sum Equals Target/Solution.ts @@ -0,0 +1,18 @@ +function maxNonOverlapping(nums: number[], target: number): number { + const n = nums.length; + let ans = 0; + for (let i = 0; i < n; ++i) { + let s = 0; + const vis: Set = new Set(); + vis.add(0); + for (; i < n; ++i) { + s += nums[i]; + if (vis.has(s - target)) { + ++ans; + break; + } + vis.add(s); + } + } + return ans; +} diff --git a/solution/1500-1599/1547.Minimum Cost to Cut a Stick/README_EN.md b/solution/1500-1599/1547.Minimum Cost to Cut a Stick/README_EN.md index 2078b6763c37b..004d9a56bb0a7 100644 --- a/solution/1500-1599/1547.Minimum Cost to Cut a Stick/README_EN.md +++ b/solution/1500-1599/1547.Minimum Cost to Cut a Stick/README_EN.md @@ -46,6 +46,20 @@ There are much ordering with total cost <= 25, for example, the order [4, 6, ## Solutions +**Solution 1: Dynamic Programming (Interval DP)** + +We can add two elements to the cut array $cuts$, which are $0$ and $n$, representing the two ends of the stick. Then we sort the $cuts$ array, so that we can cut the entire stick into several intervals, each interval has two cut points. Suppose the length of the $cuts$ array at this time is $m$. + +Next, we define $f[i][j]$ to represent the minimum cost of cutting the interval $[cuts[i],..cuts[j]]$. + +If an interval only has two cut points, that is, we do not need to cut this interval, then $f[i][j] = 0$. + +Otherwise, we enumerate the length of the interval $l$, where $l$ is the number of cut points minus $1$. Then we enumerate the left endpoint $i$ of the interval, and the right endpoint $j$ can be obtained by $i + l$. For each interval, we enumerate its cut point $k$, where $i \lt k \lt j$, then we can cut the interval $[i, j]$ into $[i, k]$ and $[k, j]$, the cost at this time is $f[i][k] + f[k][j] + cuts[j] - cuts[i]$, we take the minimum value of all possible $k$, which is the value of $f[i][j]$. + +Finally, we return $f[0][m - 1]$. + +The time complexity is $O(m^3)$, and the space complexity is $O(m^2)$. Here, $m$ is the length of the modified $cuts$ array. + ### **Python3** diff --git a/solution/1500-1599/1551.Minimum Operations to Make Array Equal/README_EN.md b/solution/1500-1599/1551.Minimum Operations to Make Array Equal/README_EN.md index a9d4ade015425..748f5e7d54b7c 100644 --- a/solution/1500-1599/1551.Minimum Operations to Make Array Equal/README_EN.md +++ b/solution/1500-1599/1551.Minimum Operations to Make Array Equal/README_EN.md @@ -37,6 +37,26 @@ In the second operation choose x = 2 and y = 0 again, thus arr = [3, 3, 3]. ## Solutions +**Solution 1: Mathematics** + +According to the problem description, the array $arr$ is an arithmetic sequence with the first term as $1$ and the common difference as $2$. Therefore, the sum of the first $n$ terms of the array is: + +$$ +\begin{aligned} +S_n &= \frac{n}{2} \times (a_1 + a_n) \\ +&= \frac{n}{2} \times (1 + (2n - 1)) \\ +&= n^2 +\end{aligned} +$$ + +Since in one operation, one number is decreased by one and another number is increased by one, the sum of all elements in the array remains unchanged. Therefore, when all elements in the array are equal, the value of each element is $S_n / n = n$. Hence, the minimum number of operations required to make all elements in the array equal is: + +$$ +\sum_{i=0}^{\frac{n}{2}} (n - (2i + 1)) +$$ + +The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$. + ### **Python3**