-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcon_work.py
More file actions
55 lines (45 loc) · 1.31 KB
/
con_work.py
File metadata and controls
55 lines (45 loc) · 1.31 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
class Solution(object):
def consecutiveNumbersSum(self, N):
"""
:type N: int
:rtype: int
"""
# Math Magic Logic
m = 1
count = 0
while N >= (m*(m+1)//2):
if m%2 == 1:
if N%m == 0:
#print m
count += 1
elif N%m == m//2:
#print m
count += 1
m += 1
return count
"""
# Logic: that failed with timeout
# 1. With subArray list of possible sum(sub) == target
# 2. With left, right pointer to traverse the array + memoize the subArray sum as we traverse through
# 3. Brute force method
# Counting the target number as well
count = 1
# Using an array
sub = []
i = 1
sumi = 0
while i <= N:
#print i, sub, sumi
if sumi < N:
sumi += i
sub.append(i)
i += 1
elif sumi > N:
sumi -= sub[0]
sub.pop(0)
elif sumi == N:
count += 1
sumi -= sub[0]
sub.pop(0)
return count
"""