当前位置:网站首页>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;
}
};
边栏推荐
- About realizing page Jump of website in Servlet
- osg3.6.5编译freetype失败
- 7.2-function-overloading
- Qt版的贪食蛇游戏项目
- Mysql语句中的函数
- SM integration is as simple as before, and the steps are clear (detailed)
- Jackson parsing JSON detailed tutorial
- AttributeError: ‘module‘ object has no attribute ‘create_ connection‘
- The method and detailed code of automatically pop-up and QQ group when players visit the website
- sql日志
猜你喜欢
随机推荐
AI应用第一课:C语言支付宝刷脸登录
C how to realize simple factory mode
QtCreator+CMake编译器设置
Deep learning brush a bunch of tricks of SOTA
[file download] easyexcel quick start
Unity metaverse (III), protobuf & socket realize multi person online
Qml控件:ComboBox
C语言求字符串的长度
Mysql的自连接和联合查询
The method and detailed code of automatically pop-up and QQ group when players visit the website
osgSimplegl3结合RenderDoc工具
Solution | get the relevant information about the current employees' highest salary in each department |
Scikit learn -- steps and understanding of machine learning application development
pytorch学习笔记
Modification of annotation based three-tier project and the way of adding package scanning
osg进阶-序
"Invisible Bridge" built in the free trade economy: domestic products and Chinese AI power
QT学习:使用JSON/XML等非ts文件实现多语言国际化
How to install Office2010 installation package? How to install Office2010 installation package on computer
AUTOSAR from introduction to proficiency 100 lectures (78) -autosar-dem module









