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. 1 <= A.length <= 100
  2. 1 <= A[i] <= 1000
  3. 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的更多相关文章

  1. 【LeetCode】1099. Two Sum Less Than K 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力求解 日期 题目地址:https://leetco ...

  2. LeetCode 1099. Two Sum Less Than K

    原题链接在这里:https://leetcode.com/problems/two-sum-less-than-k/ 题目: Given an array A of integers and inte ...

  3. [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 ...

  4. 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 ...

  5. leetcode 862 shorest subarray with sum at least K

    https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/ 首先回顾一下求max子数组的值的方法是:记录一个前缀min值, ...

  6. 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 ...

  7. [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 ...

  8. 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 ...

  9. 【LeetCode】862. Shortest Subarray with Sum at Least K 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 队列 日期 题目地址:https://leetcod ...

随机推荐

  1. 用Pandas Dataframe支撑起多只金融产品股票的数据内部形态

    3. 如果同时拿一个板块股票的收市价和成交额 前一篇说到,用大盘指数,如恒生指数,上证,深证,这些重要的大盘指数来做Dataframe主键,那麽如果是同时拿一个板块股票的收市价和成交额,可以怎样操作呢 ...

  2. SEO教程:快速增加360搜索引擎收录,360自动推送批量推送版

    上次改编了一下百度的JS推送代码,实现了批量推送 传送门>>>百度链接提交-js代码推送批量推送版 这次我们来研究360js自动推送代码. <script> (funct ...

  3. Python语言基础与应用 (P23)上机练习:容器类型操作(未完待续)

    上机练习:容器类型操作〉 列表.元组基本操作+, *, len(), [], in Python 3.7.0 (default, Jun 28 2018, 08:04:48) [MSC v.1912 ...

  4. UML-GoF设计模式-总结

    1.GRASP 2.设计模式

  5. 绩效软件交流-ZQDJ

    积分制(主管激励下属)短期任务积分 长期任务积分 制度积分 固定积分任务工作项 评估表 ,取中间值工时调整 工作表现 创新加分 难度加分 贡献加分 绩效分-积分(软件亮点)  分开做 没有管理员的中层 ...

  6. Java架构师笔记-你必须掌握的 21 个 Java 核心技术!(干货)

    闲来无事,师长一向不(没)喜(有)欢(钱)凑热闹,倒不如趁着这时候复盘复盘.而写这篇文章的目的是想总结一下自己这么多年来使用java的一些心得体会,希望可以给大家一些经验,能让大家更好学习和使用Jav ...

  7. Xpath编码问题解决

    使用Xpath获取属性时,出现乱码问题,解决办法找了好多,终于解决,特将办法贴在这,供大家尝试 不要直接简单的将爬取的网页设置为utf-8, 先通过print(r.encoding)输出看看爬取的是什 ...

  8. JavaSE--[转]加密和签名的区别

    转载 http://blog.csdn.net/u012467492/article/details/52034835 私钥用来签名的,公钥用来验签的.公钥加密私钥解密是秘送,私钥加密公钥解密是签名 ...

  9. python机器学习(1:K_means聚类算法)

    一.算法介绍 K-means算法是最简单的也是最著名的划分聚类算法,由于简洁和效率使得他成为所有聚类算法中最广泛使用的.算法的目的是使各个样本与所在均值的误差平方和达到最小(这也是评价K-means算 ...

  10. JetBrains Phpstorm 、 PyCharm 2019 最新破解码亲测可用

    Phpstorm 2019 破解码最新亲测可用 6ZUMD7WWWU-eyJsaWNlbnNlSWQiOiI2WlVNRDdXV1dVIiwibGljZW5zZWVOYW1lIjoiSmV0cyBHc ...