classSolution {public:intmaxSubArrayLen(vector<int>& nums,intk) {intsum =0, res =0; unordered_map<int,int>m;for(inti =0; i < nums.size(); ++i) { sum+=nums[i];if(sum == k) res = i +1;elseif(m.count(sum - k)) res = max(res, i - m[sum -k]);if(!m.count(sum...
classSolution {public:intmaxSubArrayLen(vector<int>& nums,intk) {if(nums.empty())return0;intres =0; unordered_map<int, vector<int>>m; m[nums[0]].push_back(0); vector<int> sum =nums;for(inti =1; i < nums.size(); ++i) { sum[i]+= sum[i -1]; m[sum[i]].push_back(i...
Maximum Size Subarray Sum Equals k -- LeetCode Given an arraynumsand a target valuek, find the maximum length of a subarray that sums tok. If there isn't one, return 0 instead. Example 1: Givennums=[1, -1, 5, -2, 3],k=3, return4. (because the subarray[1, -1, 5, -2]su...
3~5的size怎么计算呢?---> i - map.get(sum - k)在这个例子里就是5-2 代码如下: public int maxSubArrayLen(int[] nums, int k) { int sum = 0, max = 0; HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); for (int i = 0; i < nums.length; i++) { sum = ...
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 range. Example 1: Given nums = [1, -1, 5, -2,...
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. ...
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. this is a really easy undetstanding problem. and the follow up is do it in O(n) time. ...
209.Minimum-Size-Subarray-Sum (M) 088.Merge Sorted Array (M) 283.Move-Zeroes (M) 141.Linked-List-Cycle (E+) 142.Linked-List-Cycle-II (M+) 360.Sort-Transformed-Array (M) 713.Subarray-Product-Less-Than-K (M+) 923.3Sum-With-Multiplicity (H-) 1234.Replace-the-Substring-for-Balance...
Misc Maximum Sum Of Contiguous SubArray Of Fixed Size K 🟢Easy Sliding Window Algorithms / Sliding Window Misc Smallest subarray with given sum 🟢Easy Sliding Window Algorithms / Sliding Window Misc Bubble Sort 🟢Easy Sorting Algorithms / Sorting Misc Insertion Sort 🟢Easy Sorting Algorithms /...
in prefixSum array, find the maxisum size subarray sum equals k// sum of subarray (i, j) = prefixSum [j] - prefixSum[i]Map<Integer,Integer>prefixSumVsIndex=newHashMap<>();prefixSumVsIndex.put(0,-1);intmaxSize=0;intprefixSum=0;for(intj=0;j<nums.length;j++){prefixSum+=nums[...