This commit is contained in:
parent
c84d092488
commit
6149910f25
1 changed files with 22 additions and 0 deletions
22
medium/number_of_sub_arrays_with_odd_sum.py
Normal file
22
medium/number_of_sub_arrays_with_odd_sum.py
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
# https://leetcode.com/problems/number-of-sub-arrays-with-odd-sum
|
||||||
|
|
||||||
|
from typing import List
|
||||||
|
|
||||||
|
MOD = 10 ** 9 + 7
|
||||||
|
class Solution:
|
||||||
|
def numOfSubarrays(self, arr: List[int]) -> int:
|
||||||
|
odd_count = int(arr[0] % 2 == 1)
|
||||||
|
for i in range(1, len(arr)):
|
||||||
|
arr[i] += arr[i -1]
|
||||||
|
odd_count += int(arr[i] % 2 == 1)
|
||||||
|
|
||||||
|
result = odd_count
|
||||||
|
curr = 0
|
||||||
|
for i in range(len(arr)):
|
||||||
|
if (arr[i] - curr) % 2 == 1:
|
||||||
|
odd_count -= 1
|
||||||
|
odd_count = len(arr) - i - 1 - odd_count
|
||||||
|
result += odd_count
|
||||||
|
curr = arr[i]
|
||||||
|
return result % MOD
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue