-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm523 v1 Daily 2024-06-08.py
78 lines (63 loc) · 2.63 KB
/
m523 v1 Daily 2024-06-08.py
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
class Solution:
def checkSubarraySum(self, nums: List[int], k: int) -> bool:
if len(nums) <= 1 :
return False
pastRemainders = {}
pastRemainders[nums[0] % k] = 0
nums[0] %= k
for i in range(1, len(nums)) :
nums[i] = (nums[i - 1] + nums[i]) % k
if not nums[i] :
return True
if nums[i] not in pastRemainders :
pastRemainders[nums[i]] = i
continue
if pastRemainders[nums[i]] < i - 1 :
return True
return False
# def checkSubarraySum(self, nums: List[int], k: int) -> bool:
# if len(nums) <= 1 :
# return False
# # To calculate the remainder val of left, right, we take
# # nums[right] - nums[left - 1] (defaulting to 0 if out of bounds)
# helperVals = [0]
# for i in range(len(nums)) :
# helperVals.append((helperVals[i] + nums[i]) % k)
# if helperVals[-1] == 0 :
# return True
# self.seenSums = set()
# print(nums)
# print([x % k for x in nums])
# print(helperVals)
# print(len(helperVals))
# return self.helper(helperVals, k, 1, 2)
# def helper(self, nums, k, left, right) -> bool :
# if right <= left :
# return False
# if right >= len(nums) :
# return False
# if ((nums[right] - nums[left - 1]) % k) in self.seenSums :
# return True
# self.seenSums.add((nums[right] - nums[left - 1]) % k)
# # print(left, right)
# return ((nums[right] - nums[left - 1]) % k == 0) or \
# self.helper(nums, k, left + 1, right) or \
# self.helper(nums, k, left, right + 1)
# def helper(self, nums, k, left, right, currentSum) -> bool :
# # print(left, right, currentSum)
# if right - left < 1 :
# # print(f'FALSE\tleft: {left}, right: {right}, currentSum: {currentSum}')
# return False
# if right >= len(nums) :
# # print(f'FALSE\tleft: {left}, right: {right}, currentSum: {currentSum}')
# return False
# if currentSum % k == 0:
# # print(f'TRUE\tleft: {left}, right: {right}, currentSum: {currentSum}')
# return True
# # print('False. Moving on')
# if self.helper(nums, k, left + 1, right, currentSum - nums[left]) :
# return True
# if right + 1 >= len(nums) :
# return False
# else :
# return self.helper(nums, k, left, right + 1, currentSum + nums[right + 1])