当前位置:网站首页>LeetCode 1184. Distance between bus stops -- vector clockwise and counterclockwise
LeetCode 1184. Distance between bus stops -- vector clockwise and counterclockwise
2022-07-04 15:13:00 【TianChao lobster】
LeetCode 1184. The distance between bus stops —vector Clockwise and counterclockwise .
Title address : https://leetcode.cn/problems/distance-between-bus-stops/
Problem solving :
class Solution {
public:
int distanceBetweenBusStops(vector<int>& distance, int start, int destination) {
int result_clockwise=0, result_counterclockwise=0;
int size=distance.size();
for(int i=0;(start+i)%size!=destination;++i)
result_clockwise+=distance[(start+i)%size];
for(int i=0;(destination+i)%size!=start;++i)
result_counterclockwise+=distance[(destination+i)%size];
return min(result_clockwise,result_counterclockwise);
}
};
Topic notes
The key point of the topic is to give a vector, A start and end , Calculate the cumulative sum respectively , Take the minimum . How to put vector The index is divided into two parts by the following statement :
(start+i)%n
(destination+i)%n
One accumulates from the beginning , Then on vector Size redundancy of , Until we reach the end . One Accumulate from the end , Also on the vector Size redundancy of , Until we reach the starting point . This remainder is very clever to put a vector Divided into two parts , Whether the end point is greater than the starting point , And greater than vector When it comes to the size of , Will automatically index to the front .
边栏推荐
- Analysis of nearly 100 million dollars stolen and horizon cross chain bridge attacked
- 宽度精度
- Huawei cloud database DDS products are deeply enabled
- How to handle exceptions in multithreading?
- 十六进制
- AI做题水平已超过CS博士?
- Luo Gu - some interesting questions
- 力扣刷题01(反转链表+滑动窗口+LRU缓存机制)
- Live broadcast preview | PostgreSQL kernel Interpretation Series II: PostgreSQL architecture
- LNX efficient search engine, fastdeploy reasoning deployment toolbox, AI frontier paper | showmeai information daily # 07.04
猜你喜欢

中国主要城市人均存款出炉,你达标了吗?

Five minutes of machine learning every day: how to use matrix to represent the sample data of multiple characteristic variables?

Five minutes of machine learning every day: why do we need to normalize the characteristics of numerical types?

SAIC Maxus officially released its new brand "mifa", and its flagship product mifa 9 was officially unveiled!

UFO:微软学者提出视觉语言表征学习的统一Transformer,在多个多模态任务上达到SOTA性能!...

Helix swarm Chinese package is released, and perforce further improves the user experience in China

Introduction to asynchronous task capability of function calculation - task trigger de duplication

Intelligent customer service track: Netease Qiyu and Weier technology play different ways

Helix Swarm中文包发布,Perforce进一步提升中国用户体验

Redis publish and subscribe
随机推荐
PLC模拟量输入 模拟量转换FC S_ITR (CODESYS平台)
openresty 重定向
%s格式符
The performance of major mainstream programming languages is PK, and the results are unexpected
Luo Gu - some interesting questions 2
Guitar Pro 8win10最新版吉他学习 / 打谱 / 创作
When synchronized encounters this thing, there is a big hole, pay attention!
Quick introduction to automatic control principle + understanding
Unity预制件Prefab Day04
函数计算异步任务能力介绍 - 任务触发去重
【学习笔记】拟阵
怎么判断外盘期货平台正规,资金安全?
小数,指数
LNX efficient search engine, fastdeploy reasoning deployment toolbox, AI frontier paper | showmeai information daily # 07.04
中国主要城市人均存款出炉,你达标了吗?
Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing
产品好不好,谁说了算?Sonar提出分析的性能指标,帮助您轻松判断产品性能及表现
flutter 报错 No MediaQuery widget ancestor found.
Helix swarm Chinese package is released, and perforce further improves the user experience in China
31年前的Beyond演唱会,是如何超清修复的?