LeetCode 1099. Two Sum Less Than K

原题链接在这里:https://leetcode.com/problems/two-sum-less-than-k/

题目:

Given an array A of integers and integer K, return the maximum S such that there exists i < j with A[i] + A[j] = S and S < K. If no i, jexist satisfying this equation, return -1.

Example 1:

Input: A = [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: A = [10,20,30], K = 15
Output: -1
Explanation:
In this case it‘s not possible to get a pair sum less that 15.

Note:

  1. 1 <= A.length <= 100
  2. 1 <= A[i] <= 1000
  3. 1 <= K <= 2000

题解:

Sort the array. Use two pointers pointing at head and tail. Calculate the sum.

If sum < K, update res and l++ to increase sum.

Otherwise, r-- to decrease sum.

Time Complexity: O(nlogn). n = A.length.

Space: O(1).

AC Java:

 1 class Solution {
 2     public int twoSumLessThanK(int[] A, int K) {
 3         int res = -1;
 4         if(A == null || A.length == 0){
 5             return res;
 6         }
 7
 8         Arrays.sort(A);
 9         int l = 0;
10         int r  = A.length-1;
11         while(l<r){
12             int sum = A[l] + A[r];
13             if(sum < K){
14                 res = Math.max(res, sum);
15                 l++;
16             }else{
17                 r--;
18             }
19         }
20
21         return res;
22     }
23 }

类似Subarray Product Less Than K.

原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/11368115.html

时间: 2024-10-10 17:16:27

LeetCode 1099. Two Sum Less Than K的相关文章

[LC] 1099. Two Sum Less Than K

Given an array A of integers and integer K, return the maximum S such that there exists i < j with A[i] + A[j] = S and S < K. If no i, j exist satisfying this equation, return -1. Example 1: Input: A = [34,23,1,24,75,33,54,8], K = 60 Output: 58 Expl

leetcode 862 shorest subarray with sum at least K

https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/ 首先回顾一下求max子数组的值的方法是:记录一个前缀min值,然后扫一遍sum数组. 1.首先这里不需要最大,因为刚好够k就好了 2.这里需要距离最短.就是数组的长度最短. 这里的思路也一样,不过保存很多个min值,就是用一个队列,保存前缀的min值,不需要最min,只不过有更小的就更好. 也就是如果sum数组的值是: ..... 60, 40.....Y..

[leetcode 560. Subarray Sum Equals K]

Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k. Example 1: Input:nums = [1,1,1], k = 2 Output: 2 Note: The length of the array is in range [1, 20,000]. The range of numbers

Java for LeetCode 216 Combination Sum III

Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Ensure that numbers within the set are sorted in ascending order. Example 1

[LeetCode] 560. Subarray Sum Equals K_Medium

Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k. Example 1: Input:nums = [1,1,1], k = 2 Output: 2 Note: The length of the array is in range [1, 20,000]. The range of numbers

lc 862. Shortest Subarray with Sum at Least K

断网导致原来写的那么多答案全没了,博客园能不能实时保存草稿,醉. https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/ 给一个数组a,找和大于k的所有子数组中的最短的那个. 最近二分有点上头,因为的确很强大,两个考虑二分的情况(其实需要刻意去想想能不能使用二分才会想到,这会是个好习惯): 1.最优问题变判断问题.比如:行递增列递增的矩阵中找某个元素->行递增列递增的矩阵中找第k大元素 2.有相应的简单孪生问题.比

LeetCode --- 1. Two Sum

题目链接:Two Sum Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Plea

LeetCode:Range Sum Query - Immutable - 数组指定区间内的元素和

1.题目名称 Range Sum Query(数组指定区间内的元素和) 2.题目地址 https://leetcode.com/problems/range-sum-query-immutable/ 3.题目内容 英文:Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. 中文:给定一个数组nums,求出索引i和j之间元素的和,i一定是小于或等于j

ACdream 1099 瑶瑶的第K大

瑶瑶的第K大 Time Limit: 10000/5000MS (Java/Others)Memory Limit: 512000/256000KB (Java/Others) SubmitStatisticNext Problem Problem Description 一天,萌萌的妹子--瑶瑶(tsyao)很无聊,就来找你玩.可是你们都不知道玩什么...尴尬了一阵子,机智的瑶瑶就提议:"这样吧,你说N个整数xi,然后在随意说一个数字k,我能够快速地说出这些数字里面第 k 大的数字."