Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created January 20, 2018 07:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/48c8646be894d5aa34f646a048d8b1ba to your computer and use it in GitHub Desktop.
Save jianminchen/48c8646be894d5aa34f646a048d8b1ba to your computer and use it in GitHub Desktop.
Maximum number of chunks - peer analyzed the algorithm 11:20 PM Jan. 19, 2018
[2, 1, 3, 5, 4] -> integer array, N , element 1 to N
1 2 3
[1] [2] [3]
[[2, 1], [3], [5, 4] - sort each chunk, then whole array is soted. , return 3
Maximum number of chunks?
int i ;
int j ;
// between i and j - i have all the elements between i and j
int max = 1; // Max number for current chunk
int current_count = 0; // Current count of current chunk
int expected_count = 1; // Expected cout of current chunk
// [2, 1, 3, 5, 4]
for(int i = 0; i < n; i++){
current_count++;
if (a[i] > max){
expected_count = expected_count + a[i] - max;
max = a[i];
}
if (current_count == expected_count)
{
chunk++;
if (i+1 == n) break;
expected_count = a[i+1] - max;
max = a[i+1];
current_count = 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment