Created
August 7, 2019 06:41
-
-
Save crazy4groovy/5f82d93c2cd804ed3f44f1b29ad9b137 to your computer and use it in GitHub Desktop.
How many groups (more than 1 number per group) of an integer list can be split out, each sorted, and concatenated together to produce the same result as sorting the list?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function solution(A) { | |
let index = 0 | |
let results = 0 | |
let remainder = 0 | |
let number | |
while (index < A.length) { | |
number = A[index] | |
for (let i = index + 1; i < A.length; i++) { | |
if (number > A[i]) { | |
index = i | |
} | |
} | |
if (number !== A[index]) { | |
results += 1 | |
remainder = false | |
} | |
else remainder = true | |
index += 1 | |
} | |
if (remainder) results += 1 | |
return results > 1 ? results : 0 | |
} | |
console.log(solution([1,2,3,4,5])) // 0 => no subgroups, just sort it | |
console.log(solution([5,4,3,2,1])) // 0 => no subgroups, just sort it | |
console.log(solution([2,1,3,4,5])) // 2 => [2,1],[3,4,5] | |
console.log(solution([2,1,4,3,6,5])) // 3 => [2,1],[4,3],[6,5] | |
console.log(solution([2,1,5,6,3])) // 2 => [2,1],[5,6,3] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment