Given an array which has n integers,it has both positive and negative integers.Now you need sort this array in a special way.After that,the negative integers should in the front,and the positive integers should in the back.Also the relative position should not be changed.
eg. -1 1 3 -2 2 ans: -1 -2 1 3 2.

Solution

Time: O(NlogN), space: O(1)/O(logN) depends on iteration/recursion

This can be done in O(nlogn) using divide and conquer scheme. Before starting the algorithm, please see the following observation:

The basic idea of the algorithm is as follows:
1. We recursively ‘sort’ two smaller arrays of size n/2 (here ‘sort’ is defined in the question)
2. Then we spend \theta(n) time merging the two sorted smaller arrays with O(1) space complexity.
How to merge?
Suppose the two sorted smaller array is A and B. A1 denotes the negative part of A, and A2 denotes positive part of A. Similarly, B1 denotes the negative part of B, and B2 denotes positive part of B.
2.1. Compute the inverse of A2 (i.e., A2’) in \theta(|A2|) time; compute the inverse of B1 (i.e., B1’) in \theta(|B1|) time. [See observation; the total time is \theta(n) and space is O(1)]
Thus the array AB (i.e., A1A2B1B2) becomes A1A2’B1’B2.
2.2. Compute the inverse of A2’B1’ (i.e., B1A2) in \theta(|A2|) time. [See observation; the total time is \theta(n) and space is O(1)]
Thus the array A1A2’B1’B2 becomes A1B1A2B2. We are done.

Time complexity analysis:
T(n) = 2T(n/2) + \theta(n) = O(nlogn)

 package ArrayMergeSort;
import java.util.*; public class Solution3 {
public void rearrange(int[] arr) {
if (arr==null || arr.length==0) return;
rearrange(arr, 0, arr.length-1);
} public void rearrange(int[] arr, int l, int r) {
if (l == r) return;
int m = (l+r)/2;
rearrange(arr, l, m);
rearrange(arr, m+1, r);
merge(arr, l, m+1, r);
} public void merge(int[] arr, int s1, int s2, int e) {
int findPos1 = s1, findPos2 = s2;
while (findPos1<s2 && arr[findPos1] < 0) findPos1++;
while (findPos2<=e && arr[findPos2] < 0) findPos2++;
reverse(arr, findPos1, s2-1);
reverse(arr, s2, findPos2-1);
reverse(arr, findPos1, findPos2-1);
} public void reverse(int[] arr, int start, int end) {
while (start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Solution3 sol = new Solution3();
int[] arr = new int[]{-1, 2, -2, 3, 5, -4};
sol.rearrange(arr);
System.out.println(Arrays.toString(arr)); } }

M面经Prepare: Positive-Negative partitioning preserving order的更多相关文章

  1. light oj 1294 - Positive Negative Sign

    1294 - Positive Negative Sign   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: ...

  2. 1294 - Positive Negative Sign(规律)

    1294 - Positive Negative Sign   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: ...

  3. 阳/阴性预测值Positive/negative Predictive Value(推荐AA)

    sklearn实战-乳腺癌细胞数据挖掘(博主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005269003&am ...

  4. 智课雅思短语---二、exert positive/ negative effects on…

    智课雅思短语---二.exert positive/ negative effects on… 一.总结 一句话总结:对…产生有利/不利的影响 1.the advantages far outweig ...

  5. 零宽断言 -- Lookahead/Lookahead Positive/Negative

    http://www.vaikan.com/regular-expression-to-match-string-not-containing-a-word/ 经常我们会遇到想找出不包含某个字符串的文 ...

  6. LightOJ - 1294 - Positive Negative Sign(规律)

    链接: https://vjudge.net/problem/LightOJ-1294 题意: Given two integers: n and m and n is divisible by 2m ...

  7. Amazon评论数据的预处理代码(Positive & Negative)

    Amazon评论数据的预处理代码,用于情感分析,代码改自 https://github.com/PaddlePaddle/Paddle/tree/develop/demo/quick_start/da ...

  8. lightoj--1294--Positive Negative Sign(水题,规律)

    Positive Negative Sign Time Limit: 2000MS   Memory Limit: 32768KB   64bit IO Format: %lld & %llu ...

  9. Tensorflow二分类处理dense或者sparse(文本分类)的输入数据

    这里做了一些小的修改,感谢谷歌rd的帮助,使得能够统一处理dense的数据,或者类似文本分类这样sparse的输入数据.后续会做进一步学习优化,比如如何多线程处理. 具体如何处理sparse 主要是使 ...

随机推荐

  1. C#创建Excel

    创建Workbook说白了就是创建一个Excel文件,当然在NPOI中更准确的表示是在内存中创建一个Workbook对象流. 本节作为第2章的开篇章节,将做较为详细的讲解,以帮助NPOI的学习者更好的 ...

  2. NSQ部署

    一.      简介 NSQ主要有三个主要程序和一个Web服务程序: nsqd:是守护进程,接收,缓存,并投递消息给客户端 nsqlookupd:是一个守护进程,为消费者提供运行时发现服务,来查找指定 ...

  3. javaWeb中servlet开发(1)——helloworld

    1.servlet 1.1 servlet简介 1.2 servlet流程 不管是servlet还是jsp,所有的程序都是在服务器端处理的,所以必须了解一个servlet基本流程 servlet和JS ...

  4. 【转】【DP_树形DP专辑】【9月9最新更新】【from zeroclock's blog】

    树,一种十分优美的数据结构,因为它本身就具有的递归性,所以它和子树见能相互传递很多信息,还因为它作为被限制的图在上面可进行的操作更多,所以各种用于不同地方的树都出现了,二叉树.三叉树.静态搜索树.AV ...

  5. OpenMP并行编程

    什么是OpenMP?“OpenMP (Open Multi-Processing) is an application programming interface (API) that support ...

  6. Java ArrayListSerialise

    import java.io.*; import java.util.*; //ArrayListSerialise public class A { public static void main( ...

  7. Linux下编译静态MinGW环境,编译windows平台Qt程序(使用MXE)

    参考链接: MXE.>大多数程序都是在windows平台下开发的程序.windows 在现实中也是绕不过的一个系统平台,做为受过几年VC,MFC”虐待”的程序员,在做为一个程序员之前是一位Lin ...

  8. 通过自定义属性存储数据实现输入框获得焦点与失去焦点改变value值

    http://gopro.ee.cagoe.com/index.html     html: <div class="name"><input value=&qu ...

  9. charles工具的使用

    charles工具使用 charles除了之前介绍过模拟弱网的功能外,还有很多强大的功能.最近客户端测试用到的功能介绍如下: 一.准备工作 1.手机设置代理 charles设置代理端口号8888:Pr ...

  10. Object类型(对象)

    ECMAscript中的对象其实就是一组数据和功能集合.这里简单谈谈对象,复杂以后补充. 1 如何创建对象 简单创建: var box = {}; alert(box); //[object obje ...