leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

2780.cpp (581B)


      1 class Solution {
      2   public:
      3     int minimumIndex(const vector<int> &nums) {
      4         int candid = nums[0], count = 0;
      5         for (const int n : nums) {
      6             if (!count) candid = n;
      7             count += candid == n ? 1 : -1;
      8         }
      9 
     10         int ccount = 0;
     11         for (const int n : nums)
     12             if (n == candid) ccount++;
     13 
     14         count = 0;
     15         for (int i = 0; i < nums.size(); i++) {
     16             if (nums[i] == candid) count++;
     17             if ((count * 2 > i + 1) && (ccount - count) * 2 > nums.size() - i - 1) return i;
     18         }
     19 
     20         return -1;
     21     }
     22 };