diff --git a/2780. Minimum Index of a Valid Split b/2780. Minimum Index of a Valid Split new file mode 100644 index 0000000..bee9be7 --- /dev/null +++ b/2780. Minimum Index of a Valid Split @@ -0,0 +1,22 @@ +struct Solution { + int minimumIndex(vector& nums) { + int dom=1; + uint dom_count=1; + for (auto& i:nums) { // find dom + if (dom_count==0) { ++dom_count; dom = i; } + else dom_count += i==dom ? 1 : -1; + } + dom_count=0; // re-use as proper count + for (auto& i:nums) if (i==dom) ++dom_count; + uint doms=0; + // cout << dom << endl; + auto it=nums.begin(); + uint n=nums.size(); + for (uint i=1, j=n-1; i<=n; ++i, --j, ++it) { + if (*it==dom) { ++doms; --dom_count; } + // cout << doms << " " << dom_count << endl; + if (doms*2>i and dom_count*2>j) return i-1; + } + return -1; + } +};