Maximum Size Subarray Sum Equals k
Problem Descriptionβ
Visit LeetCode for the full problem description.
Solutionsβ
Solution 1: C# (Best: 273 ms)β
| Metric | Value |
|---|---|
| Runtime | 273 ms |
| Memory | 54.1 MB |
| Date | 2022-01-22 |
Solution
public class Solution {
public int MaxSubArrayLen(int[] nums, int k) {
Dictionary<int, int> map = new Dictionary<int, int>();
int max = 0, sum = 0;
map.Add(0,-1);
for (int i = 0; i < nums.Length; i++)
{
sum += nums[i];
if(map.ContainsKey(sum-k))
{
max = Math.Max(max, i-map[sum-k]);
}
if(!map.ContainsKey(sum))
{
map.Add(sum, i);
}
}
return max;
}
}
Complexity Analysisβ
| Approach | Time | Space |
|---|---|---|
| Solution | To be analyzed | To be analyzed |