Given an array nums of integers and integer k, return the maximum sum such that there exists i < j with nums[i] + nums[j] = sum and sum < k. If no i, j exist satisfying this equation, return -1.
Example 1:
Input: nums = [34,23,1,24,75,33,54,8], k = 60
Output: 58
Explanation: We can use 34 and 24 to sum 58 which is less than 60.
Example 2:
Input: nums = [10,20,30], k = 15
Output: -1
Explanation: In this case it is not possible to get a pair sum less that 15.
class Solution {
public:
int twoSumLessThanK(vector<int>& nums, int k) {
sort(nums.begin(), nums.end());
int l = 0, r = nums.size() - 1;
int result = -1;
while (l < r){
int tmp = nums[l] + nums[r];
if (tmp < k){
l++;
result = max(result, tmp);
}
else{
r--;
}
}
return result;
}
};
执行用时:0 ms, 在所有 C++ 提交中击败了100.00%的用户
内存消耗:8.8 MB, 在所有 C++ 提交中击败了65.52%的用户