https://codility.com/demo/take-sample-test/min_avg_two_slice

此题要求一个数组子段的最小的平均数(返回第一个数字的index)。刚开始想记录sum,也没用,因为所有子段的组合是O(n^2)的。后来看解释发现,最小值必然存在于长度为2或3的子段中(长度为3的子段也无法分解)。可以用反证法,如果有一个长度大于3的子段,均值小于之前找出的长度2和3的子段均值,那么必然可以在这个子段中找到均值更小的长度2和3的子段,矛盾。

int solution(vector<int> &A) {
// write your code in C++11
double minVal = (A[0] + A[1]) / 2;
int result = 0;
int N = A.size();
for (int i = 0; i < N - 1; i++)
{
double avg = (A[i] + A[i+1])/2.0;
if (avg < minVal)
{
minVal = avg;
result = i;
}
}
for (int i = 0; i < N - 2; i++)
{
double avg = (A[i] + A[i+1] + A[i+2])/3.0;
if (avg < minVal)
{
minVal = avg;
result = i;
}
}
return result;
}

  

*[codility]MinAvgTwoSlice的更多相关文章

  1. Codility NumberSolitaire Solution

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

  2. codility flags solution

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

  3. GenomicRangeQuery /codility/ preFix sums

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

  4. *[codility]Peaks

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

  5. *[codility]Country network

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

  6. *[codility]AscendingPaths

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

  7. *[codility]MaxDoubleSliceSum

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

  8. *[codility]Fish

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

  9. *[codility]CartesianSequence

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

随机推荐

  1. C#简单的加密类

    1.加密 public class EncryptHepler { // 验值 static string saltValue = "XXXX"; // 密码值 static st ...

  2. 本地安装gem install --local redis-stat-0.4.13.gem

    因为主机环境不能联外网,悲哀,所以只能想办法下载包,上传到主机来安装 环境:el6.x86_64 1. gem 安装[http://centos.ustc.edu.cn/centos/6/os/x86 ...

  3. EcilpsePHP studio 3.0 运行(run)环境配置

    EcilpsePHP studio 3.0的界面与 MyEclipse操作界面基本一样,熟悉后者的对于EcilpsePHP studio 的使用学习就不会太难了. 安装好EPP后,新建项目--> ...

  4. Mysql主从同步(复制)

    目录: mysql主从同步定义      主从同步机制 配置主从同步      配置主服务器      配置从服务器 使用主从同步来备份      使用mysqldump来备份      备份原始文件 ...

  5. python time模块和datetime模块详解

    一.time模块 time模块中时间表现的格式主要有三种: a.timestamp时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量 b.struct_time时间元组,共 ...

  6. google calendar

    1. user guide on google https://developers.google.com/google-apps/calendar/instantiate 2. google app ...

  7. java集合类(二)List学习

    接上篇  java集合类(一) List接口继承了Collection接口和Iterable接口,即同样含有Collection和 Iterable的特性,还有方法,其基本方法有: 1)有关添加: b ...

  8. [resource]Github上维护的一个机器学习相关的框架,库和工具列表

    https://github.com/josephmisiti/awesome-machine-learning  A curated list of awesome Machine Learning ...

  9. sqlserver2008 解决 ldf文件过大的方法

    SQL2008清空删除日志: '在SQL2008中清除日志就必须在简单模式下进行,等清除动作完毕再调回到完全模式. ------------------------------------------ ...

  10. Virtualbox虚拟机设置不完全笔记

    先说说我想实现的东西,我想在虚拟机安装各种开发环境,我个人在学习Node.然后我装了一个Ubuntu Server,所以我又想共享一个windows下的文件夹,这样可以让我在windows下开发,在L ...