[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 ...
随机推荐
- 全是套路——BFS
#include <iostream> #include <vector> #include <string> #include <vector> #i ...
- 国内最给力五大免费VPN商家
国内有很多提供免费VPN的商家,水平也是参差不齐,有的用心服务,为客户提供优质免费VPN线路,进而赢得客户的信任.有的则对免费线路敷衍了事,只对付费线路“尽职尽责”,从而流失大量潜在VIP客户. 笔者 ...
- js学习随笔
prompt 提示; parse解析;slice划分,切片;sort排序: 移除样式,removeAttribute("style") document.getElementByI ...
- Win10切换中英输入法问题
用此方法解决后的效果: Win10系统只剩下"美式键盘"和"搜狗拼音"两种输入法,且默认为美式键盘. 按Ctrl+Shift切换到搜狗拼音,输入完成后,再按Ct ...
- swift 之 闭包
一.闭包 格式:{ ( 参数名:类型, 参数名:类型 .. ) in 内容体 return 返回值 } 最完整的闭包 1.省略参数类型 { ( 参数名, 参数名.. ) ...
- 常用的PHP框架
ThinkPHP http://www.thinkphp.cn Yii http://www.yiichina.com laravel https://laravel ...
- aspx页面,中文乱码解决方案
由于文件编码方式编码方式不统一出现样式中文乱码解决方案: 今天碰到的问题:页面字体样式设置的'微软雅黑',可页面没引用.我调试看到样式出现中文乱码了 这种问题,就需要转换文件的编码方式,如下两步即可解 ...
- iOS9系统分享失败问题解决
因为iOS9系统需要设置打开QQ和微信的白名单,如果出现无法分享或者直接提示分享失败,试一下在infoPlist中添加以下白名单 http://wiki.mob.com/ios9-对sharesdk的 ...
- java中的条件语句(if、if...else、多重if、嵌套if)
Java条件语句之 if 生活中,我们经常需要先做判断,然后才决定是否要做某件事情.例如,如果考试成绩大于 90 分,则奖励一个 IPHONE 5S .对于这种"需要先判断条件,条件满足后才 ...
- JavaScript基础认知
此文只适用于初学者,大神们就不要看了,嘿嘿~ 一.定义变量 关键字 var,由此关键字定义变量,例如:var a =21:就把21这个数定义给了变量a 二.基本数据类型 1.Number类型 表示数字 ...