Skip to content

Commit 220c737

Browse files
author
Hieu Luong
committed
Solve Problem 324 - WiggleSort II
1 parent 98e7b4f commit 220c737

File tree

7 files changed

+130
-5
lines changed

7 files changed

+130
-5
lines changed

src/Main.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import bit.Problem_BitWiseANDofNumbersRange;
1+
import quickselect.Problem324_WiggleSortII;
22

33
public class Main {
44

55
public static void main(String[] args) {
6-
Problem_BitWiseANDofNumbersRange.run();
6+
Problem324_WiggleSortII.run();
77
}
88
}

src/backtrack/Problem1307_VerbalArithmeticPuzzle.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public boolean isSolvable(String[] words, String result) {
2424
if (m == 0 && n == 0) return true;
2525
if (m*n == 0) return false;
2626
27-
Arrays.fill(memo, -1);
27+
Array.fill(memo, -1);
2828
return backtrack(words, result, 0, 0, n-1, 0);
2929
}
3030

src/binarysearch/ReadMe.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public class ReadMe {
5151
*
5252
*
5353
* Collections.binarySearch
54-
* Arrays.binarySearch
54+
* Array.binarySearch
5555
* for binarySearch, the result can be negative. If key is not present, then it returns "(-(insertion point) - 1)".
5656
* The insertion point is defined as the point at which the key would be inserted into the list.
5757
*

src/dp/decision_making/Problem198_HouseRobber.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public int rob(int[] nums) {
3131
// return rob_top_down(nums, n-1);
3232

3333
// int[] memo = new int[n];
34-
// Arrays.fill(memo, -1);
34+
// Array.fill(memo, -1);
3535
// return rob_top_down_memo(nums, n-1, memo);
3636

3737
return rob_iterative_bottom_up_dp(nums);

src/lib/Array.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package lib;
2+
3+
import java.util.Random;
4+
5+
public class Array {
6+
7+
public static void swap(int[] A, int i, int j) {
8+
if (i == j) return;
9+
int tmp = A[i];
10+
A[i] = A[j];
11+
A[j] = tmp;
12+
}
13+
14+
public static void shuffle(int[] A) {
15+
Random random = new Random();
16+
// Attention: Reverse loop
17+
for (int i = A.length-1; i >= 0; i--) {
18+
swap(A, i, random.nextInt(i+1));
19+
}
20+
}
21+
22+
public static int findKthLargest(int[] A, int k) {
23+
int lo = 0, hi = A.length-1; k = A.length-k;
24+
// without suffle: O(N) best case / O(N^2) worst case running time + O(1) memory
25+
// with suffle: the probability O(N) is rare (1/N!), O(1) memory
26+
shuffle(A);
27+
while (true) {
28+
int pivot = quickselect(A, lo, hi);
29+
if (pivot == k) break;
30+
if (pivot < k) lo = pivot+1;
31+
else hi = pivot-1;
32+
}
33+
return A[k];
34+
}
35+
36+
public static int quickselect(int[] A, int lo, int hi) {
37+
int pivot = lo; // choose the pivot at lo
38+
while (true) {
39+
while (lo <= hi && A[lo] <= A[pivot]) lo++;
40+
while (lo <= hi && A[hi] > A[pivot]) hi--;
41+
if (lo > hi) break;
42+
swap(A, lo, hi);
43+
}
44+
swap(A, hi, pivot);
45+
return hi;
46+
}
47+
48+
public static void dutchNationalFlag(int[] A, int mid) {
49+
int i = 0, j = 0, k = A.length;
50+
while (j < k) {
51+
if (A[j] < mid) swap(A, i++, j++);
52+
else if (A[j] > mid) swap(A, j, --k);
53+
else j++;
54+
}
55+
}
56+
}

src/lib/Printer.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package lib;
2+
3+
public class Printer {
4+
5+
public static void print(int[] A) {
6+
print(A, " ");
7+
}
8+
9+
public static void print(int[] A, String separate) {
10+
for (int a : A)
11+
print(a, separate);
12+
}
13+
14+
public static void print(int a) {
15+
print(a, "");
16+
}
17+
18+
public static void print(int a, String s) {
19+
System.out.print(a + s);
20+
}
21+
22+
public static void newLine() {
23+
System.out.println();
24+
}
25+
26+
public static void println(int a) {
27+
println(a, "");
28+
}
29+
30+
public static void println(int a, String s) {
31+
System.out.println(a + s);
32+
}
33+
34+
public static void println(int[] A) {
35+
print(A);
36+
newLine();
37+
}
38+
39+
public static void println(int[] A, String separate) {
40+
for (int a : A)
41+
print(a, separate);
42+
newLine();
43+
}
44+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package quickselect;
2+
3+
import lib.Array;
4+
import lib.Printer;
5+
6+
import java.util.Arrays;
7+
8+
public class Problem324_WiggleSortII {
9+
10+
public void wiggleSort(int[] A) {
11+
int n = A.length, m = (n+1) >> 1;
12+
int[] copy = Arrays.copyOf(A, n);
13+
int mid = Array.findKthLargest(copy, m);
14+
Array.dutchNationalFlag(copy, mid);
15+
for (int i = m - 1, j = 0; i >= 0; i--, j += 2) A[j] = copy[i];
16+
for (int i = n - 1, j = 1; i >= m; i--, j += 2) A[j] = copy[i];
17+
}
18+
19+
public static void run() {
20+
Problem324_WiggleSortII solution = new Problem324_WiggleSortII();
21+
int[] A = {2,3,3,2,2,2,1,1};
22+
solution.wiggleSort(A);
23+
Printer.print(A);
24+
}
25+
}

0 commit comments

Comments
 (0)