[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 <= 1001 <= A[i] <= 10001 <= 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 ...
随机推荐
- 主席树--动态区间第k小
主席树--动态区间第\(k\)小 模板题在这里洛谷2617. 先对几个问题做一个总结: 阅读本文需要有主席树的基础,也就是通过区间kth的模板题. 静态整体kth: sort一下找第k小,时间复杂度\ ...
- java程序无法连接Rabbitmq
java程序不能连接到rabbitmq,提示有包括501.403.402等错误. 各种调试都没用,后来是尝试新建一个用户,在配置文件中使用新用户连接,最后才使程序成功运行.
- POJ 1979:Red and Black
Red and Black Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 26058 Accepted: 14139 D ...
- 和我一起从0学算法(C语言版)(四)
第三章 搜索 深度优先搜索与宽度优先搜索 定义 深度优先搜索(DFS) 过程简要来说是对每一个可能的分支路径深入到不能再深入为止,而且每个节点只能访问一次. 宽度优先搜索(BFS) 不考虑结果的可能位 ...
- Servlet基本概念及其部署
什么servlet? Servlet(Server Applet)是Java Servlet的简称,称为小服务程序或服务连接器,用Java编写的服务器端程序,主要功能在于交互式地浏览和修改数据,生成动 ...
- webpack4+vue 打包 就是没效果?求解!!!
开始对着视频操作 教学视频 用的webpack2 所以没成功 但是 Jquery 可以 成功渲染.Vue就不行. 百度 webpack4+vue打包简单入门:https://segmentfault ...
- OpenMP笔记(一)
原文:https://www.bearoom.xyz/2019/02/17/openmp1/ 并行技术有很多种,OpenMP算是比较简单可用的一种,OpenMP全称是 Open Multi-Proce ...
- uwsgi Import Error: No module named 'encodings'
https://serverfault.com/questions/558427/uwsgi-import-error-no-module-named-encodings I don't know i ...
- MVC思想概叙
随着应用系统的逐渐增大,系统的业务逻辑复杂度是以几何的方式增长,在这种情况下,如果依然把所有的业务逻辑都放在JSP页面中,那将成为一场恶梦. MVC思想将应用中各个组件按照功能来进行分类,不同的组将使 ...
- 2×c列联表|多组比例简式|卡方检验|χ2检验与连续型资料假设检验
第四章 χ2检验 χ2检验与连续型资料假设检验的区别? 卡方检验的假设检验是什么? 理论值等于实际值 何条件下卡方检验的需要矫正?如何矫正? 卡方检验的自由度如何计算? Df=k-1而不是n-1 卡方 ...