Here is a simple program test task, it doesn't have very diffcult logic:

A zero-indexed array A consisting of N integers is given. An equilibrium index of this array is any integer P such that 0 ≤ P < N and the sum of elements of lower indices is equal to the sum of elements of higher indices, i.e.

A[0] + A[1] + ... + A[P−1] = A[P+1] + ... + A[N−2] + A[N−1].

Sum of zero elements is assumed to be equal to 0. This can happen if P = 0 or if P = N−1.

For example, consider the following array A consisting of N = 7 elements:

A[0] = -7   A[1] =  1   A[2] = 5
A[3] = 2 A[4] = -4 A[5] = 3
A[6] = 0

P = 3 is an equilibrium index of this array, because:

  • A[0] + A[1] + A[2] = A[4] + A[5] + A[6]

P = 6 is also an equilibrium index, because:

  • A[0] + A[1] + A[2] + A[3] + A[4] + A[5] = 0

and there are no elements with indices greater than 6.

P = 7 is not an equilibrium index, because it does not fulfill the condition 0 ≤ P < N.

Write a function

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

int solution(NSMutableArray *A);

int solution(const vector<int> &A);

class Solution { int solution(int[] A); }

class Solution { public int solution(int[] A); }

object Solution { def solution(A: Array[Int]): Int }

function solution(A);

function solution(A)

function solution($A);

function solution(A: array of longint; N: longint): longint;

def solution(A)

sub solution { my (@A)=@_; ... }

def solution(a)

Private Function solution ( A As Integer() ) as Integer

that, given a zero-indexed array A consisting of N integers, returns any of its equilibrium indices. The function should return −1 if no equilibrium index exists.

Assume that:

  • N is an integer within the range [0..10,000,000];
  • each element of array A is an integer within the range [−2,147,483,648..2,147,483,647].

For example, given array A such that

A[0] = -7   A[1] =  1   A[2] = 5
A[3] = 2 A[4] = -4 A[5] = 3
A[6] = 0

the function may return 3 or 6, as explained above.

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.

At very beginning, I have wrotten down this:

// 62 of 100
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
class Solution {
public int solution(int[] A) {
if(A.Length <=1)
return -1;
//abc
int sumA = 0;
foreach(int i in A){
sumA += i;
} int j = 0;
int tempSum = A[0];
for(j = 1; j<A.Length; j++){ //A[0] + A[1] + ... + A[P−1] = A[P+1] + ... + A[N−2] + A[N−1].
//return P
if(sumA - tempSum - A[j] == tempSum){
break;
}
tempSum += A[j];
}
if (j == A.Length || j == A.Length - 1)
return -1;
else
return j;
}
}

I got 62 points of 100. because it missed border check and one misunderstanding.

So I updated it to this one:

//100 of 100
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
class Solution {
public int solution(int[] A) {
//N is an integer within the range [0..10,000,000];
if(A.Length <1 || A.Length >10000000)
return -1;
//single number
if(A.Length == 1)
return 0; //each element of array A is an integer within the range [−2,147,483,648..2,147,483,647]
Int64 sumA = 0;
foreach(int i in A){
sumA += i;
} int j = 0; //each element of array A is an integer within the range [−2,147,483,648..2,147,483,647]
Int64 tempSum = 0;
for(; j<A.Length; j++){
//A[0] + A[1] + ... + A[P−1] = A[P+1] + ... + A[N−2] + A[N−1].
//return P
if(sumA - tempSum - A[j] == tempSum){
break;
}
tempSum += A[j];
}
//P = 7 is not an equilibrium index, because it does not fulfill the condition 0 ≤ P < N
if (j == A.Length)
return -1;
else
return j;
}
}

Now, I got 100 points.

This task is from a test website.

