Maximize Score of Numbers in Ranges - Binary Search [JS]

Description 

Solution: Binary Search

Sort start in asc order and create the maximum difference between every adjacent pair.
Binary search for the maximum minimum difference diff between every two adjacent integers.
Greedily start with the minimum number (start[0] - d) and then try to make every difference equal to diff (it's optimal to only take the minimum needed to give a better chance for incoming integers).

n = length of startm = max diff + d
Time Complexity: O(n log(n) + n log(m))
Space Complexity: O(log(n)) (space for sorting)

function maxPossibleScore(start, d) {
  start.sort((a, b) => a - b);
  let low = 0, high = start[start.length - 1] - start[0] + d;
  while (low < high) {
    let mid = low + Math.ceil((high - low) / 2);
    if (isPossible(start, d, mid)) low = mid;
    else high = mid - 1;
  }
  return low;
};

function isPossible(start, d, diff) {
  let prev = start[0];
  for (let i = 1; i < start.length; i++) {
    if (start[i] + d - prev < diff) {
      return false;
    }
    prev = Math.max(start[i], prev + diff);
  }
  return true;
}

Comments

Popular posts from this blog

Maximum Value of an Ordered Triplet II - Two Solutions [JS]

Maximum Sum of Distinct Subarrays With Length K - Sliding Window w/ Two Pointers & Set [JS]

Sum of Prefix Scores of Strings - Trie [JS]

Maximum Count of Positive Integer and Negative Integer - Binary Search [JS]

Count Subarrays With Median K - Count Left & Right Balance [JS]