-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
78 lines (70 loc) · 1.85 KB
/
main.go
File metadata and controls
78 lines (70 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Source: https://leetcode.com/problems/maximum-size-subarray-sum-equals-k
// Title: Maximum Size Subarray Sum Equals k
// Difficulty: Medium
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Given an integer array `nums` and an integer `k`, return the maximum length of a <button type="button" aria-haspopup="dialog" aria-expanded="false" aria-controls="radix-:rs:" data-state="closed" class="">subarray</button> that sums to `k`. If there is not one, return `0` instead.
//
// **Example 1:**
//
// ```
// Input: nums = [1,-1,5,-2,3], k = 3
// Output: 4
// Explanation: The subarray [1, -1, 5, -2] sums to 3 and is the longest.
// ```
//
// **Example 2:**
//
// ```
// Input: nums = [-2,-1,2,1], k = 1
// Output: 2
// Explanation: The subarray [-1, 2] sums to 1 and is the longest.
// ```
//
// **Constraints:**
//
// - `1 <= nums.length <= 2 * 10^5`
// - `-10^4 <= nums[i] <= 10^4`
// - `-10^9<= k <= 10^9`
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package main
// Prefix sum (TLE)
func maxSubArrayLen(nums []int, k int) int {
n := len(nums)
pre := make([]int, n+1)
for i, num := range nums {
pre[i+1] = pre[i] + num
}
for l := n; l >= 1; l-- { // loop through length
for i := 0; i <= n-l; i++ {
if pre[i+l]-pre[i] == k {
return l
}
}
}
return 0
}
// Prefix sum + Hash table
func maxSubArrayLen2(nums []int, k int) int {
n := len(nums)
preMap := make(map[int]int, n) // map prefix sum to right most index
{
sum := 0
for i, num := range nums {
sum += num
preMap[sum] = i + 1
}
}
ans := 0
{
sum := 0
for i, num := range nums {
if j, ok := preMap[sum+k]; ok {
ans = max(ans, j-i)
}
sum += num
}
}
return ans
}