维护前缀数组sums[],我们维护一个记录前缀和的map,map[x]表示前缀和是x距离当前i最近的下标。 那么状态转移方程就是dp[i] = max(dp[i-1], dp[map[sums[i]-target]]+1) class Solution { public:intdp[100005];intsum[100005];map<int,int> m;intmaxNonOverlapping(vector<int>& nums,inttarget) { ...
Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target. Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to targ...
Solution 2. Prefix Sum with HashMap, O(N) runtime and space 1. Keep a running prefix sum and a hash map that stores the count of each different prefix sum value; Add (0, 1) to the hashmap, representing a sum of 0 for empty interval. 2. for a given prefix sum ps, check if ...
2101. 引爆最多的炸弹 Detonate the Maximum Bombs 力扣 LeetCode 题解 09:13 1186. 删除一次得到子数组最大和 Maximum Subarray Sum with One Deletion 力扣 LeetCode 题解 08:57 2850. 将石头分散到网格图的最少移动次数 Minimum Moves to Spread Stones Over Grid 力扣 LeetCode 题解 12:15 3096. ...
[LeetCode] 523. Continuous Subarray Sum 【原题】 Given a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2 that sums up to the multiple of k, that is, sums up to n*k where n is also an ...
[LeetCode] Maximum Size Subarray Sum Equals k Problem Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If there isn't one, return 0 instead. Note The sum of the entire nums array is guaranteed to fit within the 32-bit signed integer ...
第一种是prefix sum + 双指针。 例如Minimum Size Subarray Sum - LeetCode: Given an array of n positive integers and a positive integers, find the minimal length of a contiguous subarray of which the sum ≥s. If there isn't one, return 0 instead. 用O(N)时间得到一个prefix sum array,可...
[LeetCode] 325. Maximum Size Subarray Sum Equals k Given an arraynumsand a target valuek, find the maximum length of a subarray that sums tok. If there isn't one, return 0 instead. Note: The sum of the entirenumsarray is guaranteed to fit within the 32-bit signed integer range....
A set of practice note, solution, complexity analysis and test bench to leetcode problem set - leetcode/CountSubArrayFixBound.drawio at b58bcceb0ea27d0756ad72fb6a64b3b547fae221 · brianchiang-tw/leetcode
这题跟560. subarray sum equals k几乎一毛一样。 我想到了用map和sum - k 来实现ONE PASS,但是不知道怎么维护一个类似560题里面那个count的值了。正确答案是用Math.max每次对比当前maxLen 和 i - map.get(sum-k)。 思维难度还是有的。 这题leetcode收费了。我贴一个别人的答案。注意这里的put始终是put...