[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 ...
随机推荐
- JS正则表达式验证账号、手机号、电话和邮箱
JS正则表达式验证账号.手机号.电话和邮箱 效果体验:http://keleyi.com/keleyi/phtml/jstexiao/15.htm 验证帐号是否合法 验证规则:字母.数字.下划线组成, ...
- 解决:NoSuchAlgorithmException: Algorithm HmacSHA1 not available
windows下运行macInstance = Mac.getInstance("HmacSHA1");完全正常,Linux下则出现异常: java.security.NoSuch ...
- ruby Matrix 输出 格式化
require 'matrix' class Matrix def to_pretty_s s = "" i = 0 while i < self.column_size s ...
- history对象的一些知识点
history对象可以保存用户的上网的历史记录,即从窗口被打开的那一刻算起.这里有个比较纠结的问题,出于安全因素的考虑,开发人员无法得知用户浏览过的URL, 只能通过用户访问过的页面列表,实现后退和前 ...
- 半小时快速了解redis,基于ubuntu 12.04 + redis 2.8.9
一.什么是redis ? 其官方介绍是: Redis is what is called a key-value store, often referred to as a NoSQL databas ...
- h5页面唤起app(iOS和Android),没有安装则跳转下载页面
浏览器和app没有通信协议,所以h5不知道用户的手机释放安装了app.因此只能是h5去尝试唤起app,若不能唤起,引导用户去下载我们的app. 微信里屏蔽了 schema 协议,如果在微信中打开h5, ...
- oracle win7下 卸载
1 右击“计算机”-->管理-->服务和应用程序-->服务,停掉所有Oracle相关的服务(以Oracle打头的,比如OracleDBConsoleorcl). 2 开始--> ...
- Sanarus公司的Cassi微创乳房活检设备投入使用
这种新型可转动的大核心乳房活检设备,是一种全自动一次性的手工操作的设备.该设备对乳房造成的创伤最小,是传统乳房活检设备很好的替代选择. 该设备被称作Cassi,操作方便而且无需准备时间.无需固定设备的 ...
- Daily Scrum 12.15
今日完成任务: 完成关于主页右侧资源显示的算法优化:解决了下载资源时的异常. 遇到困难: 编译课设这周要检查,小组成员的大部分时间在完成编译课设,时间很紧. 明日任务: 孙思权 完成第二组提供的数据库 ...
- html理解
dispay:inline-block: display:inline不会独占一行,会排在同一行里 display:block 独占一行多个block会各自重起一行 margin:容器外间距 容器到 ...