数组之差

  1. package com.code;
  2.  
  3. public class Test03_3 {
  4. public static int solution(int[] A) {
  5. int size = A.length;
  6. if (size<2){
  7. return -1;
  8. }
  9. int [] rightSum = new int[size];
  10. rightSum[size-1] = A[size-1];
  11. for(int i=size-2;i>=0;i--){
  12. rightSum[i] = A[i]+rightSum[i+1];
  13. }
  14. int [] leftSum = new int[size];
  15. leftSum[0] = A[0];
  16. for(int i=1;i<size-1;i++){
  17. leftSum[i] = A[i]+leftSum[i-1];
  18. }
  19. int min = 2147483647;
  20. for(int i=0;i<size-1;i++){
  21. min = Math.min(min, Math.abs(leftSum[i]-rightSum[i+1]));
  22. }
  23. return min;
  24. }
  25. public static void main(String[] args) {
  26. int a [] = {3,1,2,4,3};
  27. System.out.println(solution(a));
  28. int b[] = {1,3};
  29. System.out.println(solution(b));
  30. }
  31. }
  32.  
  33. /**
  34.  
  35. A non-empty zero-indexed array A consisting of N integers is given. Array A represents numbers on a tape.
  36.  
  37. 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].
  38.  
  39. 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])|
  40.  
  41. In other words, it is the absolute difference between the sum of the first part and the sum of the second part.
  42.  
  43. For example, consider array A such that:
  44.  
  45. A[0] = 3
  46. A[1] = 1
  47. A[2] = 2
  48. A[3] = 4
  49. A[4] = 3
  50. We can split this tape in four places:
  51.  
  52. P = 1, difference = |3 − 10| = 7
  53. P = 2, difference = |4 − 9| = 5
  54. P = 3, difference = |6 − 7| = 1
  55. P = 4, difference = |10 − 3| = 7
  56. Write a function:
  57.  
  58. class Solution { public int solution(int[] A); }
  59.  
  60. that, given a non-empty zero-indexed array A of N integers, returns the minimal difference that can be achieved.
  61.  
  62. For example, given:
  63.  
  64. A[0] = 3
  65. A[1] = 1
  66. A[2] = 2
  67. A[3] = 4
  68. A[4] = 3
  69. the function should return 1, as explained above.
  70.  
  71. Assume that:
  72.  
  73. N is an integer within the range [2..100,000];
  74. each element of array A is an integer within the range [−1,000..1,000].
  75. Complexity:
  76.  
  77. expected worst-case time complexity is O(N);
  78. expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments).
  79. Elements of input arrays can be modified.
  80. */

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. WebForm vs MVC

    What is ASP.NET? ASP.NET is a Microsoft’s Web application framework built on Common language runtime ...

  2. IntelliJ IDEA jrebel 实现热部署

    前提是能够访问登陆Facebook,有Facebook的账号(我注册了一个) 点击:https://my.jrebel.com 用Facebook登陆  之后按要求填写信息 然后获取激活码 打开 id ...

  3. [ POI 2010 ] Antisymmetry

    \(\\\) \(Description\) 给出一个长度为 \(N\) 的二进制串,定义一个子串是优秀的,当且仅当其正着看,和倒着按位取反后看结果是一样的,求整个串有多少个优秀的子串. \(N\le ...

  4. cocos2dx在windows下搭建环境android报错

    报错:Program bash is not found in PATH   (如果按照我的方法来的话是没有这个错误的,我之前用别的方法的时候有但是后来还是没解决,写出来放到这里做参考吧) 参考原文: ...

  5. Android 解决RecyclerView瀑布流效果结合Glide使用时图片变形的问题

    问题描述:使用Glide加载RecyclerView的Item中的图片,RecyclerView使用了瀑布流展示图片,但是滚动时图片会不断的加载,并且大小位置都会改变,造成显示错乱. 解决方法:使用瀑 ...

  6. 两年,VMware又重回巅峰?

    两年前,被公有云和容器打的焦头烂额的VMware一度被众多业界人士看衰,营收.股价双双下滑.然而,仅仅经过短短两年时间,VMware已经和AWS,IBM.微软.Rackspace等众多公有云厂商成为合 ...

  7. [Android]异常3-java.lang.NoClassDefFoundError: javax.activation.DataHandler

    背景:JavaMail发送电子邮件 异常原因: 可能一>缺少DataHandler类相关jar包 可能二>有DataHandler类,DataHandler类与使用的mail.jar包不一 ...

  8. 解决 HTTP Status 500 - Unable to show problem report: freemarker.core.InvalidReferenceException:

    HTTP Status 500 - Unable to show problem report: freemarker.core.InvalidReferenceException: The foll ...

  9. jmeter接口测试小结

    摘自:http://www.cnblogs.com/houzhizhe/p/6839736.html JMeter做http接口压力测试 测前准备 用JMeter做接口的压测非常方便,在压测之前我们需 ...

  10. MxCAD5.2 20180726更新

    下载地址: http://www.mxdraw.com/ndetail_105.html 1. 增加属性匹配功能 2. 增加List命令 3. 增加CAD图纸审图批注功能 4. 环形阵列功能