1. 数组之差TapeEquilibrium Minimize the value |(A[0] + ... + A[P-1]) - (A[P] + ... + A[N-1])|.
数组之差
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])|.的更多相关文章
- php 算法之切割数组,不用array_chunk(),算法之二,取数组的差值,不用array_diff()
用php写算法切割数组,不用array_chunk();算法例如以下所看到的. <?php //$array 数组 //$size 每一个数组的个数 //每一个数组元素是否默认键值 functi ...
- 输入一个字符串,内有数字和非数字字符。例如:a123x456 17960 302tab5876。将其中连续的数字作为一个整数,依次存放到一维数组a中,例如123放在a[0],456放在a[1]……统计共有多少个整数,并输出这些数。
题目内容:输入一个字符串,内有数字和非数字字符.例如:a123x456 17960 302tab5876.将其中连续的数字作为一个整数,依次存放到一维数组a中,例如123放在a[0],456放在a[1 ...
- [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 ...
- 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 ...
- 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, ...
- 532 K-diff Pairs in an Array 数组中差为K的数对
详见:https://leetcode.com/problems/k-diff-pairs-in-an-array/description/ C++: class Solution { public: ...
- 第九课,T语言数组的定义与访问(版本5.0)
数组的定义与访问 数组是一系列数据的集合,可以存储大量数据,通过数组的下标.key,可以实现对数据的快速访问. 为什么要使用数组呢? 如果您有一个项目列表(例如汽车品牌列表),在单个变量中存储这些品牌 ...
- 算法题——给定一个数组 arr,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。
参考自:https://blog.csdn.net/qq_38200548/article/details/80688630 示例: 输入: [0,1,0,3,12] 输出: [1,3,12,0,0] ...
- 偶然在博客中见对百度一个面试题的探讨,写些自己的看法以及指出探讨中不对的观点:百度面试题:求绝对值最小的数 有一个已经排序的数组(升序),数组中可能有正数、负数或0,求数组中元素的绝对值最小的数,要求,不能用顺序比较的方法(复杂度需要小于O(n)),可以使用任何语言实现 例如,数组{-20,-13,-4, 6, 77,200} ,绝对值最小的是-4。
今天申请了博客园账号,在下班后阅览博客时发现了一个关于百度面试题探讨的博客(其实是个很基础的问题),此博客url为:http://www.blogjava.net/nokiaguy/archive/2 ...
随机推荐
- EasyUI系列学习(十)-Tabs(选项卡)
一.创建组件 <div class="easyui-tabs" style="width:500px;height:250px"> <div ...
- P1334 瑞瑞的木板
题目描述 瑞瑞想要亲自修复在他的一个小牧场周围的围栏.他测量栅栏并发现他需要N(1≤N≤20,000)根木板,每根的长度为整数Li(1≤Li≤50,000).于是,他神奇地买了一根足够长的木板,长度为 ...
- 60使用nanopim1plus查看HDMI显示分辨率的问题(分色排版)V1.0
60使用nanopim1plus查看HDMI显示分辨率的问题(分色排版)V1.0 大文实验室/大文哥 壹捌陆捌零陆捌捌陆捌贰 21504965 AT qq.com 完成时间:2017/12/5 17: ...
- scroll的应用
jQuery(document).ready(function($){ $('#shang').click(function(){ $('html,body').animate({scrollTop: ...
- 伪装IP进行投票
伪装IP投票说明 1,目的 在访问网页链接进行投票时,网站往往对同一个IP的投票次数进行了限制,无法连续重复投票.为此可以使用“火狐浏览器+IP修改插件”,通过人为设置浏览器IP,绕过网站IP检查,可 ...
- 一个完整的网站记录(springmvc hibernate juery bootstrap)
总述 该网站为了满足测试人员自主添加测试条目,编辑更新信息和删除信息,同时同步到后台数据库的基本功能. 关键技术:oracle数据库.tomcat8.5.springMVC.Hibernate.aja ...
- (转)WKT转换工具terraformers
http://blog.csdn.net/gisshixisheng/article/details/53150111 概述: 前面的文章中,提到了Arcgis中实现wkt转换为geometry,但是 ...
- 安卓app测试之流量监控
一.查看PID 通过ps命令查看:ps | grep packageName 案例:adb shell "ps | grep tv.danmaku.bili" adb shell ...
- 梦想CAD控件com接口扩展数据
随着CAD应用软件的飞速发展,经常需要保存一些与图形可视性无关的数据,即非图形参数.例如在绘制化验样图中包含品位数据.MxCAD定义了一类参数——实体扩展数据.扩展数据与实体的可视性无关,而是用户根据 ...
- 使用Java中Calendar类测试当前年月日
import java.util.Calendar; public class time { public static void main(String[] args) { Calendar cal ...