【LeetCode】1099. Two Sum Less Than K 解题报告(C++)
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
题目地址:https://leetcode-cn.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 <= 1001 <= A[i] <= 10001 <= K <= 2000
题目大意
给你一个整数数组 A 和一个整数 K,请在该数组中找出两个元素,使它们的和小于 K 但尽可能地接近 K,返回这两个元素的和。
如不存在这样的两个元素,请返回 -1。
解题方法
暴力求解
本来觉得这个题好难,但是看了数组的范围才有100个元素,果断暴力求解即可。
C++代码如下:
class Solution {
public:
int twoSumLessThanK(vector<int>& A, int K) {
const int N = A.size();
int res = -1;
int minDist = INT_MAX;
for (int i = 0; i < N; i ++) {
for (int j = i + 1; j < N; j ++) {
int sum = A[i] + A[j];
if (sum < K && K - sum < minDist) {
minDist = K - sum;
res = sum;
}
}
}
return res;
}
};
日期
2019 年 9 月 18 日 —— 今日又是九一八
【LeetCode】1099. Two Sum Less Than K 解题报告(C++)的更多相关文章
- 【LeetCode】862. Shortest Subarray with Sum at Least K 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 队列 日期 题目地址:https://leetcod ...
- LeetCode 1099. Two Sum Less Than K
原题链接在这里:https://leetcode.com/problems/two-sum-less-than-k/ 题目: Given an array A of integers and inte ...
- 【LeetCode】404. Sum of Left Leaves 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目大意 题目大意 解题方法 递归 迭代 日期 [LeetCode] 题目地址:h ...
- 【LeetCode】371. Sum of Two Integers 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 位运算 日期 题目地址:https://leetco ...
- 【LeetCode】633. Sum of Square Numbers 解题报告(python & Java & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 列表生成式 循环 日期 题目地址:https ...
- 【LeetCode】779. K-th Symbol in Grammar 解题报告(Python)
[LeetCode]779. K-th Symbol in Grammar 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingz ...
- 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)
[LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】881. Boats to Save People 解题报告(Python)
[LeetCode]881. Boats to Save People 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...
- 【LeetCode】802. Find Eventual Safe States 解题报告(Python)
[LeetCode]802. Find Eventual Safe States 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemi ...
随机推荐
- 54. Flatten Binary Tree to Linked List
Flatten Binary Tree to Linked List My Submissions QuestionEditorial Solution Total Accepted: 81373 T ...
- Ubantu nodejs卸载与二进制安装
#apt-get 卸载 sudo apt-get remove --purge npm sudo apt-get remove --purge nodejs sudo apt-get remove - ...
- 【JAVA】【Basic】MacOS上搭建JAVA开发环境
1. JRE 1.1. 下载地址:https://www.oracle.com/technetwork/java/javase/downloads/index.html 1.1.1. dmg格式安装: ...
- java面试--(生成随机数,获取重复次数最多,并且数是最大的一个,打印出来)
import java.util.*; public class MaxRandom { public static void main(String[] args){ int[] num = new ...
- Vue中如何书写js来渲染页面填充数据的部分代码
new Vue({ el:"#app" , data:{ user:{ id:"", username:"", password:" ...
- 【HarmonyOS】【多线程与并发】EventHandler
EventHandler与EventRunner EventHandler相关概念 ● EventHandler是一种用户在当前线程上投递InnerEvent事件或者Runnable任务到异步线程上处 ...
- Webpack学习篇
<深入浅出Webpack>优化篇 01 Webpack 优化可以分为开发优化和输出质量优化两部分,主要要点如下: 优化开发体验,提升开发效率 优化构建速度 优化使用体验 优化输出质量 减少 ...
- 07-Spring5 WebFlux响应式编程
SpringWebFlux介绍 简介 SpringWebFlux是Spring5添加的新模块,用于Web开发,功能和SpringMvc类似的,WebFlux使用当前一种比较流行的响应式编程框架 使用传 ...
- 算法 A-Star(A星)寻路
一.简介 在游戏中,有一个很常见地需求,就是要让一个角色从A点走向B点,我们期望是让角色走最少的路.嗯,大家可能会说,直线就是最短的.没错,但大多数时候,A到B中间都会出现一些角色无法穿越的东西,比如 ...
- react-hook简单使用
一.函数式组件创建 function HelloComponent(props, /* context */) { return <div>Hello {props.name}</d ...