Task description

A non-empty zero-indexed array A consisting of N integers is given.

The leader of this array is the value that occurs in more than half of the elements of A.

An equi leader is an index S such that 0 ≤ S < N − 1 and two sequences A[0], A[1], ..., A[S] and A[S + 1], A[S + 2], ..., A[N − 1] have leaders of the same value.

For example, given array A such that:

A[0] = 4 A[1] = 3 A[2] = 4 A[3] = 4 A[4] = 4 A[5] = 2

we can find two equi leaders:

  • 0, because sequences: (4) and (3, 4, 4, 4, 2) have the same leader, whose value is 4.
  • 2, because sequences: (4, 3, 4) and (4, 4, 2) have the same leader, whose value is 4.

The goal is to count the number of equi leaders.

Write a function:

class Solution { public int solution(int[] A); }

that, given a non-empty zero-indexed array A consisting of N integers, returns the number of equi leaders.

For example, given:

A[0] = 4 A[1] = 3 A[2] = 4 A[3] = 4 A[4] = 4 A[5] = 2

the function should return 2, as explained above.

Assume that:

  • N is an integer within the range [1..100,000];
  • each element of array A is an integer within the range [−1,000,000,000..1,000,000,000].

Complexity:

  • expected worst-case time complexity is O(N);
  • expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments).

Elements of input arrays can be modified.

Solution

 
Programming language used: Java
Total time used: 26 minutes
 
Code: 02:26:16 UTC, java, final, score:  100
// 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, count=0, fcnt=0, res=0;
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 0;
for(int i=0; i<A.length; i++) {
if(leader == A[i]) {
count++;
}
}
for(int i=0; i<A.length; i++) {
if(leader == A[i] ) {
fcnt++;
}
if((i+1) < fcnt*2 && (A.length-i-1) < (count-fcnt)*2) {
res++;
}
}
return res;
}
}
https://codility.com/demo/results/trainingKHWPS7-27V/

Codility---EquiLeader的更多相关文章

  1. [codility]Equi-leader

    http://codility.com/demo/take-sample-test/equileader 一开始想到从左和右两边开始扫取众数,但求众数又要重新扫一遍,这样复杂度就是O(n^2)了.此题 ...

  2. Codility NumberSolitaire Solution

    1.题目: A game for one player is played on a board consisting of N consecutive squares, numbered from ...

  3. codility flags solution

    How to solve this HARD issue 1. Problem: A non-empty zero-indexed array A consisting of N integers i ...

  4. GenomicRangeQuery /codility/ preFix sums

    首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which ...

  5. *[codility]Peaks

    https://codility.com/demo/take-sample-test/peaks http://blog.csdn.net/caopengcs/article/details/1749 ...

  6. *[codility]Country network

    https://codility.com/programmers/challenges/fluorum2014 http://www.51nod.com/onlineJudge/questionCod ...

  7. *[codility]AscendingPaths

    https://codility.com/programmers/challenges/magnesium2014 图形上的DP,先按照路径长度排序,然后依次遍历,状态是使用到当前路径为止的情况:每个 ...

  8. *[codility]MaxDoubleSliceSum

    https://codility.com/demo/take-sample-test/max_double_slice_sum 两个最大子段和相拼接,从前和从后都扫一遍.注意其中一段可以为0.还有最后 ...

  9. *[codility]Fish

    https://codility.com/demo/take-sample-test/fish 一开始习惯性使用单调栈,后来发现一个普通栈就可以了. #include <stack> us ...

  10. *[codility]CartesianSequence

    https://codility.com/programmers/challenges/upsilon2012 求笛卡尔树的高度,可以用单调栈来做. 维持一个单调递减的栈,每次进栈的时候记录下它之后有 ...

随机推荐

  1. 如何做一个导航栏————浮动跟伪类(hover)事件的应用

    我们先说一下伪类选择器的写法: 写法:选择器名称:伪类状态{}4 常见伪类状态: 未访问:link 鼠标移上去:hover 激活选定:active 已访问:visited 获得焦点的时候触发:focu ...

  2. 重启mysql提示:The server quit without updating PID file问题的解决办法

    今天因为需要开启事件调度器event_scheduler,所以修改了mysql的配置文件/etc/my.cnf 就因为配置多了个分号,导致一直启动失败,如下图所示: 然后去网上搜了帖子(MySQL提示 ...

  3. mac下安装MySQL完整步骤(图文详情)

    原文摘自:http://www.jb51.net/article/103841.htm 最近使用Mac系统,准备搭建一套本地web服务器环境.因为Mac系统自带PHP和apach,但是没有自带mysq ...

  4. (转)详解JS位置、宽高属性之一:offset系列

    很多初学者对于JavaScript中的offset.scroll.client一直弄不明白,虽然网上到处都可以看一张图(图1),但这张图太多太杂,并且由于浏览器差异性,图示也不完全正确. 图一 不知道 ...

  5. GitExtensions-2.48安装详细教程

    在安装GitExtensions时你可能遇到如下问题,如果出现此提示,则先退出安装,去下载安装.NET Framework4.0之后,再启动GitExtension的安装. 开始进行安装: 安装完成, ...

  6. “永恒之蓝"漏洞的紧急应对--毕业生必看

    早上6点多起床了,第一次起这么早,昨天晚上12点多,看到了一则紧急通知,勒索软件通过微软"永恒之蓝"漏洞针对教育网进行了大规模的攻击,而且有很多同学中招.中招后的结果如下图所示. ...

  7. Linux-vim学习入门

    1.前言     vi/vim是linux中很重要的文本编辑器.我第一次使用这个编辑器时,很不习惯,甚至都不知道如何移动光标和插入字符.慢慢地经过学习,才知道如何使用vi/vim.     vi/vi ...

  8. OutputStream类详解

    主要内容包括OutputStream及其部分子类,以分析源代码的方式学习.关心的问题包括:每个字节输出流的作用,各个流之间的主要区别,何时使用某个流,区分节点流和处理流,流的输出目标等问题. Outp ...

  9. 头皮发麻的HTML课时一

    话说我都不知道有多少天没有更新我的随笔了,不过我忽的一下发现到灵魂深处的罪孽:好吧,不扯淡了,其实就是自己懒得外加上HTML这个东西又实在是很重要,所以良心发现把我自己所学的给记录下来,我会尽量的写的 ...

  10. window(x64)+IIS+Access发布网站出现HTTP 错误 404.0 - Not Found

    我的电脑是window7 64位,昨天用ASP.NET写好一个网页,想用IIS发布到校园局域网,配置好IIS,谁知道最后还是出现问题,截图如下 于是我就上网找解决方案,网上各种版本都有,有的说是文件路 ...