当前位置:网站首页>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;
}
};
边栏推荐
- QML custom tabbar
- The road to success in R & D efficiency of 1000 person Internet companies
- CMU15-213 Shell Lab实验记录
- 为啥谷歌的内部工具不适合你?
- R & D efficiency | analysis of kubernetes' core technology and Devops' landing experience
- Qt版的贪食蛇游戏项目
- Why is Google's internal tools not suitable for you?
- With frequent data leakage and deletion events, how should enterprises build a security defense line?
- Helm chart for Kubernetes
- CryEngine技术
猜你喜欢

C语言求字符串的长度

6.2 function-parameters

QML control: combobox

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

On AspectJ framework

Live broadcast preview | how to improve enterprise immunity through "intelligent edge security"?

MySQL的基础概念+数据库系统结构+拓展延申+基础命令学习

研发效能|Kubernetes核心技术剖析和DevOps落地经验

Helm chart for Kubernetes

阿里云架构师梁旭:MES on 云盒,助力客户快速构建数字工厂
随机推荐
WDDM学习
AiTalk创始人梁宇淇:镜像连接虚拟与现实的纽带
如视技术副总裁杨永林:当传统产业遇到“数字空间”
研发效能生态完整图谱&DevOps工具选型必看
7.2-function-overloading
During the appointment, the 2022 JD cloud industrial integration new product launch was launched online
Xiaolu Inn - Trailer
整数溢出和打印
最新坦克大战2022-全程开发笔记-2
This article takes you to understand the implementation of surround notification @around and final notification @after
ARFoundation从零开始9-AR锚点(AR Anchor)
On AspectJ framework
SQL log
365天挑战LeetCode1000题——Day 039 完全二叉树插入器 + 寻找峰值 II + 快照数组
365天挑战LeetCode1000题——Day 040 设计跳表 + 避免洪水泛滥 + 查找大小为 M 的最新分组 + 销售价值减少的颜色球
QML control: combobox
Alibaba cloud architect Liang Xu: MES on cloud box helps customers quickly build digital factories
Thousands of databases, physical machines all over the country, JD logistics full volume cloud live record | interview with excellent technical team
Do you remember the process analysis and function implementation of post notification?
QT学习:QDropEvent拖拽事件