From 30c43e820c3b3e49f42de584da2e32579143c57e Mon Sep 17 00:00:00 2001 From: "rica.rdo" Date: Wed, 9 Jun 2021 15:18:57 +0900 Subject: [PATCH 1/3] Add SliceSort template code and test --- .../com/company/problem/hard/SliceSort.java | 29 ++++++++++++ .../company/problem/hard/SliceSortTest.java | 46 +++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 src/main/java/com/company/problem/hard/SliceSort.java create mode 100644 src/test/java/com/company/problem/hard/SliceSortTest.java diff --git a/src/main/java/com/company/problem/hard/SliceSort.java b/src/main/java/com/company/problem/hard/SliceSort.java new file mode 100644 index 0000000..6bda23b --- /dev/null +++ b/src/main/java/com/company/problem/hard/SliceSort.java @@ -0,0 +1,29 @@ +package com.company.problem.hard; + +/** + * PROBLEM: + * + * We are given an array A consisting of N distinct integers. + * We would like to sort array A into ascending order using a simple algorithm. + * First, we divide it into one or more slices (a slice is a contiguous subarray). + * Then we sort each slice. After that, we join the sorted slices in the same order. + * Write a function solution that returns the maximum number of slices for which the algorithm will return a correctly sorted array. + * + * 1. Given A = [2,4,1,6,5,9,7] will return e. + * [2,4,1], [6,5], [9,7] then after soring each slice and joining them together, the whole array will be sorted into ascending order. + * + * A= [4,3,2,6,1] will return 1. the array cannot be split into smaller slices; it has to be sorted all at once. + * + * + * 3. A = [2,1,6,4,3,7] will return 3. + * + * N range [1..100,000] + * element range [1..1,000,000,000] + */ +public class SliceSort { + + public int sort(int[] array) { + int count = array.length; + return count; + } +} diff --git a/src/test/java/com/company/problem/hard/SliceSortTest.java b/src/test/java/com/company/problem/hard/SliceSortTest.java new file mode 100644 index 0000000..578dea6 --- /dev/null +++ b/src/test/java/com/company/problem/hard/SliceSortTest.java @@ -0,0 +1,46 @@ +package com.company.problem.hard; + +import org.junit.Test; +import org.junit.jupiter.api.Assertions; + +import static org.junit.jupiter.api.Assertions.*; + +public class SliceSortTest { + public SliceSort sliceSort = new SliceSort(); + + @Test + public void sample1() { + // Given + int[] given = {2, 4, 1, 6, 5, 9, 7}; + + // When + int slicedCount = sliceSort.sort(given); + + // Then + Assertions.assertEquals(3, slicedCount); + } + + @Test + public void sample2() { + // Given + int[] given = {4, 3, 2, 6, 1}; + + // When + int slicedCount = sliceSort.sort(given); + + // Then + Assertions.assertEquals(1, slicedCount); + } + + @Test + public void sample3() { + // Given + int[] given = {2, 1, 6, 4, 3, 7}; + + // When + int slicedCount = sliceSort.sort(given); + + // Then + Assertions.assertEquals(3, slicedCount); + } +} From cbe905b514209839fb2c5c2a8f403a76e41bc253 Mon Sep 17 00:00:00 2001 From: "rica.rdo" Date: Wed, 9 Jun 2021 16:50:24 +0900 Subject: [PATCH 2/3] Complete 1st version of SliceSort problem --- .../com/company/problem/hard/SliceSort.java | 82 ++++++++++++++++++- .../company/problem/hard/SliceSortTest.java | 6 +- 2 files changed, 83 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/company/problem/hard/SliceSort.java b/src/main/java/com/company/problem/hard/SliceSort.java index 6bda23b..0e6bdac 100644 --- a/src/main/java/com/company/problem/hard/SliceSort.java +++ b/src/main/java/com/company/problem/hard/SliceSort.java @@ -1,5 +1,10 @@ package com.company.problem.hard; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; + /** * PROBLEM: * @@ -22,8 +27,81 @@ */ public class SliceSort { - public int sort(int[] array) { - int count = array.length; + public int sort(Integer[] array) { + int count = 0; + // Exit condition 1 : array length is 1 + if (array.length == 1) { + return 1; + } + // 1) Slice until asc order stops (Note: a last element included) + List slices = slice(array); + + // Exit condition 2: Only a single slice exits + if (slices.size() == 1) { + Arrays.sort(array); + return 1; + } + // 2) Recursive call sort() for each sliced array + for (Integer[] s: slices) { + count += sort(s); + } + // 3) Merge sliced arrays sequentially and verify elements are asc ordered. + List merged = new LinkedList<>(); + + Integer prev = null; + boolean sorted = true; + for (Integer[] slice: slices) { + + for (Integer el: slice) { + merged.add(el); + if (prev == null) { + prev = el; + continue; + } + if (el < prev) { + sorted = false; + } + } + } + // 4-1) if so, Assign the sorted array to the given array + // 4-2) otherwise, Re-sort the merged array and slice count is 1 + if (sorted) { + merged.toArray(array); + } else { + count = 1; + Arrays.sort(array); + } + // 5) Return the count of slices return count; } + + private List slice(Integer[] array) { + List slices = new ArrayList<>(); + + Integer prev = null; + List slice = new LinkedList<>(); + for (int i = 0; i < array.length; i++) { + if (prev == null) { + prev = array[i]; + slice.add(array[i]); + continue; + } + + if (array[i] < prev) { + slice.add(array[i]); + + Integer[] e = slice.toArray(new Integer[]{}); + slices.add(e); + prev = null; + slice = new LinkedList<>(); + } else { + prev = array[i]; + slice.add(array[i]); + if (i == array.length -1) { + slices.add(slice.toArray(new Integer[]{})); + } + } + } + return slices; + } } diff --git a/src/test/java/com/company/problem/hard/SliceSortTest.java b/src/test/java/com/company/problem/hard/SliceSortTest.java index 578dea6..d14511f 100644 --- a/src/test/java/com/company/problem/hard/SliceSortTest.java +++ b/src/test/java/com/company/problem/hard/SliceSortTest.java @@ -11,7 +11,7 @@ public class SliceSortTest { @Test public void sample1() { // Given - int[] given = {2, 4, 1, 6, 5, 9, 7}; + Integer[] given = {2, 4, 1, 6, 5, 9, 7}; // When int slicedCount = sliceSort.sort(given); @@ -23,7 +23,7 @@ public void sample1() { @Test public void sample2() { // Given - int[] given = {4, 3, 2, 6, 1}; + Integer[] given = {4, 3, 2, 6, 1}; // When int slicedCount = sliceSort.sort(given); @@ -35,7 +35,7 @@ public void sample2() { @Test public void sample3() { // Given - int[] given = {2, 1, 6, 4, 3, 7}; + Integer[] given = {2, 1, 6, 4, 3, 7}; // When int slicedCount = sliceSort.sort(given); From d008ec6dc65d93cecf7702a3de433a7c687b636d Mon Sep 17 00:00:00 2001 From: "rica.rdo" Date: Wed, 9 Jun 2021 17:06:36 +0900 Subject: [PATCH 3/3] Remove a unnecessary exit condition --- src/main/java/com/company/problem/hard/SliceSort.java | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/company/problem/hard/SliceSort.java b/src/main/java/com/company/problem/hard/SliceSort.java index 0e6bdac..eb6c225 100644 --- a/src/main/java/com/company/problem/hard/SliceSort.java +++ b/src/main/java/com/company/problem/hard/SliceSort.java @@ -29,14 +29,11 @@ public class SliceSort { public int sort(Integer[] array) { int count = 0; - // Exit condition 1 : array length is 1 - if (array.length == 1) { - return 1; - } + // 1) Slice until asc order stops (Note: a last element included) List slices = slice(array); - // Exit condition 2: Only a single slice exits + // Exit condition: Only a single slice exits if (slices.size() == 1) { Arrays.sort(array); return 1;