Skip to content

Commit a6cbe9d

Browse files
committed
Did yesterday's daily
1 parent d8d219d commit a6cbe9d

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

my-submissions/m1524 v0.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def numOfSubarrays(self, arr: List[int]) -> int:
3+
for i in range(1, len(arr)) :
4+
arr[i] += arr[i - 1]
5+
6+
arr[0] = (arr[0] % 2, (arr[0] + 1) % 2) # (# odds, # evens)
7+
for i in range(1, len(arr)) :
8+
arr[i] = (arr[i - 1][0] + arr[i] % 2, arr[i - 1][1] + (arr[i] + 1) % 2)
9+
10+
return arr[-1][0] + arr[-1][1] * arr[-1][0] % (10 ** 9 + 7)

my-submissions/m1524 v1.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def numOfSubarrays(self, arr: List[int]) -> int:
3+
for i in range(1, len(arr)) :
4+
arr[i] += arr[i - 1]
5+
6+
output = 0
7+
evens, odds = 1, 0
8+
for i in range(len(arr)) :
9+
if arr[i] % 2 : # odd
10+
output += evens
11+
odds += 1
12+
else :
13+
output += odds
14+
evens += 1
15+
return output % (10 ** 9 + 7)

my-submissions/m1524 v2.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def numOfSubarrays(self, arr: List[int]) -> int:
3+
output = 0
4+
pref_sum = 0
5+
evens, odds = 1, 0
6+
for num in arr :
7+
pref_sum += num
8+
if pref_sum % 2 : # odd
9+
output += evens
10+
odds += 1
11+
else :
12+
output += odds
13+
evens += 1
14+
return output % (10 ** 9 + 7)

my-submissions/m1524.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## V0
2+
3+
Initial attempt where I make each iteration by pass and reuse the existing array. Was inefficient I realized but the math worked. The process consisted of...
4+
5+
1. Turning arr into a prefix sum array
6+
2. Replacing each index of arr with a tuple containing the prefix count of the number of items prior that had an odd or even prefix sum
7+
3. Returning the number of odd prefix sums plus the number of odds times the number of evens (since those can be matched up to get a subarray).
8+
9+
## V1
10+
11+
Much improved version that insteads sums the cases during the odds and evens iteration rather than replacing the array values
12+
13+
## V2
14+
15+
Improved version off of V1 that removes the need for the first prefix sum pass as it does all calculations within a single pass.

0 commit comments

Comments
 (0)