DSA Section簡単

Boyer Moore Voting

Detailed guide and Python implementation for the 'Boyer Moore Voting' problem.

問題提起

簡単

Write a function boyer_moore_majority(arr) that implements the Boyer-Moore Majority Vote Algorithm to find the majority element (the element that appears more than n // 2 times) in a list of integers arr. If no majority element exists, return -1.

制約
  • 1 <= len(arr) <= 5 * 10^4
  • -10^9 <= arr[i] <= 10^9

Example 1
Input
arr = [1, 1, 1, 2, 2]
Output
1
Explanation

1 appears 3 times which is greater than 5 // 2 = 2 times.

Example 2
Input
arr = [1, 2, 3]
Output
-1
Explanation

No element appears more than 3 // 2 = 1 time.

Need a Hint?
Consider using Arrays-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

解決する準備はできましたか?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

エディタで開く
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

推奨される Python リソース

関連するインタラクティブなチュートリアル、チートシート、コード比較で知識を深めてください。