aboutsummaryrefslogtreecommitdiff
path: root/count_subarr_len_three_cond.py
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-04-26 19:27:09 -0600
committerJack Sangdahl <[email protected]>2025-04-26 19:27:09 -0600
commit5a210dfaefaa53cfa6f3152c23d992c93e83c4cf (patch)
tree3e9a9a223f9097060fac14ba160b945367dbfbb1 /count_subarr_len_three_cond.py
parente005e72b441ae5d8f214adaace18a2312f8be2cf (diff)
downloadleetcode-5a210dfaefaa53cfa6f3152c23d992c93e83c4cf.tar.gz
leetcode-5a210dfaefaa53cfa6f3152c23d992c93e83c4cf.zip
3392. count subarrays of length three with a condition
Diffstat (limited to 'count_subarr_len_three_cond.py')
-rw-r--r--count_subarr_len_three_cond.py22
1 files changed, 22 insertions, 0 deletions
diff --git a/count_subarr_len_three_cond.py b/count_subarr_len_three_cond.py
new file mode 100644
index 0000000..cb542bd
--- /dev/null
+++ b/count_subarr_len_three_cond.py
@@ -0,0 +1,22 @@
+# 3392. Count Subarrays of Length Three With a Condition
+
+"""
+given an integer array 'nums', return the number of subarrays of length 3
+such that the sum of the first and third number equals exactly half of the
+second number.
+"""
+
+
+class Solution(object):
+ def countSubarrays(self, nums):
+ """
+ :type nums: List[int]
+ :rtype: int
+ """
+ return sum(l + r << 1 == m for l, m, r in zip(nums, nums[1:], nums[2:]))
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.countSubarrays(nums=[1, 2, 1, 4, 1]))
+ print(obj.countSubarrays(nums=[1, 1, 1]))