Find the equipment indices的更多相关文章

  1. elasticsearch auto delete old indices

    定在crontab 每天执行 crontab -e * 2 * * * ~/autodelete.py Python 代码如下 #!/usr/bin/env python # encoding:utf ...

  2. Equipment Box[HDU1110]

    Equipment Box Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  3. 转载:监控每个节点(Indices部分)

    集群的健康只是一个方面,它是对整个集群所有方面的一个很高的概括.节点状态的api是另外一个方面,它提供了关于你的集群中每个节点令你眼花缭乱的统计数据. 节点的状态提供了那么多的统计数据,在你很熟悉它们 ...

  4. HDOJ 3177 Crixalis&#39;s Equipment

    Crixalis's Equipment Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  5. How do I list all tables/indices contained in an SQLite database

    How do I list all tables/indices contained in an SQLite database If you are running the sqlite3 comm ...

  6. HDU ACM 3177 Crixalis's Equipment

    Crixalis's Equipment Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  7. 杭电 3177 Crixalis&#39;s Equipment

    http://acm.hdu.edu.cn/showproblem.php? pid=3177 Crixalis's Equipment Time Limit: 2000/1000 MS (Java/ ...

  8. Hdu 3177 Crixalis's Equipment

    Crixalis's Equipment Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  9. /Home/Tpl/Equipment/rangeIndex.html 里调用魔板

    <pre name="code" class="html">demo:/var/www/DEVOPS# vim ./Home/Tpl/Equipme ...

随机推荐

  1. 【 D3.js 入门系列 --- 2 】 如何使用数据和选择元素

    接着上一讲的内容,这次讨论如何选择元素和使用数据.    现在页面中有三行文字,代码为: <p>Hello World 1</p> <p>Hello World 2 ...

  2. bootstrap-11

    下拉菜单(基本用法) 在使用Bootstrap框架的下拉菜单时,必须调用Bootstrap框架提供的bootstrap.js文件.当然,如果你使用的是未编译版本,在js文件夹下你能找到一个名为“dro ...

  3. UDP信息接收与发送

    转载:http://www.cnblogs.com/sunev/archive/2012/08/08/2627247.html 一.摘要 总结基于C#的UDP协议的同步通信. 二.实验平台 Visua ...

  4. sql语句与数据库2

    增加数据 insert into wyx(xh,nl,xb,sfzh,zcrq)values(0422,28,男,210623198711111111,2016-8-19); 删除数据 delete ...

  5. MySQL安装及主从配置

    系统环境:CentOS release 6.5 (Final)(最小化安装) MySQL版本:mysql-5.6.12 Cmake版本:cmake-2.8.4 说明:安装mysql先安装cmake(原 ...

  6. 体验一下cygwin

    一直在windows下使用gvim,为了更方便的使用cscope.先是写了bat脚本,发现太不方便了. 于是想到了cygwin.下载安装工具就可以了进行默认安装了,配置比较简单. 调整包: ./set ...

  7. kuangbin_SegTree A (HDU 1166)

    大牛们的文章里这句 题意:O(-1) 思路:O(-1) 深深地嘲讽了我........ 不过单点更新 区间求和也算是基本操作了吧 (虽然我还是看了好久才理解) 跟之前学图论的时候感觉完全不一样啊orz ...

  8. 源码分析:Java对象的内存分配

    Java对象的分配,根据其过程,将其分为快速分配和慢速分配两种形式,其中快速分配使用无锁的指针碰撞技术在新生代的Eden区上进行分配,而慢速分配根据堆的实现方式.GC的实现方式.代的实现方式不同而具有 ...

  9. F1 分数

    F1 分数会同时考虑精确率和召回率,以便计算新的分数. 可将 F1 分数理解为精确率和召回率的加权平均值,其中 F1 分数的最佳值为 1.最差值为 0: F1 = 2 * (精确率 * 召回率) / ...

  10. 全面理解Javascript中Function对象的属性和方法

    http://www.cnblogs.com/liontone/p/3970420.html 函数是 JavaScript 中的基本数据类型,在函数这个对象上定义了一些属性和方法,下面我们逐一来介绍这 ...