[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 ...
随机推荐
- frame busting
[frame busting] 参考:http://book.51cto.com/art/201204/330076.htm
- 忘记BIOS超级管理员密码,怎么破解?
[请尊重原创版权,如需引用,请注明来源及地址] 本人就喜欢没事瞎折腾,动动手活动活动筋骨没坏处,前不久非常便宜的弄到一玩具 ThinkPad T400(公司处理品),外观还算不错,除了电源适配器是坏的 ...
- Android中的事件传递机制
Android源码版本:API Level 19(Android 4.4) Android事件构成 在Android中,事件主要包括点按.长按.拖拽.滑动等,点按又包括单击和双击,另外还包括单指操作和 ...
- 通过RGB灯输出七色
本文由博主原创,如有不对之处请指明,转载请说明出处. /********************************* 代码功能:输出模拟信号,控制RGB灯的颜色 使用函数: pinMode(引脚 ...
- Android之listview && adapter
今天我们讲的也是非常重要的一个控件listview-最常用也是最难的 一个ListView通常有两个职责. (1)将数据填充到布局. (2)处理用户的选择点击等操作. 第一点很好理解,ListView ...
- ubuntu pip 安装django报错解决
系统版本 ubuntu Kylin 16.04 LTS 安装pip3 安装 Django 总是提示time out,无法安装. 逛了好多论坛终于遭到了解决办法,分享保存: sudo pi ...
- Linux cp命令使用说明
Linux cp命令使用说明 --功能说明:复制目录或文件 --命令格式:cp [参数] <文件或目录> <文件或目录> --常用参数: -R 复制目录 -i 覆盖文件之 ...
- iOS中坐标转换
坐标转换,可以用UIVIew的方法 //由要转换坐标view的superView执行该方法,rect为待转换view的frame,view是要显示到哪儿的 - (CGRect)convertRect: ...
- EasyUI中Treegrid节点的删除
// 删除function removes() { var rows = ruletreegrid.treegrid('getSelections'); if (rows &&am ...
- bzoj 1051 (强连通) 受欢迎的牛
题目:这里 题意: Description 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎. 这 种关系是具有传递性的,如果A认为B受欢迎,B认为 ...