[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
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
class Solution {
public int twoSumLessThanK(int[] A, int K) {
if (A == null || A.length == 0) {
return -1;
}
Arrays.sort(A);
int start = 0, end = A.length - 1;
int res = -1;
while (start < end) {
if (A[start] + A[end] >= K) {
end -= 1;
} else {
res = Math.max(res, A[start] + A[end]);
start += 1;
}
}
return res;
}
}
[LC] 1099. Two Sum Less Than K的更多相关文章
- 【LeetCode】1099. Two Sum Less Than K 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力求解 日期 题目地址:https://leetco ...
- LeetCode 1099. Two Sum Less Than K
原题链接在这里:https://leetcode.com/problems/two-sum-less-than-k/ 题目: Given an array A of integers and inte ...
- [Swift]LeetCode862. 和至少为 K 的最短子数组 | Shortest Subarray with Sum at Least K
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K. If there ...
- LeetCode862. Shortest Subarray with Sum at Least K
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K. If there ...
- leetcode 862 shorest subarray with sum at least K
https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/ 首先回顾一下求max子数组的值的方法是:记录一个前缀min值, ...
- 862. Shortest Subarray with Sum at Least K
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K. If there ...
- [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 ...
- LC 974. Subarray Sums Divisible by K
Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum ...
- 【LeetCode】862. Shortest Subarray with Sum at Least K 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 队列 日期 题目地址:https://leetcod ...
随机推荐
- Mac Outlook 2016 无法打开会议室日历
问题:Mac Outlook 2016 无法打开会议室日历信息,报错截图如下: 解决方案: Set-MailboxFolderPermission -Identity XXX@xxx.com:\日历 ...
- CentOS 7.3 下部署基于 Node.js的微信小程序商城
本文档为微信小程序商城NideShop项目的安装部署教程,欢迎star NideShop商城api服务:https://github.com/tumobi/nideshop NideShop微信小程序 ...
- 康冕峰IT技术总结博客CSDN索引
计算1-x内的质数, 结果保存在mysql中. Java 程序员面试笔试宝典 4.1基础知识https://blog.csdn.net/qq_40993412/article/details/1040 ...
- Java 知识点(一)
博主对 Java知识点的整理基于 c语言,整理内容为 Java的重点及与 c语言的差异点或编程通要知识点.水平有限,欢迎指正.(参考书籍<Java 核心技术 卷Ⅰ>) Java 的类名:名 ...
- Java中常用的API(四)——其他
前面说三篇文章分别介绍了Object.String.字符缓冲类的API,接下来我们简要介绍一下其他常用的API. 1.System System类用于获取各种系统信息,最为常用的是: System.o ...
- eclipse环境配置,字体大小,代码智能提示,JSP页面默认字符集修改
安装好JDK后,下载Java EE解压版eclipse 1.字体大小 Windows——>Preferences——>General——>Appearance——>Colors ...
- Spring Cloud Alibaba 教程 | Nacos(三)
使用Nacos作为配置中心 前面我们已经介绍过滤Nacos是一个更易于构建云原生应用的动态服务发现.配置管理和服务管理平台.所以它可以作为注册中心和配置中心,作为注册中心Nacos可以让我们灵活配置多 ...
- IE浏览器F12调试模式不能使用或报错以及安装程序遇到错误0x80240037的解决办法
记录一下,方便以后查找 IE浏览器F12调试模式不能使用: 需要下载补丁: 64位系统 然后下载安装,就能解决问题. 要是在安装时遇到出现: 安装程序遇到错误 0x80240037 解决方式 最后 ...
- Java 中的接口有什么作用?以及接口和其实现类的关系?
Java 中的接口有什么作用? - Ivony的回答 - 知乎 https://www.zhihu.com/question/20111251/answer/16585393 这是一个初学者非常常见的 ...
- 蓝屏(BSOD)转储设置,看本文就够了!
原总结注册表debug调试dump转储文件蓝屏BSODprocess monitor 前言 我们在 内核转储,开抓啦! 这篇文章里介绍了一个关键的系统设置.设置好后可以让系统在蓝屏(Blue Scre ...