leetcode

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

1913.cpp (621B)


      1 class Solution {
      2   public:
      3     int maxProductDifference(vector<int> &nums) const {
      4         sort(begin(nums), begin(nums) + 4);
      5         int a = nums[0], b = nums[1];
      6         int c = nums[2], d = nums[3];
      7         for (int i = 4; i < nums.size(); i++) {
      8             if (nums[i] > d) {
      9                 c = d;
     10                 d = nums[i];
     11             } else if (nums[i] > c) {
     12                 c = nums[i];
     13             } else if (nums[i] < a) {
     14                 b = a;
     15                 a = nums[i];
     16             } else if (nums[i] < b) {
     17                 b = nums[i];
     18             }
     19         }
     20         return c * d - a * b;
     21     }
     22 };