TGViewer
Python | LeetCode Python | LeetCode @easy_python_task · 9.03K subscribers
Post #2182 850
Задача: 1150. Check If a Number Is Majority Element in a Sorted Array
Сложность: easy

Дан целочисленный массив nums, отсортированный в неубывающем порядке, и целое число target. Верните true, если target является элементом большинства, или false в противном случае.

Элемент большинства в массиве nums — это элемент, который встречается в массиве более чем nums.length / 2 раз.

Пример:
Input: nums = [2,4,5,5,5,5,5,6,6], target = 5
Output: true
Explanation: The value 5 appears 5 times and the length of the array is 9.
Thus, 5 is a majority element because 5 > 9/2 is true.


👨‍💻 Алгоритм:

1⃣Инициализация переменной count:
Инициализируйте переменную count значением 0..

2⃣Итерация по списку nums:
Пройдите по каждому элементу списка nums.
Если элемент num равен target, увеличьте значение переменной count.

3⃣Проверка условия мажоритарного элемента:
Если count больше чем половина длины списка nums, верните true.
В противном случае верните false.

😎 Решение:
class Solution:
def isMajorityElement(self, nums: List[int], target: int) -> bool:
count = 0
for num in nums:
if num == target:
count += 1
return count > len(nums) // 2


Ставь 👍 и забирай 📚 Базу знаний
  • 👍 1
More from @easy_python_task
  1. Oct 11, 2026Post #2439
  2. Oct 11, 2026Задача: 997. Find the Town Judge Сложность: easy В городе есть n человек, помеченных от 1…
  3. Oct 11, 2026Post #2437
  4. Oct 11, 2026Задача: 436. Find Right Interval Сложность: medium Дан массив интервалов, где intervals[i]…
  5. Oct 10, 2026Post #2435
  6. Oct 10, 2026Задача: 1249. Minimum Remove to Make Valid Parentheses Сложность: medium Дана строка s из…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →