[leetcode-908-Smallest Range I]
2024-09-16 21:31:48
Given an array A
of integers, for each integer A[i]
we may choose any x
with -K <= x <= K
, and add x
to A[i]
.
After this process, we have some array B
.
Return the smallest possible difference between the maximum value of B
and the minimum value of B
.
Example 1:
Input: A = [1], K = 0
Output: 0
Explanation: B = [1]
Example 2:
Input: A = [0,10], K = 2
Output: 6
Explanation: B = [2,8]
Example 3:
Input: A = [1,3,6], K = 3
Output: 0
Explanation: B = [3,3,3] or B = [4,4,4]
Note:
1 <= A.length <= 10000
0 <= A[i] <= 10000
0 <= K <= 10000
思路:
很朴素的想法,扫描一趟即可知道最大值与最小值,然后再比较一下即可。
int smallestRangeI(vector<int>& A, int K)
{
int minV = A[], maxV = A[];
for(int i = ; i < A.size(); i++)
{
minV = min(minV, A[i]);
maxV = max(maxV, A[i]);
}
return max(((maxV - minV) - * K),);
}
最新文章
- .NET面试题系列[9] - IEnumerable
- 【开源】OSharp框架解说系列(5.2):EntityFramework数据层实现
- Android Studio--学习系列(2)
- C++设计模式-Strategy策略模式
- Python3基础 使用技巧:把代码的字体变大
- WP8__从windowsphone app store 中根据app id获取应用的相关信息(下载网址及图片id等)
- Graylog2+mongdb+rsyslog中央日志服务器对syslog的web管理--转载
- Chrome浏览器跨域问题
- 为UITextView添加与UITextField一样的边框——UITextField默认边框颜色、宽度、圆角
- 【Xamarin挖墙脚系列:现有IPhone/IPad 设备尺寸】
- HDU 5651 xiaoxin juju needs help 水题一发
- YII中文件上传
- Dev表格导出工具类 z
- 判断mysqli函数里的sql语句是否有错和影响行数
- 楼天城楼教主的acm心路历程(作为励志用)
- redhat6.3+oracle11GR2 单库 安装规划
- Nagios监控生产环境redis群集服务战
- MySQL意外关闭, 导致软件崩溃而无法启动的解决办法
- 在非controllers中获取httpServletRequest
- 【转】C++ Vector(向量容器)