[UCSD白板题] Majority Element
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的更多相关文章
- LeetCode算法题-Majority Element(Java实现)
这是悦乐书的第181次更新,第183篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第40题(顺位题号是169).给定大小为n的数组,找到数组中出现次数超过n/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 ...
- [UCSD白板题] Maximize the Value of an Arithmetic Expression
Problem Introduction In the problem, your goal is to add parentheses to a given arithmetic expressio ...
- [UCSD白板题] Compute the Edit Distance Between Two Strings
Problem Introduction The edit distinct between two strings is the minimum number of insertions, dele ...
- [UCSD白板题] Take as Much Gold as Possible
Problem Introduction This problem is about implementing an algorithm for the knapsack without repeti ...
- [UCSD白板题] Primitive Calculator
Problem Introduction You are given a primitive calculator that can perform the following three opera ...
- [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 ...
- [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 ...
- [UCSD白板题] Sorting: 3-Way Partition
Problem Introduction The goal in this problem is to redesign a given implementation of the randomize ...
随机推荐
- DB2 runstats、reorgchk、reorg 命令
runstats.reorgchk.reorg 1.runstats runsats可以搜集表的信息,也可以搜集索引信息.作为runstats本身没有优化的功能,但是它更新了统计信息以后,可以让DB2 ...
- Xml与DataTable相互转换方法
1.Xml与DataTable相互转换方法:http://www.cnblogs.com/lilin/archive/2010/04/18/1714927.html
- 博弈论揭示了深度学习的未来(译自:Game Theory Reveals the Future of Deep Learning)
Game Theory Reveals the Future of Deep Learning Carlos E. Perez Deep Learning Patterns, Methodology ...
- JS-小球碰撞反弹
类似于屏保的一种动画,当小球碰到边框时,发生反弹,并且变化颜色. <!DOCTYPE html><html lang="en"><head> ...
- gulp LiveReload middleware
用yo搭建的angular项目,用gulp自动化构建. 自动化构建主要的功能大致有: 1. 文件压缩 2. 文件重命名 3. 文件合并 4. css,js文件自动引入到html 5. 自动刷新 ... ...
- 伸缩盒 Flexible Box(旧)
box-orient box-pack box-align box-flex box-flex-group box-ordinal-group box-direction box ...
- JVM常量池
常量池(constant_pool)指的是在编译期被确定,并被保存在已编译的.class文件中的一些数据.它包括了关于类.方法.接口等中的常量,也包括字符串常量和符号引用.运行时常量池是方法区的一部分 ...
- 浏览器缓存详解:expires,cache-control,last-modified,etag详细说明
最近在对CDN进行优化,对浏览器缓存深入研究了一下,记录一下,方便后来者 画了一个草图: 每个状态的详细说明如下: 1.Last-Modified 在浏览器第一次请求某一个URL时,服务器端的返回状态 ...
- python获取路径
#!/usr/bin/env python import os #文件所在路径(模块路径) print __file__,os.path.realpath(__file__),os.path.absp ...
- inotify resources exhausted
inotify resources exhausted tail -f /var/log/kubelet.log tail: inotify resources exhausted tail: ino ...