Skip to content

Commit

Permalink
python dictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
SamirPaulb committed Mar 4, 2022
1 parent 93dece4 commit 212b8fd
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
18 changes: 18 additions & 0 deletions 19_HashMap/01. Continuous Subarray Sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# https://leetcode.com/problems/continuous-subarray-sum/

class Solution:
def checkSubarraySum(self, nums: List[int], k: int) -> bool:
sumIndexDic = {0 : -1} # nums = [14, 7]; k = 7
curSum = 0

for i in range(len(nums)):
curSum += nums[i]
curSum = curSum % k

if curSum in sumIndexDic:
if i - sumIndexDic[curSum] >= 2:
return True
else:
sumIndexDic[curSum] = i

return False
25 changes: 25 additions & 0 deletions 19_HashMap/02. Subarray Sums Divisible by K.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# https://leetcode.com/problems/subarray-sums-divisible-by-k/
# https://www.youtube.com/watch?v=QM0klnvTQzk

class Solution:
def subarraysDivByK(self, nums: List[int], k: int) -> int:
# Use COncept of 523. Continuous Subarray Sum

ans = 0
remainderCountDic = {0 : 1}
curSum = 0

for i in range(len(nums)):
curSum += nums[i]

if curSum < 0: curSum += k

curSum = curSum

if curSum in remainderCountDic:
ans += remainderCountDic[curSum]
remainderCountDic[curSum] += 1
else:
remainderCountDic[curSum] = 1

return ans

0 comments on commit 212b8fd

Please sign in to comment.