aboutsummaryrefslogtreecommitdiff
path: root/max_chunks_make_sorted.c
diff options
context:
space:
mode:
Diffstat (limited to 'max_chunks_make_sorted.c')
-rw-r--r--max_chunks_make_sorted.c27
1 files changed, 27 insertions, 0 deletions
diff --git a/max_chunks_make_sorted.c b/max_chunks_make_sorted.c
new file mode 100644
index 0000000..ec73bc5
--- /dev/null
+++ b/max_chunks_make_sorted.c
@@ -0,0 +1,27 @@
+// 769. Max Chunks To Make Sorted
+#include "leetcode.h"
+
+/*
+ * you are given an integer array 'arr' of length 'n' that represents a
+ * permutation of the integers in the range '[0, n - 1]'. we split 'arr' into
+ * some number of chunks (ie. partitions), and individually sort each chunk.
+ * after concatenating them, the result should equal the sorted array. return
+ * the largest number of chunks we can make to sort the array.
+ */
+
+int maxChunksToSorted(int *arr, int arrSize) {
+ int cnt = 0, sum = 0, ans = 0;
+ for (int i = 0; i < arrSize; i++) {
+ sum += i;
+ cnt += arr[i];
+ if (sum == cnt)
+ ans++;
+ }
+ return ans;
+}
+
+int main() {
+ int a1[] = {4, 3, 2, 1, 0}, a2[] = {1, 0, 2, 3, 4};
+ printf("%d\n", maxChunksToSorted(a1, ARRAY_SIZE(a1))); // expect: 1
+ printf("%d\n", maxChunksToSorted(a2, ARRAY_SIZE(a2))); // expect: 4
+}