zoukankan      html  css  js  c++  java
  • M面经Prepare: Positive-Negative partitioning preserving order

    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 heta(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 heta(|A2|) time; compute the inverse of B1 (i.e., B1’) in heta(|B1|) time. [See observation; the total time is heta(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 heta(|A2|) time. [See observation; the total time is heta(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) + heta(n) = O(nlogn)

     1 package ArrayMergeSort;
     2 import java.util.*;
     3 
     4 public class Solution3 {
     5     public void rearrange(int[] arr) {
     6         if (arr==null || arr.length==0) return;
     7         rearrange(arr, 0, arr.length-1);
     8     }
     9     
    10     public void rearrange(int[] arr, int l, int r) {
    11         if (l == r) return;
    12         int m = (l+r)/2;
    13         rearrange(arr, l, m);
    14         rearrange(arr, m+1, r);
    15         merge(arr, l, m+1, r);
    16     }
    17     
    18     public void merge(int[] arr, int s1, int s2, int e) {
    19         int findPos1 = s1, findPos2 = s2;
    20         while (findPos1<s2 && arr[findPos1] < 0) findPos1++;
    21         while (findPos2<=e && arr[findPos2] < 0) findPos2++;
    22         reverse(arr, findPos1, s2-1);
    23         reverse(arr, s2, findPos2-1);
    24         reverse(arr, findPos1, findPos2-1);
    25     }
    26     
    27     public void reverse(int[] arr, int start, int end) {
    28         while (start < end) {
    29             int temp = arr[start];
    30             arr[start] = arr[end];
    31             arr[end] = temp;
    32             start++;
    33             end--;
    34         }
    35     }
    36     
    37     /**
    38      * @param args
    39      */
    40     public static void main(String[] args) {
    41         // TODO Auto-generated method stub
    42         Solution3 sol = new Solution3();
    43         int[] arr = new int[]{-1, 2, -2, 3, 5, -4};
    44         sol.rearrange(arr);
    45         System.out.println(Arrays.toString(arr));
    46 
    47     }
    48 
    49 }
  • 相关阅读:
    Flush输出表格内容
    [From Linux Toy] inxi– bash script to get system information
    .NET中Mocking框架的对比
    Python中的对象和动态性 [菜鸟的理解,高手莫入]
    《正见 佛陀的证悟》读书摘记 (1)
    Quote for tomorrow
    不抱怨就会死
    web deploy + windows server 2008 r2 64bit + iis 7.5
    bootsnipp
    [转载] A Beginner's Guide to HTTP Cache Headers
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/5191636.html
Copyright © 2011-2022 走看看