Codility---Dominator
|
Task description
A zero-indexed array A consisting of N integers is given. The dominator of array A is the value that occurs in more than half of the elements of A. For example, consider array A such that A[0] = 3 A[1] = 4 A[2] = 3 A[3] = 2 A[4] = 3 A[5] = -1 A[6] = 3 A[7] = 3 The dominator of A is 3 because it occurs in 5 out of 8 elements of A (namely in those with indices 0, 2, 4, 6 and 7) and 5 is more than a half of 8. Write a function
that, given a zero-indexed array A consisting of N integers, returns index of any element of array A in which the dominator of A occurs. The function should return −1 if array A does not have a dominator. Assume that:
For example, given array A such that A[0] = 3 A[1] = 4 A[2] = 3 A[3] = 2 A[4] = 3 A[5] = -1 A[6] = 3 A[7] = 3 the function may return 0, 2, 4, 6 or 7, as explained above. Complexity:
Elements of input arrays can be modified. |
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
int size = 0, leader = -1;
for(int i=0; i<A.length;i++) {
if(size == 0) {
leader = A[i];
}
if(leader == A[i]) {
size++;
} else {
size--;
}
}
if(size == 0) return -1;
int index = 0, count = 0;
for(int i=0; i<A.length; i++) {
if(leader == A[i]) {
index = i;
count++;
}
}
if(count*2 <= A.length) return -1;
return index;
}
}
https://codility.com/demo/results/trainingK4VATM-6K8/
Codility---Dominator的更多相关文章
- codility上的练习 (1)
codility上面添加了教程.目前只有lesson 1,讲复杂度的……里面有几个题, 目前感觉题库的题简单. tasks: Frog-Jmp: 一只青蛙,要从X跳到Y或者大于等于Y的地方,每次跳的距 ...
- Codility NumberSolitaire Solution
1.题目: A game for one player is played on a board consisting of N consecutive squares, numbered from ...
- codility flags solution
How to solve this HARD issue 1. Problem: A non-empty zero-indexed array A consisting of N integers i ...
- GenomicRangeQuery /codility/ preFix sums
首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which ...
- *[codility]Peaks
https://codility.com/demo/take-sample-test/peaks http://blog.csdn.net/caopengcs/article/details/1749 ...
- *[codility]Country network
https://codility.com/programmers/challenges/fluorum2014 http://www.51nod.com/onlineJudge/questionCod ...
- *[codility]AscendingPaths
https://codility.com/programmers/challenges/magnesium2014 图形上的DP,先按照路径长度排序,然后依次遍历,状态是使用到当前路径为止的情况:每个 ...
- *[codility]MaxDoubleSliceSum
https://codility.com/demo/take-sample-test/max_double_slice_sum 两个最大子段和相拼接,从前和从后都扫一遍.注意其中一段可以为0.还有最后 ...
- *[codility]Fish
https://codility.com/demo/take-sample-test/fish 一开始习惯性使用单调栈,后来发现一个普通栈就可以了. #include <stack> us ...
- *[codility]CartesianSequence
https://codility.com/programmers/challenges/upsilon2012 求笛卡尔树的高度,可以用单调栈来做. 维持一个单调递减的栈,每次进栈的时候记录下它之后有 ...
随机推荐
- WM_NOTIFY消息流程实例分析
我们以CListCtrl控件为例来分析WM_NOTIFY消息. CListCtrl控件在Report样式下会包含CHeaderCtrl标头控件,即CHeaderCtrl标头控件为CListCtrl控件 ...
- leetcode先刷_Valid Sudoku
我没有看到这个问题,这使其在现货需求数独,害怕一直没敢做.后来我发现原来的标题就是这么简单.推断现在只有数字全不符合的就可以了棋盘上的形势的要求. 是不是正确的三个周期..人是不能满意地看到每一行.每 ...
- O'Stolz 定理及其应用
1. 基本形式 对于 ⋆∞(分母为无穷大,分子无要求),设两数列 an,bn,满足: bn 严格单调递增: limn→∞bn=∞ 如果有 limn→∞an+1−anbn+1−bn=L(L 为有限实数) ...
- jQuery怎么选择两个class属性
Jquery选择多个Class属性: $('.className1,.className2,.className4,.className5')
- ubuntu grub 操作
系统开机时,按住 shift 进入 grub 1. 什么是 Grub GNU GRUB(GRand Unified Bootloader 简称"GRUB")是一个来自GNU项目的多 ...
- 51系列小型操作系统精髓 简单实现6 C语言版待改进
#include "STC12C5A.H" #define TIMER_RELOAD() {TL0=0x00;TH0=0xC4;}//使能T/C 初始10ms #define ...
- either you have JavaScript disabled or your browser does not support JavaScript
作者:朱金灿 来源:http://blog.csdn.net/clever101 在服务器(操作系统为WindowsServer)上部署Hudson平台,使用IE访问:http://localhost ...
- 用Delphi即时判断当前的网络的连接方式
判断网络的是否连接或者连接方式,比较重要的一点就是是不是即时判断,如果你明明无连接,判断出来却说你有,连接上了,却说你还未连接,下面要介绍的两个方法中 InternetGetConnectedStat ...
- 全栈工程师之路(二)—— JavaScript(网页前端脚本语言)
javascript 是可以运行在网页前端的脚本语言,可以基于 html 之上实现更丰富的交互(网页内容的交互显示).异步回调.多线程.定时器.动画等. hello_world.html <ht ...
- 各个版本 Windows 10 系统中自带的 .NET Framework 版本
原文各个版本 Windows 10 系统中自带的 .NET Framework 版本 Windows 名称 Windows 版本 自带的 .NET Framework 版本 Windows 10 Oc ...