Skip to content

Commit 7285c80

Browse files
authoredJan 16, 2023
0560-subarray-sum-equals-k.py added and Updated README (#2039)
* 0560-subarray-sum-equals-k.py added and Updated README * 0560-subarray-sum-equals-k.py added and Updated README * Added Python Solution for 0560-subarray-sum-equlas-k
1 parent 0c746cd commit 7285c80

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
 

‎python/0560-subarray-sum-equals-k.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def subarraySum(self, nums: List[int], k: int) -> int:
3+
count = 0
4+
sum = 0
5+
dic = {}
6+
dic[0] = 1
7+
for i in range(len(nums)):
8+
sum += nums[i]
9+
if sum-k in dic:
10+
count += dic[sum-k]
11+
dic[sum] = dic.get(sum, 0)+1
12+
return count
13+
14+
# Time Complexity :
15+
# O(N) -> Where N is the size of the array and we are iterating over the array once
16+
17+
# Space Complexity:
18+
# O(N) -> Creating a hashmap/dictionary

0 commit comments

Comments
 (0)
Please sign in to comment.