average slice

A non-empty zero-indexed array A consisting of N integers is given. A pair of integers (P, Q), such that 0 ≤ P < Q < N, is called a slice of array A (notice that the slice contains at least two elements). The average of a slice (P, Q) is the sum of A[P] + A[P + 1] + ... + A[Q] divided by the length of the slice. To be precise, the average equals (A[P] + A[P + 1] + ... + A[Q]) / (Q ? P + 1).

For example, array A such that:

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

contains the following example slices:

  • slice (1, 2), whose average is (2 + 2) / 2 = 2;

  • slice (3, 4), whose average is (5 + 1) / 2 = 3;
  • slice (1, 4), whose average is (2 + 2 + 5 + 1) / 4 = 2.5.

The goal is to find the starting position of a slice whose average is minimal.

Write a function:

int solution(int A[], int N);

that, given a non-empty zero-indexed array A consisting of N integers, returns the starting position of the slice with the minimal average. If there is more than one slice with a minimal average, you should return the smallest starting position of such a slice.

For example, given array A such that:

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

the function should return 1, as explained above.

Assume that:

  • N is an integer within the range [2..100,000];

  • each element of array A is an integer within the range [?10,000..10,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.

Copyright 2009–2015 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.

时间: 2024-08-28 08:18:32

average slice的相关文章

training 2

Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.136 Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.000 Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.198 Average Precision (AP)

详解go语言的array和slice 【二】

上一篇  详解go语言的array和slice [一]已经讲解过,array和slice的一些基本用法,使用array和slice时需要注意的地方,特别是slice需要注意的地方比较多.上一篇的最后讲解到创建新的slice时使用第三个索引来限制slice的容量,在操作新slice时,如果新slice的容量大于长度时,添加新元素依然后使源的相应元素改变.这一篇里我会讲解到如何避免这些问题,以及迭代.和做为方法参数方面的知识点. slice的长度和容量设置为同一个值 如果在创建新的slice时我们把

深度学习方法(十):卷积神经网络结构变化——Maxout Networks,Network In Network,Global Average Pooling

技术交流QQ群:433250724,欢迎对算法.技术感兴趣的同学加入. 最近接下来几篇博文会回到神经网络结构的讨论上来,前面我在"深度学习方法(五):卷积神经网络CNN经典模型整理Lenet,Alexnet,Googlenet,VGG,Deep Residual Learning"一文中介绍了经典的CNN网络结构模型,这些可以说已经是家喻户晓的网络结构,在那一文结尾,我提到"是时候动一动卷积计算的形式了",原因是很多工作证明了,在基本的CNN卷积计算模式之外,很多简

[ jquery 过滤器 slice(start, [end]) ] 此方法用于在选择器的基础之上精确筛选出匹配的子集(可以使用前导限制范围)

此方法用于在选择器的基础之上精确筛选出匹配的子集(可以使用前导限制范围): 1.start:开始选取子集的位置.第一个元素是0.如果是负数,则可以从集合的尾部开始选起 2.end:结束选取自己的位置,如果不指定,则就是本身的结尾 3.参数包含开始,不包含结束 [ start , end ) 实例: <!DOCTYPE html> <html lang='zh-cn'> <head> <title>Insert you title</title>

slice()、splice()详解

前面在开发的时候对于slice().splice()这两个函数老是模糊不清,不清楚具体的参数传参以及用法. 今天写个笔记专门记录一下. 1.slice()  从指定位置删除数组里面的元素,可以传一个或者两个参数.不破坏原来的数组. var a=[1,2,3,4,5]; alert(a.slice(2)); //3,4,5 alert(a.slice(2,1)) //3 2.splice()这个就比slice强大太多了,主要有删除.插入.替换三个功能.基本可以代替slice()使用. 删除:可以删

js中 substr(), substring(), slice()的区别

一.作用 三者都是基于原字符串创建新字符串的方法. 接收一到两个参数,第一个参数截取字符串的开始位置(字符下标,从0开始),第二个参数因方法不同而不同,后面不同点会说到. 另外,三个方法都不会修改原字符串的值. 二.相同点 都可以传入一个或两个参数 只传入一个参数时,都表示从指定下标,截取字符串长度,直到字符串最后一个字符 var str = 'hello sarahshine!'; console.log(str.slice(3)); // 'lo sarahshine!' console.l

javascript中slice(),splice(),split(),substring(),substr()使用方法

1.slice(): Array和String对象都有 在Array中  slice(i,[j]) i为开始截取的索引值,负数代表从末尾算起的索引值,-1为倒数第一个元素j为结束的索引值,缺省时则获取从i到末尾的所有元素 参数返回:返回索引值从i到j的数组,原数组不改变 在String中 slice(i,[j]) 参数说明:i为开始截取的索引值,负数代表从末尾算起的索引值,-1为倒数第一个字符j为结束的索引值,缺省时则获取从i到末尾的所有字符 2.splice() 存在Array中     方法

Average

public class Average { public static void main(String[] args) { // TODO 自动生成的方法存根 int i=args.length; int[]arr=new int [10]; int num =0; int k =0; int p=0; for(int j=0;j<i;j++){ arr[j]=Integer.parseInt(args[j]); if(arr[j]<0){ k++; } else p++; num =nu

PAT甲题题解-1108. Finding Average (20)-字符串处理

求给出数的平均数,当然有些是不符合格式的,要输出该数不是合法的. 这里我写了函数来判断是否符合题目要求的数字,有点麻烦. #include <iostream> #include <cstdio> #include <algorithm> #include <string.h> using namespace std; const int maxn=105; bool islegal(char*str){ int len=strlen(str); int p