NeetCode #267LC-1150EasyBinary SearchNC Algo100
← Back to All Problems#267 · #1150 · Check If a Number Is Majority Element in a Sorted Array(检查一个数是否在数组中占绝大多数)
📌 Problem Statement & Constraints
Given an integer array
nums sorted in non-decreasing order and an integer target, return true if target is the majority element (appears more than n / 2 times). Constraints: 1 <= nums.length <= 10^4, -10^9 <= nums[i], target <= 10^9.💡 Core Algorithmic Approaches
- Binary search for the lower bound of
target(bisect_left) and the upper bound (bisect_right). - The difference is the number of occurrences, which can be compared with
n / 2directly. - This is O(log n) rather than the O(n) linear count.
- The standard Boyer-Moore voting algorithm is not applicable here, because we need the count of a specific value.
💻 Benchmark Python3 Implementation
class Solution:
def isMajorityElement(self, nums: List[int], target: int) -> bool:
from bisect import bisect_left, bisect_right
lo = bisect_left(nums, target) # first index with value >= target
hi = bisect_right(nums, target) # first index with value > target
return (hi - lo) * 2 > len(nums) # strictly more than half⚡ Complexity Deep Dive
⏱️ Time Complexity
O(log n): two binary searches.
💾 Space Complexity
O(1): two indices.
⚠️ Interview Pitfalls & Follow-ups
- Using
>= len(nums) / 2: the majority requires strictly more than half, so* 2 >is the right test. - Scanning linearly to count occurrences: O(n), which defeats the point of the sorted input.
- Using
bisect_rightfor the lower bound: that would skip equal values. - Assuming
targetis present: if it is absent, both bounds are equal and the difference is 0, correctly returning false.
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
🧩 Related Binary Search Problems
中文速览与面试要点
检查一个数是否在数组中占绝大多数
给出一个按 非递减 顺序排列的数组 nums,和一个目标值 target。如果 nums 中 target 出现的次数 超过 nums.length / 2 (即 nums.length // 2 + 1 次及以上),返回 true;否则返回 false。约束:1 ≤ nums.length ≤ 10^4;-10^9 ≤ nums[i], target ≤ 10^9。
核心要点
- 二分定位 + 跳跃检查(标准解) :用二分找到 target 的 第一个 出现位置 i 。若 i + n // 2 < n 且 nums[i + n // 2] == target ,则 target 至少出现 n//2 + 1 次,返回 True。
- 为什么只检查一个位置就够 :若 target 出现次数 > n/2,则从它的第一个位置起,第 n//2 个偏移处必然还是 target;反之若该处不是 target,说明出现次数 ≤ n/2。
- bisect_left 的作用 :它返回第一个 ≥ target 的位置,即 target 的首次出现位置。
- 边界处理 :需判断 i + n // 2 < n ,避免越界。
常见陷阱
- 用 nums.count(target) > n // 2 : count 是 O(n),未利用有序性;虽然能过,但失去了本题的考点。
- 偏移量写成 n // 2 + 1 :正确偏移是 n // 2 。例如 n = 5,target 出现 3 次(> 2.5),首次位置 i 处, nums[i + 2] 必然还是 target(因为 3 > 2)。
- 忘记越界判断 j < n : i + n // 2 可能 ≥ n。
- 误以为要统计出现次数 :只需一次跳跃检查即可判定。
参考实现
from bisect import bisect_left
class Solution:
def isMajorityElement(self, nums: list[int], target: int) -> bool:
n = len(nums)
i = bisect_left(nums, target) # target 的首次出现位置
j = i + n // 2 # 偏移 n/2 个位置
return j < n and nums[j] == target
# 手写二分版本:
# l, r = 0, n
# while l < r:
# mid = (l + r) // 2
# if nums[mid] < target: l = mid + 1
# else: r = mid
# i = l
# return i + n // 2 < n and nums[i + n // 2] == target