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 ...
随机推荐
- position置顶或某固定位置 兼容ie6ie7
用absolute来模拟fixed效果: /*相当于正常的position:fixed;top:0 */.sl_fixed_top{bottom:auto;top:0;_bottom:auto;_to ...
- python ftplib.FTP 获取当前路径下所有目录
FTP 模块里有一个dir函数,可以打印出当前路径下所有文件,但是这个函数没有返回值,只是打印出来. 还有一个nlst函数,可以返回一个文件名的列表,但是只有文件名,没有详细信息,无法判断是否是目录. ...
- MSP430精准配置高速串口波特率的方法
引言 在实际项目大批量生产调试设备时,笔者发现同样版本的程序在不同设备上运行时效果不一致,一部分设备串口通信正常,另外一部分串口通信不正常.通过示波器对多个设备的串口波特率及系统时钟频率测试, ...
- Oracle学习笔记(一)
1.常用sqlplus命令 (1)查看当前登录用户:show user; (2)切换当前登录用户:conn 用户名/密码 (切换系统用户+as sysdba ); (3)断开当前登录用户:disc; ...
- 一加3,CM13蓝牙共享互联网 无效。
一加3准备把4G网络共享给魅族PRO5. 但在一加3的蓝牙设置里怎么勾选都无用. 最后发现在要在PRO5上设置才行. 1.在蓝牙列表中,点击带圈的感叹号. 2.选择“互联网访问”. - --
- HackerRank "Prim's (MST) : Special Subtree"
An intuitive Prim algorithm impl. #include <vector> #include <iostream> #include <que ...
- jQuery使用load方法加载其他文档内容
A文档载入B文档的内容,并且通过JQ操作被引入到A文档中的元素 A文档 (index.html): <!DOCTYPE html> <html lang="en" ...
- 正常月报表年初未分配利润修改backup
原来:GLQC('3132',K('年')-1,'12',,,'本币','N','','本币','0001')+GLQC('314115',K('年')-1,'01',,,'本币','N','','本 ...
- SQL Server 2008 数据库镜像部署实例之二 配置镜像,实施手动故障转移
SQL Server 2008 数据库镜像部署实例之二 配置镜像,实施手动故障转移 上一篇文章已经为配置镜像数据库做好了准备,接下来就要进入真正的配置阶段 一.在镜像数据库服务器上设置安全性并启动数据 ...
- Bootstrap整体架构
大多数Bootstrap的使用者都认为Bootstrap只是提供了CSS组件和JavaScript插件,其实CSS组件和JavaScript插件只是Bootstrap框架的表现形式而已,他们都是构建在 ...