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 ...
随机推荐
- C++分治策略实现快速排序
问题描述: 给定一个未知顺序的n个元素组成的数组,现要利用快速排序算法对这n个元素进行非递减排序. 细节须知: (1)代码实现了利用递归对数组进行快速排序,其中limit为从已有的随机数文件中输入的所 ...
- Python之路【第二十八篇】:django视图层、模块层
1.视图函数 文件在view_demo 一个视图函数简称视图,是一个简单的Python 函数,它接受Web请求并且返回Web响应.响应可以是一张网页的HTML内容,一个重定向,一个404错误,一个XM ...
- k8s 回滚应用
kubectl apply 每次更新应用时 Kubernetes 都会记录下当前的配置,保存为一个 revision(版次),这样就可以回滚到某个特定 revision. 滚动更新是一次只更新一小部分 ...
- ssm动态sql语句
1.将上面的元素分为四组来演示,分别为:[if,where,trim],[if,set,trim],[choose,when,otherwise],[foreach] ________________ ...
- 关于.Net使用企业库访问MySql数据库
关于.Net使用企业库访问MySql数据库 在网上看了很多又重写又加WebConfig中的内容,其实不用那么麻烦 企业库5.0访问MySql数据库只需要在Web服务器安装mysql-connector ...
- Java 之 框架概述
一.什么是框架 框架(Framework)是整个或部分系统的可重用设计,表现为一组抽象构件及构件实例间交互的方法;另一种定义认为,框架是可被应用开发者定制的应用骨架.前者是从应用方面而后者是从目的方面 ...
- Jmeter学习笔记(十九)——后置处理器之正则表达式的使用
一.正则表达式提取器的作用 允许用户从服务器的响应中通过使用perl的正则表达式提取值.作为一个后置处理器,该元素会作用在指定范围的取样器,应用正则表达式,提取所需要的值,生成模板字符串,并将结果存储 ...
- RSA算法一:数学原理
- intellij IDEA github clone 指定分支代码
1.问题描述 在实际开发中,我们通常会使用idea克隆一个新项目(clone),通常情况下,我们默认克隆的是master分支,但是如果master分支只是一个空文件夹而已,真正的代码在develop分 ...
- AIX 静默安装11gR2 RAC
AIX安装11gR2 RAC 一.1 BLOG文档结构图 一.2 前言部分 一.2.1 导读和注意事项 各位技术爱好者,看完本文后,你可以掌握如下的技能,也可以学到一些其它 ...