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, j
exist 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 <= A.length <= 100
1 <= A[i] <= 1000
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:
class Solution {
public int twoSumLessThanK(int[] A, int K) {
int res = -1;
if(A == null || A.length == 0){
return res;
} Arrays.sort(A);
int l = 0;
int r = A.length-1;
while(l<r){
int sum = A[l] + A[r];
if(sum < K){
res = Math.max(res, sum);
l++;
}else{
r--;
}
} return res;
}
}
类似Subarray Product Less Than K.
LeetCode 1099. Two Sum Less Than K的更多相关文章
- 【LeetCode】1099. Two Sum Less Than K 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力求解 日期 题目地址:https://leetco ...
- [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 wit ...
- LeetCode 560. Subarray Sum Equals K (子数组之和等于K)
Given an array of integers and an integer k, you need to find the total number of continuous subarra ...
- leetcode 862 shorest subarray with sum at least K
https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/ 首先回顾一下求max子数组的值的方法是:记录一个前缀min值, ...
- LeetCode解题报告--2Sum, 3Sum, 4Sum, K Sum求和问题总结
前言: 这几天在做LeetCode 里面有2sum, 3sum(closest), 4sum等问题, 这类问题是典型的递归思路解题.该这类问题的关键在于,在进行求和求解前,要先排序Arrays.sor ...
- [LeetCode] 862. Shortest Subarray with Sum at Least K 和至少为K的最短子数组
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K. If there ...
- [LeetCode] 560. Subarray Sum Equals K 子数组和为K
Given an array of integers and an integer k, you need to find the total number of continuous subarra ...
- 【LeetCode】862. Shortest Subarray with Sum at Least K 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 队列 日期 题目地址:https://leetcod ...
- [leetcode]560. Subarray Sum Equals K 和为K的子数组
Given an array of integers and an integer k, you need to find the total number of continuous subarra ...
随机推荐
- Linux内核--伙伴系统--页释放
本文转载自:http://www.cnblogs.com/tolimit/ 感觉原博分析的不错,借花献佛. ---------------------------------------------- ...
- Akka-CQRS(9)- gRPC,实现前端设备与平台系统的高效集成
前面我们完成了一个CQRS模式的数据采集(录入)平台.可以预见:数据的产生是在线下各式各样的终端系统中,包括web.桌面.移动终端.那么,为了实现一个完整的系统,必须把前端设备通过某种网络连接形式与数 ...
- 2019 同程旅游java面试笔试题 (含面试题解析)
本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.同程等公司offer,岗位是Java后端开发,因为发展原因最终选择去了同程,入职一年时间了,之前面试了很多家公 ...
- Java自学-Scanner类
使用Scanner读取数据 System.out.println("") 用于向控制台输出数据. 我们还需要从控制台输入数据,所以需要用到Scanner类. 步骤 1 : 使用Sc ...
- 英伟达 cuda 开发套件下载
下载地址 https://developer.nvidia.com/cuda-toolkit 安装比较简单,就不多说了.
- loadrunner通过字符串左右边界提取字符串
/****** *函数名称:strcut *函数说明:通过左边界.右边界,从字符串中截取子字符串 *注意事项:会申请新的内存,需要手动释放 ******/ void strcut(char *strS ...
- 纯css实现省略号,兼容火狐,IE9,chrome
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 介绍一个免费的云开发工具:Cloud Shell
上周和一德国同事吹牛的时候,他说最近业余时间在玩一个东东,叫做Cloud Shell,Google出品.Jerry之前听说过国内的阿里云也提供过类似的解决方案,即在云端提供一个受限制的Linux环境并 ...
- java代码转python代码
(1)安装工具(windows 环境下面) 先下载antlr: http://www.antlr3.org/download/antlr-3.1.3.tar.gz 链接:http://pan.baid ...
- pycharm下site-packages文件标记为红的问题;pycharm无法识别本地site-packages问题
当图示红框标记区域的文件夹颜色显示红色时,需要到FIle-setting里面设置好本地的运行环境,设置错误就会导致引用问题: 启动谷歌浏览器 from selenium import webdrive ...