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, jexist 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
题解:
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 ...
随机推荐
- day21——面向对象初识、结构、从类名研究类、从对象研究类、logging模块进阶版
day21 面向对象的初识 面向对象第一个优点: 对相似功能的函数,同一个业务下的函数进行归类,分类. 想要学习面向对象必须站在一个上帝的角度去分析考虑问题. 类: 具有相同属性和功能的一类事物. 对 ...
- SQL SERVER 查询所有表大小
DECLARE @T TABLE ( [name] VARCHAR(max), [rows] INT, reserved VARCHAR(max), data_size VARCHAR(max), i ...
- (转)为什么ssh一关闭,程序就不再运行了?
ref :https://www.cnblogs.com/lomper/p/7053694.html 问题描述 当SSH远程连接到服务器上,然后运行一个程序,eg: ./test.sh, 然后把终端开 ...
- Docker登录容器命令
1. docker exec -i -t 13496e7d5830(容器名) /bin/sh 2.退出容器命令 exit
- vue-cli中轮播图vue-awesome-swiper使用方法
1 npm 安装 npm install vue-awesome-swiper --save 2在所用的组件中引入 import 'swiper/dist/css/swiper.css' import ...
- 常用 Maven 仓库地址
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/niuzhucedenglu/article ...
- Python进阶(一)----函数
Python进阶(一)----函数初识 一丶函数的初识 什么函数: 函数是以功能为导向.一个函数封装一个功能 函数的优点: 1.减少代码的重复性, 2.增强了代码的可读性 二丶函数的结构 ...
- Flutter — IDE Shortcuts for Faster Development
https://medium.com/flutter-community/flutter-ide-shortcuts-for-faster-development-2ef45c51085b If yo ...
- JavaWeb 之 Listener:监听器
一.概述 1.事件监听机制 事件: 一件事情 事件源: 事件发生的地方 监听器: 一个对象 注册监听: 将事件.事件源.监听器绑定在一起. 2.监听器概念 当事件源上发生某个 ...
- nginx服务的基本配置
Nginx在运行时,至少必须加载几个核心模块和一个事件类模块.这些模块运行时所支持的配置项称为基本配置.由于配置项较多,所以把它们按照用户使用时的预期功能分为四类: 用于调试.定位问题的配置项 正常运 ...