Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- The score of an array is defined as the product of its sum and its length.
- For example, the score of [1, 2, 3, 4, 5] is (1 + 2 + 3 + 4 + 5) * 5 = 75.
- Given a positive integer array nums and an integer k, return the number of non-empty subarrays of nums whose score is strictly less than k.
- A subarray is a contiguous sequence of elements within an array.
- """
- class Solution:
- def countSubarrays(self, nums: List[int], k: int) -> int:
- answer = 0
- left_i = 0; total = 0; right_i = 0
- while right_i < len(nums):
- total += nums[right_i]
- while True:
- if left_i > right_i:
- break
- if total * (right_i - left_i + 1) < k:
- break
- total -= nums[left_i]
- left_i += 1
- if left_i > right_i:
- right_i = left_i
- else:
- answer = answer + (right_i - left_i + 1)
- right_i += 1
- return answer
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement