Find the equipment indices
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的更多相关文章
- elasticsearch auto delete old indices
定在crontab 每天执行 crontab -e * 2 * * * ~/autodelete.py Python 代码如下 #!/usr/bin/env python # encoding:utf ...
- Equipment Box[HDU1110]
Equipment Box Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- 转载:监控每个节点(Indices部分)
集群的健康只是一个方面,它是对整个集群所有方面的一个很高的概括.节点状态的api是另外一个方面,它提供了关于你的集群中每个节点令你眼花缭乱的统计数据. 节点的状态提供了那么多的统计数据,在你很熟悉它们 ...
- HDOJ 3177 Crixalis's Equipment
Crixalis's Equipment Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- 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 ...
- HDU ACM 3177 Crixalis's Equipment
Crixalis's Equipment Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- 杭电 3177 Crixalis's Equipment
http://acm.hdu.edu.cn/showproblem.php? pid=3177 Crixalis's Equipment Time Limit: 2000/1000 MS (Java/ ...
- Hdu 3177 Crixalis's Equipment
Crixalis's Equipment Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- /Home/Tpl/Equipment/rangeIndex.html 里调用魔板
<pre name="code" class="html">demo:/var/www/DEVOPS# vim ./Home/Tpl/Equipme ...
随机推荐
- Sqlite注入测试
测试了一个网站是Sqlite数据库,还装有安全狗,绕过了防护,找到Payload,写了一个Python脚本来跑表,这里总结一下: 取得sqlite数据库里所有的表名 查询table,type 段是't ...
- LA 3027 合作网络 并查集
题目链接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...
- 在linux和windows下自动备份数据库
摘要: 详细介绍在windows和linux下自动备份数据库的过程,希望可以让新手立即上手吧! 本文档内容共分为2大部分:linux和windows Linux和windows都分为:准备工作和操作阶 ...
- 《C与指针》第二章练习
本章问题 1.Comments in C do not nest(嵌套).What would be the result of "commenting out" the code ...
- Appium使用PageFactory初始化对象时报空指针错误
自己的测试框架里面,每个app页面都要初始化appium field,所以想到使用一个静态的变量,后来初始化一个页面对象时总是报空指针. 在网上找了好多材料,看着没有什么区别.后来在github上面看 ...
- DB_MYSQL_mysql-5.7.10-winx64解压版安装笔记
1.http://dev.mysql.com/downloads/mysql/ 里面下载Windows (x86, 64-bit), ZIP Archive mysql-5.7.10-winx64.z ...
- X5的UI部分和传统Web页面开发的差异
http://doc.wex5.com/different-with-std-web-ui/#1 X5的UI部分和传统Web页面开发的差异 WeX5是跨端移动开发框架,BeX5是基于WeX5的企业快速 ...
- Spring-RMI固定端口
Spring-RMI固定端口 最近接到一个需求项目所应用的RMI端口(数据传输端口)为随机指定的,项目要求对其端口固定,费劲周折找了很多资料,最后解决了问题. 其实解决问题的方法及其简单,只需要在 ...
- Python-Tkinter几何布局管理(转)
所有的Tkinter组件都包含专用的几何管理方法,这些方法是用来组织和管理整个父配件区中子配件的布局的.Tkinter提供了截然不同的三种几何管理类:pack.grid和place. pack() p ...
- 1.date 命令
转自:http://www.cnblogs.com/peida/archive/2012/12/13/2815687.html 在linux环境中,不管是编程还是其他维护,时间是必不可少的,也经常会用 ...