You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.
Suppose you have n
versions [1, 2, ..., n]
and you want to find out the first bad one, which causes all the following ones to be bad.
You are given an API bool isBadVersion(version)
which returns whether version
is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.
Example 1:
Example 2:
1 <= bad <= n <= 2^31 - 1
var solution = function(isBadVersion) {
return function(n) {
for (let i = 1; i <= n; i++) {
if (isBadVersion(i)) {
return i;
}
}
};
};
var solution = function(isBadVersion) {
return function(n) {
let left = 1;
let right = n;
let middle;
let firstBadVersion = n;
while (left <= right) {
middle = Math.floor((left + right)/2);
if (isBadVersion(middle)) {
firstBadVersion = Math.min(firstBadVersion, middle);
right = middle - 1;
} else {
left = middle + 1;
}
}
return firstBadVersion;
};
};
Given an array nums
sorted in non-decreasing order, return the maximum between the number of positive integers and the number of negative integers.
In other words, if the number of positive integers in nums
is pos
and the number of negative integers is neg
, then return the maximum of pos
and neg
.
0
is neither positive nor negative.Example 1:
Example 2:
Example 3:
1 <= nums.length <= 2000
-2000 <= nums[i] <= 2000
nums
is sorted in a non-decreasing order./**
* @param {number[]} nums
* @return {number}
*/
var maximumCount = function(nums) {
return Math.max(getNegativeCount(nums), getPositiveCount(nums));
};
var getNegativeCount = function(nums) {
if (nums[0] >= 0) {
return 0;
}
let left = 0;
let right = nums.length - 1;
while (left < right) {
const middle = Math.ceil((right+left)/2);
if (nums[middle] < 0) {
left = middle;
} else {
right = middle - 1;
}
}
return left + 1;
}
var getPositiveCount = function(nums) {
if (nums[nums.length - 1] <= 0) {
return 0;
}
let left = 0;
let right = nums.length - 1;
while (left < right) {
const middle = Math.floor((right+ left)/2);
if (nums[middle] > 0) {
right = middle;
} else {
left = middle + 1;
}
}
return nums.length - left;
}
The pair sum of a pair (a,b)
is equal to a + b
. The maximum pair sum is the largest pair sum in a list of pairs.
For example, if we have pairs (1,5)
, (2,3)
, and (4,4)
, the maximum pair sum would be max(1+5, 2+3, 4+4) = max(6, 5, 8) = 8
.
Given an array nums
of even length n
, pair up the elements of nums into n / 2
pairs such that:
nums
is in exactly one pair, andExample 1:
Example 2:
n == nums.length
2 <= n <= 10^5
n
is even.1 <= nums[i] <= 10^5
/**
* @param {number[]} nums
* @return {number}
*/
var minPairSum = function(nums) {
const length = nums.length;
let maxPairSum = 0;
nums.sort((a, b) => a - b);
for (let i = 0; i < length/2; i++) {
const pairSum = nums[i] + nums[length - i - 1];
maxPairSum = Math.max(maxPairSum, pairSum);
}
return maxPairSum;
};