当前位置:网站首页>365 day challenge leetcode1000 question - distance between bus stops on day 038 + time-based key value storage + array closest to the target value after transforming the array and + maximum value at t
365 day challenge leetcode1000 question - distance between bus stops on day 038 + time-based key value storage + array closest to the target value after transforming the array and + maximum value at t
2022-07-29 05:25:00 【ShowM3TheCode】
List of articles
1184. The distance between bus stops

Code implementation ( Self solution )
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. Time based key value storage
Code implementation ( Self solution )

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. After transforming the array, the array closest to the target value and

Code implementation ( Self solution )
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. The maximum value at the specified subscript in a bounded array

Code implementation ( Self solution )
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;
}
};
边栏推荐
- ARFoundation从零开始3-创建ARFoundation项目
- Live broadcast preview | how to improve enterprise immunity through "intelligent edge security"?
- C 语言手写 QQ-AI 版
- Helm chart for Kubernetes
- ARFoundation从零开始9-AR锚点(AR Anchor)
- C语言 N皇后问题
- 如视技术副总裁杨永林:当传统产业遇到“数字空间”
- vs2019编译cryengine失败问题处理
- [event preview] cloud development, efficient and intelligent - the second Alibaba cloud ECS cloudbuild developer competition is about to start
- Webrtc audio anti weak network technology (Part 2)
猜你喜欢

This article takes you to understand the implementation of surround notification @around and final notification @after

如视技术副总裁杨永林:当传统产业遇到“数字空间”

321, Jingdong Yanxi × Nlpcc 2022 challenge starts!

C语言用指向指针的指针对n个整数排序

AI应用第一课:C语言支付宝刷脸登录

C语言宏#define命令练习

The latest tank battle 2022 - Notes on the whole development -2

OCCT学习003-----MFC单文档工程

Webrtc audio anti weak network technology (Part 2)

More than 200 ISVs have settled in! The first anniversary of Alibaba cloud computing nest
随机推荐
[from_bilibili_DR_CAN][【Advanced控制理论】9_状态观测器设计][学习记录]
Container security open source detection tool - veinmind (mirror backdoor, malicious samples, sensitive information, weak password, etc.)
ARFoundation从零开始5-AR图像跟踪
321,京东言犀×NLPCC 2022挑战赛开赛!
The latest tank battle 2022 - full development notes-3
365天挑战LeetCode1000题——Day 039 完全二叉树插入器 + 寻找峰值 II + 快照数组
QT学习:使用JSON/XML等非ts文件实现多语言国际化
CryEngine3 调试Shader方法
QT learning: qdropevent drag event
CMU15-213 Shell Lab实验记录
QT series - Installation
Architecture analysis of three-tier project and parameter name injection of construction method
【赛事预告】云上开发,高效智能——第二届阿里云ECS CloudBuild开发者大赛即将启动
ARFoundation入门教程10-平面检测和放置
200 多家 ISV 入驻!阿里云计算巢发布一周年
Unity3d - the object is too far away to see
2022数学建模竞赛暑期培训讲座——最优化方法:目标规划
osg进阶-序
CryEngine5 Shader调试
C how to realize simple factory mode