0621-0630

621. Task Scheduler $\star\star$

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
 public:
  int leastInterval(vector<char>& tasks, int n) {
    vector<int> count(26, 0);
    for (char task : tasks) ++count[task - 'A'];

    const int maxCount = *max_element(count.begin(), count.end());
    int ans = (maxCount - 1) * (n + 1);
    ans += count_if(count.begin(), count.end(),
                    [maxCount](int freq) { return freq == maxCount; });

    return max(ans, (int)tasks.size());
  }
};

622. Design Circular Queue $\star\star$

623. Add One Row to Tree $\star\star$

624. Maximum Distance in Arrays $\star$

625. Minimum Factorization $\star\star$

626. Exchange Seats $\star\star$

627. Swap Salary $\star$

628. Maximum Product of Three Numbers $\star$

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution {
 public:
  int maximumProduct(vector<int>& nums) {
    int min = INT_MAX;
    int secondMin = INT_MAX;
    int max = INT_MIN;
    int secondMax = INT_MIN;
    int thirdMax = INT_MIN;

    for (int num : nums) {
      if (num <= min) {
        secondMin = min;
        min = num;
      } else if (num <= secondMin)
        secondMin = num;

      if (num >= max) {
        thirdMax = secondMax;
        secondMax = max;
        max = num;
      } else if (num >= secondMax) {
        thirdMax = secondMax;
        secondMax = num;
      } else if (num >= thirdMax)
        thirdMax = num;
    }

    return std::max(min * secondMin * max, max * secondMax * thirdMax);
  }
};

629. K Inverse Pairs Array $\star\star\star$

630. Course Schedule III $\star\star\star$