Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Max slice sorted array #11

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Max slice sorted array #11

wants to merge 3 commits into from

Conversation

rica-v3
Copy link
Member

@rica-v3 rica-v3 commented Jun 9, 2021

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.

  1. A = [2,1,6,4,3,7] will return 3.

N range [1..100,000]
element range [1..1,000,000,000]

Solution

  1. Slice a given until ascending order stops (Note: a last element included)
  2. Recursively call itself for each sliced array from step 1
  3. Merge sliced arrays sequentially and verify elements of the merged array are asc ordered.
    4-1) if so, Assign the merged array to the given array and return the slice count as it is.
    4-2) otherwise, sort the given array and return 1

Exit condition

  1. a slice count of a given array is 1

Another solution

fullshinesun/LeetCode@bd17eaf

@fullshinesun
Copy link

bigO 몇인가요?

@rica-v3
Copy link
Member Author

rica-v3 commented Jun 9, 2021

best-case: N*logN
worst-case: N^2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants