http://codility.com/demo/take-sample-test/equileader

一开始想到从左和右两边开始扫取众数,但求众数又要重新扫一遍,这样复杂度就是O(n^2)了。
此题的关键在于Equi-Leader必然是众数,否则不可能左边和右边都是众数。
所以先求出众数及其出现次数,再扫就行了。

// you can also use includes, for example:
// #include <algorithm>
int solution(vector<int> &A) {
// write your code in C++98
int x = A[0];
int cnt = 0;
for (int i = 1; i < A.size(); i++) {
if (A[i] == x) {
cnt++;
}
else if (cnt > 0) {
cnt--;
}
else {
cnt = 1;
x = A[i];
}
}
int total = 0;
for (int i = 0; i < A.size(); i++) {
if (A[i] == x) total++;
}
if (total <= A.size() / 2) return 0;
int ans = 0;
int currentTotal = 0;
for (int i = 0; i < A.size() - 1; i++) {
if (A[i] == x)
currentTotal++;
if ((currentTotal > (i + 1) / 2) &&
((total - currentTotal) > (A.size() - i - 1) / 2)) {
ans++;
}
}
return ans;
}

  

[codility]Equi-leader的更多相关文章

  1. Codility---EquiLeader

    Task description A non-empty zero-indexed array A consisting of N integers is given. The leader of t ...

  2. codility上的练习 (1)

    codility上面添加了教程.目前只有lesson 1,讲复杂度的……里面有几个题, 目前感觉题库的题简单. tasks: Frog-Jmp: 一只青蛙,要从X跳到Y或者大于等于Y的地方,每次跳的距 ...

  3. zookeeper源码分析之五服务端(集群leader)处理请求流程

    leader的实现类为LeaderZooKeeperServer,它间接继承自标准ZookeeperServer.它规定了请求到达leader时需要经历的路径: PrepRequestProcesso ...

  4. Team Leader 你不再只是编码, 来炖一锅石头汤吧

    h3{ color: #000; padding: 5px; margin-bottom: 10px; font-weight: bolder; background-color: #ccc; } h ...

  5. 【分布式】Zookeeper的Leader选举

    一.前言 前面学习了Zookeeper服务端的相关细节,其中对于集群启动而言,很重要的一部分就是Leader选举,接着就开始深入学习Leader选举. 二.Leader选举 2.1 Leader选举概 ...

  6. Codility NumberSolitaire Solution

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

  7. codility flags solution

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

  8. Curator leader 选举(一)

    要想使用Leader选举功能,需要添加recipes包,可以在maven中添加如下依赖: <dependency> <groupId>org.apache.curator< ...

  9. GenomicRangeQuery /codility/ preFix sums

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

  10. 我眼中的项目leader

    个人觉得项目leader应该具备一下基础: 1.技术能力 2.领导能力 3.过滤产品不合理需求能力 4.项目周期把控能力

随机推荐

  1. 工作流引擎Activiti使用总结

    http://www.kafeitu.me/activiti/2012/03/22/workflow-activiti-action.html 1.简单介工作流引擎与Activiti 对于工作流引擎的 ...

  2. ASP防止盗链的一段代码

    <%@Language="vbscript"%> <% Response.Buffer = true Response.Expires = -1441 ' var ...

  3. Linq DataTable Group By 分组显示人员明细

    实现功能:       多个字段分组源码样例: 原始数据: 分组后的输出结果: 源代码: public static void PrintPersons() { //准备数据 DataTable dt ...

  4. 使用repeater控件显示列表替代treeview

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  5. 自定义popupmenu菜单背景色

    procedure TForm1.N11DrawItem(Sender: TObject; ACanvas: TCanvas; ARect: TRect; Selected: Boolean); be ...

  6. 三分钟学会CSS3中的FLEXBOX布局

    原文地址,保护版权,请勿转载:http://page.factj.com/blog/p/2574 这篇文章里我们将学习CSS里flexbox布局的几个最重要的概念,通过学习flexbox布局,你会发现 ...

  7. javascript 第28节 jQuery事件、迭代、样式

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. [译]当你在浏览器输入url后发生了什么

    面试题会经常问这个,之前也被问过,回答的不是很好,后来看到百度前端的一篇博客,啰嗦了好多,很么触摸屏都上了..后来看到stackoverflow上的一个回答,比较短. 原文地址:http://stac ...

  9. OpenJudge 2813 画家问题 / Poj 1681 Painter's Problem

    1.链接地址: http://bailian.openjudge.cn/practice/2813 http://poj.org/problem?id=1681 2.题目: 总时间限制: 1000ms ...

  10. spring常用的连接池属性文件配置

    (一) DBCP 引入jar文件 commons-dbcp-1.2.1.jar commons-pool-1.3.jar二个包. spring配置文件 <bean id="dataSo ...