数组之差

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] = 3A[1] = 1A[2] = 2A[3] = 4A[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] = 3A[1] = 1A[2] = 2A[3] = 4A[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.
*/

转载于:https://www.cnblogs.com/stono/p/6419148.html

1. 数组之差TapeEquilibrium Minimize the value |(A[0] + ... + A[P-1]) - (A[P] + ... + A[N-1])|.相关推荐

  1. Leetcode1685. 有序数组中差绝对值之和[C++题解]:前缀和和差的绝对值之和

    文章目录 题目分析 题目链接 题目分析 分析: 货仓选址同类型题目. 差的绝对值求和,分两半,前面的都小于等于它,后面的都大于等于它. 另外需要注意 前缀和的技巧.O(1)时间求区间之和. 总的时间复 ...

  2. php求两个数组的差值,数组计算差值及项的小计,该如何处理

    数组计算差值及项的小计 本帖最后由 lazygc520 于 2014-04-14 16:19:41 编辑 $s = array ( 0 => array ( 0 => array ( 0  ...

  3. .NET(C#) Internals: 以一个数组填充的例子初步了解.NET 4.0中的并行(二)

    引言 随着CPU多核的普及,编程时充分利用这个特性越显重要.上篇首先用传统的嵌套循环进行数组填充,然后用.NET 4.0中的System.Threading.Tasks提供的Parallel Clas ...

  4. .NET(C#) Internals: 以一个数组填充的例子初步了解.NET 4.0中的并行(一)

    引言 随着CPU多核的普及,编程时充分利用这个特性越显重要.本文首先用传统的嵌套循环进行数组填充,然后用.NET 4.0中的System.Threading.Tasks提供的Parallel Clas ...

  5. 每日一题:给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。

    每日一题:给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序. 2020年11月19日,力扣,简单,移动零 一.题目描述 给定一个数组 nums,编写一个函数 ...

  6. C语言基础入门48篇_26_身份证号校验程序(以身份证的校验方式是实例加深对数组及函数封装的理解、字符-‘0‘得到字符对应的int类型数字)

    本篇根据以二代身份证的校验方式是实例加深对数组及函数封装的理解 1. 第二代身份证号的组成 第二代身份证号为18位,各位数字对应了不同的信息(以下顺序从左往右): AABBCCYYYYMMDDXXXV ...

  7. 2021-12-30:分裂问题。 一个数n,可以分裂成一个数组[n/2, n%2, n/2], 这个数组中哪个数不是1或者0,就继续分裂下去。 比如 n = 5,一开始分裂成[2, 1, 2], [2

    2021-12-30:分裂问题. 一个数n,可以分裂成一个数组[n/2, n%2, n/2], 这个数组中哪个数不是1或者0,就继续分裂下去. 比如 n = 5,一开始分裂成[2, 1, 2], [2 ...

  8. java数组中包含元素_Java中声明数组时,包括数组的名字、数组中包含的元素的 。 (5.0分)_学小易找答案...

    [简答题]3,单冲压片机的安装程序? [判断题]continue语句只用于循环语句中,它起到终止本次循环,返回到循环开始处的作用. (5.0分) [判断题]default在switch选择结构中是必需 ...

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

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

  10. 题8.16:输入一个字符串, 内有数字和非数字字符, 例如:A123x456 17960 ? , 302tab5876,将其中连续的数字作为一个整数, 依次存放到一数组a中。例如, 123 放在a[0

    题目 本题是谭浩强<C程序设计课后习题>题8.16. 题目: 16. 输入一个字符串, 内有数字和非数字字符, 例如:A123x456 17960 ? , 302tab5876,将其中连续 ...

最新文章

  1. 计算机视觉在生物力学和运动康复中的应用和研究
  2. MS:中山大学丁涛/吴忠道-肠道菌群调控血吸虫病传播媒介光滑双脐螺适生性的新机制...
  3. 资源 | 李沐等人开源中文书《动手学深度学习》预览版上线
  4. tomcat安全配置之禁用Directory Listing
  5. python做股票分析_利用Python进行股票投资组合分析(调试)
  6. c语言入门经典18个程序
  7. 【蓝桥杯嵌入式】【STM32】7_RTC之实时时间显示和硬件闹钟设置
  8. nodejs 前端 返回数组给_互联网寒冬,一年经验字节跳动、虾皮、快手、拼多多前端面试总结...
  9. maven项目转换为web项目
  10. Addrss already in user 解决方案 (linux)
  11. EZX交叉编译配置、MPKG程序制作教程
  12. JUnit 5- 概述
  13. ping通ipv6地址
  14. icmp协议用在什么服务器上,ICMP协议是什么?ICMP协议的作用是什么?
  15. Linux配置DNS域名解析服务
  16. Windows下的YouTube-dl与FFmpeg下载安装配置
  17. Maximum sum on a torus UVA - 10827
  18. 洛谷 P1217 [USACO1.5]回文质数 Prime Palindromes 题解 C/C++
  19. mysql统计每半小时内的数据(查寻某段时间内的数据)
  20. spa单页面开发的尝试

热门文章

  1. 王亚叶:洞悉客户心声用大数据运营
  2. Rust 的安装和使用举例
  3. Android:制作Update.zip升级包 【转】
  4. webapp开发技术选型
  5. SQL Server 2008支持将数据导出为脚本
  6. 基于地理区域的广告推送模块
  7. c语言主程序调用子程序数组,perl子程序返回多个数组到主程序中多个数组
  8. mysql数据库管理手册_CentOS MySQL 用户及数据库管理手册
  9. 科多大数据_redis缓存的简单使用—科多大数据
  10. Unity中 创建射线的两种方法