-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy patharray-score.py
More file actions
27 lines (26 loc) · 800 Bytes
/
array-score.py
File metadata and controls
27 lines (26 loc) · 800 Bytes
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
class Solution:
"""
@param nums: the array to be scored.
@param k: the requirement of subarray length.
@param u: if the sum is less than u, get 1 score.
@param l: if the sum is greater than l, lose 1 score.
@return: return the sum of scores for every subarray whose length is k.
"""
def arrayScore(self, nums, k, u, l):
# write your code here.
ret = 0
s = 0
for i in range(0, k):
s += nums[i]
if s < u:
ret += 1
if s > l:
ret -= 1
for i in range(k, len(nums)):
s += nums[i] - nums[i - k]
if s < u:
ret += 1
if s > l:
ret -= 1
return ret
# easy: https://www.lintcode.com/problem/array-score/