当前位置:网站首页>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;
}
};
边栏推荐
猜你喜欢
随机推荐
321,京东言犀×NLPCC 2022挑战赛开赛!
阿里云架构师梁旭:MES on 云盒,助力客户快速构建数字工厂
OCCT学习002-----环境搭建
研发效能生态完整图谱&DevOps工具选型必看
英伟达周锡健:设计到数字营销的最后一公里
The latest tank battle 2022 - full development notes-3
京东云联合Forrester咨询发布混合云报告 云原生成为驱动产业发展新引擎
Helm chart for Kubernetes
Live broadcast Preview: integration of JD cloud Devops and jfrog product library
Qml类型:MouseArea
QT系列---安装
Ros1 dead chicken data is stored in txt and SQLite
Scikit learn -- steps and understanding of machine learning application development
MySQL的详细安装使用教程(保姆式安装图文讲解)
2022年泰迪杯数据挖掘挑战赛C题方案及赛后总结
2022数学建模竞赛暑期培训讲座——最优化方法:目标规划
Come on! See how Clickhouse, which has risen 16 places a year, can be implemented in jd.com
京东云分布式链路追踪在金融场景的最佳实践
Unity3d - the object is too far away to see
C language handwritten qq-ai version









