Problem Introduction

An element of a sequence of length \(n\) is called a majority element if it appears in the sequence strictly more than \(n/2\) times.

Problem Description

Task.The goal in this code problem is to check whether an input sequence contains a majority element.

Input Format.The first line contains an integer \(n\), the next one contains a sequence of \(n\) non-negative integers \(a_0,a_1,\cdots,a_{n-1}\).

Constraints.\(1 \leq n \leq 10^5; 0 \leq a_i \leq 10^9\) for all \(0 \leq i < n\).

Output Format.Ouput 1 if the sequence contains a majority element and 0 otherwise.

Sample 1.
Input:

5
2 3 9 2 2

Output:

1

Sample 2.
Input:

4
1 2 3 4

Output:

0

Sample 3.
Input:

4
1 2 3 1

Output:

0

Solution

# Uses python3
import sys

def get_majority_element(a, left, right):
    if left == right:
        return -1
    if left + 1 == right:
        return a[left]
    mid = left + (right - left) // 2
    majority_left = get_majority_element(a, left, mid)
    majority_right = get_majority_element(a, mid, right)
    b = a[left:right]
    threshold = len(b) // 2
    if b.count(majority_left) > threshold:
        return majority_left
    if b.count(majority_right) > threshold:
        return majority_right
    return -1

if __name__ == '__main__':
    input = sys.stdin.read()
    n, *a = list(map(int, input.split()))
    if get_majority_element(a, 0, n) != -1:
        print(1)
    else:
        print(0)

[UCSD白板题] Majority Element的更多相关文章

  1. LeetCode算法题-Majority Element(Java实现)

    这是悦乐书的第181次更新,第183篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第40题(顺位题号是169).给定大小为n的数组,找到数组中出现次数超过n/2的元素.假 ...

  2. [UCSD白板题] Longest Common Subsequence of Three Sequences

    Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...

  3. [UCSD白板题] Maximize the Value of an Arithmetic Expression

    Problem Introduction In the problem, your goal is to add parentheses to a given arithmetic expressio ...

  4. [UCSD白板题] Compute the Edit Distance Between Two Strings

    Problem Introduction The edit distinct between two strings is the minimum number of insertions, dele ...

  5. [UCSD白板题] Take as Much Gold as Possible

    Problem Introduction This problem is about implementing an algorithm for the knapsack without repeti ...

  6. [UCSD白板题] Primitive Calculator

    Problem Introduction You are given a primitive calculator that can perform the following three opera ...

  7. [UCSD白板题] Points and Segments

    Problem Introduction The goal in this problem is given a set of segments on a line and a set of poin ...

  8. [UCSD白板题] Number of Inversions

    Problem Introduction An inversion of a sequence \(a_0,a_1,\cdots,a_{n-1}\) is a pair of indices \(0 ...

  9. [UCSD白板题] Sorting: 3-Way Partition

    Problem Introduction The goal in this problem is to redesign a given implementation of the randomize ...

随机推荐

  1. ES 集群

    http://www.cnblogs.com/o-andy-o/p/5067184.html

  2. 使用Word发布文章到 WordPress 博客

    使用Word发布文章到 WordPress 博客 我们都知道,WordPress 自带的编辑器功能比较弱,而使用 Word 编辑文档却功能强大.其实我们使用 Word 编辑好的文档也是可以直接发布到 ...

  3. js中的一些容易混淆的方法!

    数组的一些方法: 1.join()和split()方法  与之相反的是split()方法:用于把一个字符串分割成字符串数组.  注意返回的数组中不包括separator本身: 提示和注释注释:如果把空 ...

  4. python 模拟登录--day1

    ---------------edit-in-windows-by-pycharm---------------------import randomuname = "mologa" ...

  5. dockManager中DockPanel的刷新问题!

    使用的是DevExpress的dockManager控件,新增一个DockPanel,在DockPanel中添加一个AxMapControl控件并设置默认加载地图. 实现效果图如下: 但是存在一个问题 ...

  6. redis 的消息发布订阅

    redis支持pub/sub功能(可以用于消息服务器),这个功能类似mq,这里做一个简单的介绍 Pub/Sub Pub/Sub 从字面上理解就是发布(Publish)与订阅(Subscribe),在R ...

  7. homestead注意事项

    1.如何修改php.ini Here is how you grant read/write access to php.ini, modify it, save changes & relo ...

  8. Java多线程简析

    一.线程的状态: 线程共有下面4种状态: 1.新建状态(New): 新创建了一个线程对象,当你用new创建一个线程时,该线程尚未运行. 2.就绪状态(Runnable): 线程对象创建后,其他线程调用 ...

  9. URAL 1992 CVS 可持久化链栈

    http://www.cnblogs.com/tedzhao/archive/2008/11/12/1332112.html 看这篇的链表部分的介绍应该就能理解“可持久化”了 动态分配内存的会T,只能 ...

  10. windows下搭建scrapywindows 7 (64) + python 3.5 (64)

    说明 之前在 window 10 (64) + python 3.5 (64) 环境下就已经成功安装了 scrapy,当然也费了不少周折. 由于近日将系统换回 windows 7 (64),再安装 s ...