Codility-task 1-Tape Equilibrium

原创 2015年07月06日 19:01:08

Codility是个great的OJ,用起来非常爽,所以我要来CSDN上安利一下!大笑

网址是 https://codility.com/programmers/challenges/

Task Description

A non-empty zero-indexed array A consisting of N integers is given. Array A represents numbers on a tape.

Any integer P, such that 0 < P < N, splits this tape into two non-empty parts: A[0], A[1], ..., A[P − 1] and A[P], A[P + 1], ..., A[N − 1].

The difference between the two parts is the value of: |(A[0] + A[1] + ... + A[P − 1]) − (A[P] + A[P + 1] + ... + A[N − 1])|

In other words, it is the absolute difference between the sum of the first part and the sum of the second part.

For example, consider array A such that:

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

We can split this tape in four places:

  • P = 1, difference = |3 − 10| = 7 
  • P = 2, difference = |4 − 9| = 5 
  • P = 3, difference = |6 − 7| = 1 
  • P = 4, difference = |10 − 3| = 7 

Write a function:

int solution(vector<int> &A);

that, given a non-empty zero-indexed array A of N integers, returns the minimal difference that can be achieved.

For example, given:

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

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 [−1,000..1,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.


题目的等级是easy,无需多言,下边是我的解答。


//Codility 
//Zhang Wenjian 07-06-2015
int solution(vector<int> &A) {
    // write your code in C++11
    long long sum;
    long long th;
    
    for(vector<int>::size_type it=0;it<A.size();it++)
    sum+=A[it];
    th=abs(sum-2*A[0]);
    if(A.size()==2)
    return abs(A[0]-A[1]);
    for(vector<int>::size_type it=0;it<A.size()-1;it++)
    {
        sum-=2*A[it];
        if(abs(sum)<th)
        th=abs(sum);
    }
    
    return th;
}


相关文章推荐

codility上的练习 (1)

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

listening_task_1_(2010-3-3)

  • 2010年03月03日 23:44
  • 48KB
  • 下载

Task1 notMNIST

这个作业主要是训练一个logistics regression模型,用来识别图片的字母,字母从’A’到‘J’共十种,与MNIST类似。 首先要下载jupyter以及其他包,具体过程见我知乎上的回答 ...

TASK1: STM32F051通过SPI与常见FLASH(W25X16/64)通信

TASK1: STM32F051通过SPI与常见FLASH(W25X16/64)通信 本人在实习期间的第一项学习任务就是在STM32F051的开发板STM32F0DISCOVERY上学习基本操作,并...
  • szzjlzy
  • szzjlzy
  • 2015年07月21日 08:30
  • 504

1k_coding task

  • 2011年12月05日 00:35
  • 9.81MB
  • 下载

android task与back stack 开发文档翻译 - 1

由于本人英文能力实在有限,不足之初敬请谅解 本博客只要没有注明“转”,那么均为原创,转贴请注明链接 android task与back stack 开发文档翻译 - 1 android task...
  • su1216
  • su1216
  • 2012年08月07日 13:23
  • 1894

Flink Runtime 1.0 Notes: Task Execution(1)

AboutI will try to give the mainline of how does Flink task works.Main classes and methods are menti...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Codility-task 1-Tape Equilibrium
举报原因:
原因补充:

(最多只允许输入30个字)