数组之差

package com.code;

public class Test03_3 {
public static int solution(int[] A) {
int size = A.length;
if (size<2){
return -1;
}
int [] rightSum = new int[size];
rightSum[size-1] = A[size-1];
for(int i=size-2;i>=0;i--){
rightSum[i] = A[i]+rightSum[i+1];
}
int [] leftSum = new int[size];
leftSum[0] = A[0];
for(int i=1;i<size-1;i++){
leftSum[i] = A[i]+leftSum[i-1];
}
int min = 2147483647;
for(int i=0;i<size-1;i++){
min = Math.min(min, Math.abs(leftSum[i]-rightSum[i+1]));
}
return min;
}
public static void main(String[] args) {
int a [] = {3,1,2,4,3};
System.out.println(solution(a));
int b[] = {1,3};
System.out.println(solution(b));
}
} /** 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: class Solution { public int solution(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.
*/

1. 数组之差TapeEquilibrium Minimize the value |(A[0] + ... + A[P-1]) - (A[P] + ... + A[N-1])|.的更多相关文章

  1. php 算法之切割数组,不用array_chunk(),算法之二,取数组的差值,不用array_diff()

    用php写算法切割数组,不用array_chunk();算法例如以下所看到的. <?php //$array 数组 //$size 每一个数组的个数 //每一个数组元素是否默认键值 functi ...

  2. 输入一个字符串,内有数字和非数字字符。例如:a123x456 17960 302tab5876。将其中连续的数字作为一个整数,依次存放到一维数组a中,例如123放在a[0],456放在a[1]……统计共有多少个整数,并输出这些数。

    题目内容:输入一个字符串,内有数字和非数字字符.例如:a123x456 17960 302tab5876.将其中连续的数字作为一个整数,依次存放到一维数组a中,例如123放在a[0],456放在a[1 ...

  3. [LeetCode] K-diff Pairs in an Array 数组中差为K的数对

    Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in t ...

  4. 220. Contains Duplicate III 数组指针差k数值差t

    [抄题]: Given an array of integers, find out whether there are two distinct indices i and j in the arr ...

  5. Linq 数据操作,两个数组求差、交集、并集

    int[] a = { 1, 2, 3, 4, 5, 6, 7 }; int[] b = { 4, 5, 6, 7, 8, 9, 10 }; int[] c = { 1, 2, 3, 3, 4, 1, ...

  6. 532 K-diff Pairs in an Array 数组中差为K的数对

    详见:https://leetcode.com/problems/k-diff-pairs-in-an-array/description/ C++: class Solution { public: ...

  7. 第九课,T语言数组的定义与访问(版本5.0)

    数组的定义与访问 数组是一系列数据的集合,可以存储大量数据,通过数组的下标.key,可以实现对数据的快速访问. 为什么要使用数组呢? 如果您有一个项目列表(例如汽车品牌列表),在单个变量中存储这些品牌 ...

  8. 算法题——给定一个数组 arr,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。

    参考自:https://blog.csdn.net/qq_38200548/article/details/80688630 示例: 输入: [0,1,0,3,12] 输出: [1,3,12,0,0] ...

  9. 偶然在博客中见对百度一个面试题的探讨,写些自己的看法以及指出探讨中不对的观点:百度面试题:求绝对值最小的数 有一个已经排序的数组(升序),数组中可能有正数、负数或0,求数组中元素的绝对值最小的数,要求,不能用顺序比较的方法(复杂度需要小于O(n)),可以使用任何语言实现 例如,数组{-20,-13,-4, 6, 77,200} ,绝对值最小的是-4。

    今天申请了博客园账号,在下班后阅览博客时发现了一个关于百度面试题探讨的博客(其实是个很基础的问题),此博客url为:http://www.blogjava.net/nokiaguy/archive/2 ...

随机推荐

  1. NPOI 导出Excel 2007, 2013问题

    NPOI默认有两个命名空间HSSF为Excel 2003 版本,若导出2007 及以上后缀名打开excel 则会报错,NPOI也提供了一个07及以上的版本空间XSSF,具体操作列下: NPOI.XSS ...

  2. git初使用的心得

    转到Java方向后,版本控制工具也开始以git为主了.由于之前不怎么使用bash,所以目前还是以ui工具,比如sourcetree为主导,但一些简单的操作命令,已经能够快速地使用.sourcetree ...

  3. js文件中引用其他js文件

    这一个功能的作用是做自己的js包时,可以通过引入一个整体的js文件而引入其他js. 只需要在总体的js加上这一句话 document.write("<script type='text ...

  4. Ps 快捷键全解

    一.工具箱(多种工具共用一个快捷键的可同时按[Shift]加此快捷键选取)矩形.椭圆选框工具 [M]移动工具 [V]套索.多边形套索.磁性套索 [L]魔棒工具 [W]裁剪工具 [C]切片工具.切片选择 ...

  5. 史上最大型广告欺诈活动Methbot:黑客是如何每天赚到500万美元的

    根据国外安全专家的最新报告,有一群黑客正在对美国的知名企业和媒体机构进行广告欺诈活动,而这群黑客每天都可以从中赚取三百万到五百万美金. 是的,你没看错,这绝对是人类历史上最牛X的恶意广告欺诈活动!不过 ...

  6. Windows开启ICMP包回显

  7. list.sort结果是None

    错误原因:  list.sort()功能是针对列表自己内部进行排序, 不会有返回值, 因此返回为None.  举例说明: In [19]: a=["a","c" ...

  8. CAD得到当前选择的实体(网页版)

    主要用到函数说明: IMxDrawSelectionSet::CurrentSelect 得到当前已经选择的实体.详细说明如下: 参数 说明 filterData 过滤条件 js中使用如下: 1 2 ...

  9. Linux常用命令——关机与重启命令

    1.shutdown命令 shutdown [选项] 时间 --使用shutdown进行关机或重启会正确保存正在使用的服务,其他命令有一定的危险性,建议最好使用shutdown命令进行关机重启 选项: ...

  10. 使用TransactionTemplate

    通过TransactionCallback接口中的方法后(这里用来做业务),将返回值传递到TransactionTemplate的execute()中.通过调用TransactionStatus 的s ...