当前位置:网站首页>365天挑战LeetCode1000题——Day 038 公交站间的距离 + 基于时间的键值存储 + 转变数组后最接近目标值的数组和 + 有界数组中指定下标处的最大值
365天挑战LeetCode1000题——Day 038 公交站间的距离 + 基于时间的键值存储 + 转变数组后最接近目标值的数组和 + 有界数组中指定下标处的最大值
2022-07-29 05:08:00 【ShowM3TheCode】
文章目录
1184. 公交站间的距离
代码实现(自解)
class Solution {
public:
int distanceBetweenBusStops(vector<int>& distance, int start, int destination) {
int n = 0;
int total = 0;
for (int i = 0; i < distance.size(); i++) {
if ((i + start) % distance.size() == destination) n = total;
total += distance[(i + start) % distance.size()];
}
return min(total - n, n);
}
};
981. 基于时间的键值存储
代码实现(自解)
class TimeMap {
public:
map<string, vector<pair<int, string>>> _map;
TimeMap() {
}
void set(string key, string value, int timestamp) {
_map[key].push_back(make_pair(timestamp, value));
}
string get(string key, int timestamp) {
if (!_map.count(key)) return "";
vector<pair<int, string>>& v = _map[key];
if (v[0].first > timestamp) return "";
int l = 0, r = v.size() - 1;
while (l < r) {
int m = (l + r + 1) >> 1;
if (v[m].first <= timestamp) l = m;
else r = m - 1;
}
return v[l].second;
}
};
/** * Your TimeMap object will be instantiated and called as such: * TimeMap* obj = new TimeMap(); * obj->set(key,value,timestamp); * string param_2 = obj->get(key,timestamp); */
1300. 转变数组后最接近目标值的数组和
代码实现(自解)
class Solution {
public:
int findBestValue(vector<int>& arr, int target) {
int n = arr.size();
if (n >= 2 * target) return 0;
int l = 0, r = *max_element(arr.begin(), arr.end());
int bestDiff = INT_MAX;
int bestVal = INT_MAX;
while (l < r) {
int m = (l + r + 1) >> 1;
int sum = 0;
for (int num : arr) {
sum += min(num, m);
}
if (abs(sum - target) < bestDiff) {
bestVal = m;
bestDiff = abs(sum - target);
}
else if (abs(sum - target) == bestDiff) {
bestVal = min(m, bestVal);
}
if (sum <= target) l = m;
else r = m - 1;
}
return bestVal;
}
};
1802. 有界数组中指定下标处的最大值
代码实现(自解)
class Solution {
public:
int maxValue(int n, int index, int maxSum) {
int l = 1, r = maxSum;
while (l < r) {
int m = (l + r + 1) >> 1;
long long sum = 0;
if (m - index > 0) {
sum += (long long) (2 * m - index) * (index + 1) / 2;
}
else {
sum += (long long) (m + 1) * m / 2 + index - m + 1;
}
if (m + index + 1 - n > 0) {
sum += (long long) (2 * m + index - n) * (n - index - 1) / 2;
}
else {
sum += (long long) m * (m - 1) / 2 + n - index - m;
}
if (sum <= maxSum) l = m;
else r = m - 1;
}
return l;
}
};
边栏推荐
- Xiaobai high salary shortcut Qt development game Snake
- 最新坦克大战2022-全程开发笔记-2
- "Invisible Bridge" built in the free trade economy: domestic products and Chinese AI power
- osgSimplegl3结合RenderDoc工具
- ARFoundation从零开始3-创建ARFoundation项目
- 基于注解的三层项目的改造及添加包扫描的方式
- Database course design of online assistant teaching platform for high school chemistry
- Getting started with solidity
- CMake 设置vs启动运行环境路径
- How mongodb inserts, deletes and updates documents
猜你喜欢
小白高薪捷径-Qt开发游戏—贪吃蛇
【2022新生学习】第三周要点
向往的开源之多YOUNG新生 | 从开源到就业的避坑指南来啦!
Activity workflow table structure learning
ARFoundation从零开始9-AR锚点(AR Anchor)
Qt版的贪食蛇游戏项目
AI应用第一课:C语言支付宝刷脸登录
6.2 function-parameters
This article takes you to understand the implementation of surround notification @around and final notification @after
Google GTEST event mechanism
随机推荐
Soft link & hard link
C语言连连看秒杀辅助
TCP三次握手四次挥手
自定义Qml控件:ImageButton
SQL log
The latest tank battle 2022 full development notes-1
"Invisible Bridge" built in the free trade economy: domestic products and Chinese AI power
关于thymeleaf的配置与使用
Visual Basic .Net 如何获取命令参数
pytorch学习笔记
三层项目的架构分析及构造方法的参数名称注入
小白高薪捷径-Qt开发游戏—贪吃蛇
How to install Office2010 installation package? How to install Office2010 installation package on computer
Arfoundation starts from scratch 3- create an arfoundation project
Rolabelimg to data format data
ARFoundation从零开始8-Geospatial API(地理空间)开发
[2022 freshmen learning] key points of the third week
Adb常用命令列表
This article takes you to understand the implementation of surround notification @around and final notification @after
Qt版的贪食蛇游戏项目