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 求笛卡尔树的高度,可以用单调栈来做. 维持一个单调递减的栈,每次进栈的时候记录下它之后有 ... 
随机推荐
- 编程算法 - 背包问题(三种动态规划) 代码(C)
			背包问题(三种动态规划) 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目參考: http://blog.csdn.net/caroline_wen ... 
- C# Span 入门
			原文:C# Span 入门 版权声明:博客已迁移到 http://lindexi.gitee.io 欢迎访问.如果当前博客图片看不到,请到 http://lindexi.gitee.io 访问博客.本 ... 
- HDOJ 4901 The Romantic Hero
			DP....扫两次合并 The Romantic Hero Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 ... 
- openresty: nginx worker不同请求之间共享数据
			To globally share data among all the requests handled by the same nginx worker process, encapsulate ... 
- Python属性和方法
			关键字:Python 属性 方法原文:http://www.cafepy.com/article/python_attributes_and_methods/python_attributes_and ... 
- 如何删除您的注册js图书馆bower私人图书馆
			建立你自己bower 这样的私人图书馆参考http://blog.csdn.net/nsrainbow/article/details/35988611 本文 假设我们想注册自己的创作js私人图书馆图 ... 
- 各种Message中文解释(一部分)
			函数功能:该函数将指定的消息发送到一个或多个窗口.此函数为指定的窗口调用窗口程序,直到窗口程序处理完消息再返回.该函数是应用程序和应用程序之间进行消息传递的主要手段之一. 函数原型:LRESUL ... 
- ABP缓存示例
			private readonly ICacheManager _cacheManager; public ProgrammeManage(ICacheManager cacheManager) { _ ... 
- Asp.net MVC Razor输出字符串方法(js中嵌入razor)
			@{ Model p = new Model(); //输出名称和年龄 //1.第一种方式 @:姓名=@p.Name //2.第二中方式 <text>年龄=</text>p.A ... 
- MVC WebApi的两种访问方法
			//UserInfoController using ClassLibrary; using System;using System.Collections.Generic;using System. ... 
