diff options
author | Jack Sangdahl <[email protected]> | 2025-03-26 20:36:50 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-03-26 20:36:50 -0600 |
commit | 3b7e5a469d82048dbb0122dd2207390ef8f5f653 (patch) | |
tree | 8de171df3d9ddab351cae84a6ce0e46346bd5f72 /min_idx_valid_split.c | |
parent | 4772cdabe0a208eca2e63d5eb56b3f0bc2164eeb (diff) | |
download | leetcode-3b7e5a469d82048dbb0122dd2207390ef8f5f653.tar.gz leetcode-3b7e5a469d82048dbb0122dd2207390ef8f5f653.zip |
2780. minimum index of a valid split
Diffstat (limited to 'min_idx_valid_split.c')
-rw-r--r-- | min_idx_valid_split.c | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/min_idx_valid_split.c b/min_idx_valid_split.c new file mode 100644 index 0000000..fbb2b98 --- /dev/null +++ b/min_idx_valid_split.c @@ -0,0 +1,43 @@ +// 2780. Minimum Index of a Valid Split +#include "leetcode.h" + +/* + * an element 'x' of an integer array 'arr' of length 'm' is dominant if more + * than half the elements of 'arr' have a value of 'x'. you are given a + * 0-indexed integer array 'nums' of length 'n' with one dominant element. you + * can split 'nums' at an index 'i' into two arrays 'nums[0, ..., i]' and + * 'nums[i + 1, ..., n - 1]', but the split is only valid if '0 <= i < - 1' and + * the two arrays have the same dominant element. return the minimum index of a + * valid split. if no split exists, return -1. + */ + +int minimumIndex(int *nums, int numsSize) { + int dominant = nums[0], cnt = 0; + for (int i = 0; i < numsSize; i++) { + cnt = nums[i] == dominant ? cnt + 1 : cnt - 1; + if (!cnt) { + dominant = nums[i]; + cnt = 1; + } + } + int left = 0; + for (int i = 0; i < numsSize; i++) + if (nums[i] == dominant) + left++; + cnt = 0; + for (int i = 0; i < numsSize; i++) { + if (nums[i] == dominant) + cnt++; + if (cnt * 2 > (i + 1) && (left - cnt) * 2 > (numsSize - i - 1)) + return i; + } + return -1; +} + +int main() { + int n1[] = {1, 2, 2, 2}, n2[] = {2, 1, 3, 1, 1, 1, 7, 1, 2, 1}, + n3[] = {3, 3, 3, 3, 7, 2, 2}; + printf("%d\n", minimumIndex(n1, ARRAY_SIZE(n1))); // expect: 2 + printf("%d\n", minimumIndex(n2, ARRAY_SIZE(n2))); // expect: 4 + printf("%d\n", minimumIndex(n3, ARRAY_SIZE(n3))); // expect: -1 +} |