数组之差

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. [ Nowcoder Contest 167 #D ] 重蹈覆辙

    \(\\\) \(Description\) 用\(1\times 2\)的矩形和面积为\(3\)的\(L\)形去覆盖一个\(2\times N\) 的矩形,求方案数对\(10^4+7\)取模后的结果 ...

  2. redis学习-sds数据类型

    今天开始了redis的学习,本来想直接从源码看起的,不过看到有篇对redis介绍的基础教程 <Redis 设计与实现(第一版)> 于是决定从这个开始入门. 1.数据类型定义 typedef ...

  3. JS求斐波那契数列的N项

    第一种求法: <!DOCTYPE html><html lang="en"><head> <meta charset="UTF- ...

  4. git reset作用

    git reset: 1. 文件从暂存区回退到工作区,撤销add 2. 版本回退  一:文件从暂存区回退到工作区,撤销add 如果想取消某个add的文件,可以使用该命令来进行撤销操作 撤消add:gi ...

  5. Farseer.net轻量级开源框架 中级篇:数据库切换

    导航 目   录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 中级篇: 动态数据库访问 下一篇:Farseer.net轻量级开源框架 中级篇: SQL执行 ...

  6. 安装mask-rcnn问题汇总

    1. I download file from https://github.com/waleedka/coco. Then I placed the file in Mask_RCNN-master ...

  7. .net core Elasticsearch 查询更新

    记录一下: 数据结构如下: public class ESUserTransaction { public long AccountId { get; set; } public string Var ...

  8. java内存组成

     java内存组成介绍:堆(Heap)和非堆(Non-heap)内存 按照官方的说法:“Java 虚拟机具有一个堆,堆是运行时数据区域,所有类实例和数组的内存均从此处分配.堆是在 Java 虚拟机启动 ...

  9. C++调用Com

    需求:1.创建myCom.dll,该COM只有一个组件,两个接口:   IGetRes--方法Hello(),   IGetResEx--方法HelloEx() 2.在工程中导入组件或类型库  #im ...

  10. CAD插入图片

    在CAD设计绘图时,需要插入外部图片,可以设置图片的缩放比例.旋转角度.图片显示文件名等属性. 主要用到函数说明: _DMxDrawX::DrawImageMark 绘图制一个图象标记对象.详细说明如 ...