LeetCode solutions

4074. Count Subarrays With Majority Element I

My accepted Python solution to LeetCode problem 4074, Count Subarrays With Majority Element I, running in 1704ms.

  • Difficulty: Medium
  • Python
  • Runtime 1704ms
  • Memory 17.7MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 1704ms, memory 17.7MB, accepted 2026-01-01.

python
class Solution:
    def countMajoritySubarrays(self, nums: List[int], target: int) -> int:
        n = len(nums)
        count = 0
        for i in range(n):
            target_count = 0
            for j in range(i, n):
                if nums[j] == target:
                    target_count += 1
                length = j - i + 1
                if target_count > length // 2:
                    count += 1
        return count

Source