LC: 1099. Two Sum Less Than K
1099. Two Sum Less Than K
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.Constraints:
1 <= nums.length <= 1001 <= nums[i] <= 10001 <= k <= 2000
The Essence:
Der Schwerpunkt der Frage ist, wie man in Integer-Array suchen kann. Es ist zu beachten, dass das Array nicht sortiert ist.
Details:
Man kann das gegebene Array sortieren. Die Summe von dem ersten und dem letzten Element kann mit der gesuchten Summe verglichen werden, um die Indizes zu verschieben.
Solution(s):
Default Code:
PreviousLC: 1088. Confusing Number IINextLC: 1100. Find K-Length Substrings With No Repeated Characters
Last updated