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. java面试题—精选30道Java笔试题解答(一)

    下面都是我自己的答案非官方,仅供参考,如果有疑问或错误请一定要提出来,大家一起进步啦~~~ 1. 下面哪些是Thread类的方法() A start() B run() C exit() D getP ...

  2. Memcached安装使用教程及常见问题

    一.Windows下安装memcahed 1.下载memcache的windows稳定版,解压放某个盘下面,比如在c:/memcached2.在终端(也即cmd命令界面)下输入"c:/mem ...

  3. Android NDK开发之从环境搭建到Demo级十步流

    写在正文之前: 几个月没有更新博客,感觉有点生疏了,所以说不能断,一断人就懒. 其实这几个月也并不是什么事也没有做,俺可是时刻想着今年的任务呢,10本书,30篇博文...,这几个月间断性的也是在学习中 ...

  4. uoj#228 基础数据结构练习题

    题面:http://uoj.ac/problem/228 正解:线段树. 我们可以发现,开根号时一个区间中的数总是趋近相等.判断一个区间的数是否相等,只要判断最大值和最小值是否相等就行了.如果这个区间 ...

  5. APP反编译第一课《如何找到核心代码》

    相信很多人都应该会去接触APP反编译,本小七给大家带来入门级别套路,自己也在慢慢摸索学习,一起成长吧.第一步,反编译需要的工具有:一.java环境,其实这里你只要安装了burp就不用管这个的二.apk ...

  6. DirectFB 之 动画播放初步

    在基于linux的嵌入式仿真平台开发中,终端的美观和可定制是一个重要的问题.单调的"白纸黑字"型表现方式可谓大煞风景.改造linux控制台使之美观可定制地展示开机信息和logo成为 ...

  7. 基于CSS的个人网页

    前端时间做的CSS作业:基于CSS的个人网页 基于CSS的个人网页 效果图: 代码: <!DOCTYPE html> <html> <head> <meta ...

  8. VM VirtrualBox 安装centos6.5后的网络设置

    小白学习linux(一) 本文只是介绍VirtrualBox安装centos完成后的网络设置: 每次用虚拟机装完centos后,捣鼓半天才能上网.熟话说的好,好记性不如烂笔头,写个随笔记录下设置过程, ...

  9. VueJs开发笔记—IDE选择和WebStorm性能优化、框架特性和数据调用、路由选项以及使用

    一.IDE的选择: VsCode和WebStorm都是不错的选择,两者运行调试都非常的方便都可以使用快捷键运行和停止,就打开项目的速度和对电脑配置的要求来说,vscode要比webstorm要出色很多 ...

  10. ShadowBroker释放的NSA工具中Esteemaudit漏洞复现过程

    没有时间测试呢,朋友们都成功复现,放上网盘地址:https://github.com/x0rz/EQGRP 近日臭名昭著的方程式组织工具包再次被公开,TheShadowBrokers在steemit. ...