当前位置:网站首页>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 .
边栏推荐
猜你喜欢
03-存储系统
Kubernets pod exists finalizers are always in terminating state
一篇文章搞懂Go语言中的Context
关于FPGA底层资源的细节问题
都在说DevOps,你真正了解它吗?
对话龙智高级咨询顾问、Atlassian认证专家叶燕秀:Atlassian产品进入后Server时代,中国用户应当何去何从?
flutter 报错 No MediaQuery widget ancestor found.
Force button brush question 01 (reverse linked list + sliding window +lru cache mechanism)
Introduction to modern control theory + understanding
大神详解开源 BUFF 增益攻略丨直播
随机推荐
UFO: Microsoft scholars have proposed a unified transformer for visual language representation learning to achieve SOTA performance on multiple multimodal tasks
Ffprobe common commands
Unity脚本API—Time类
Unity预制件Prefab Day04
Quick introduction to automatic control principle + understanding
go-zero微服务实战系列(九、极致优化秒杀性能)
深度学习 神经网络的优化方法
How did the beyond concert 31 years ago get super clean and repaired?
Guitar Pro 8win10最新版吉他学习 / 打谱 / 创作
Analysis of nearly 100 million dollars stolen and horizon cross chain bridge attacked
mysql 联合主键_Mysql 创建联合主键[通俗易懂]
函数计算异步任务能力介绍 - 任务触发去重
Five minutes of machine learning every day: how to use matrix to represent the sample data of multiple characteristic variables?
Quelles sont les perspectives de l'Internet intelligent des objets (aiot) qui a explosé ces dernières années?
AI做题水平已超过CS博士?
信号处理之一阶RC低通滤波器宏指令实现(繁易触摸屏)
对话龙智高级咨询顾问、Atlassian认证专家叶燕秀:Atlassian产品进入后Server时代,中国用户应当何去何从?
找数字
Openresty redirection
[Dalian University of technology] information sharing of postgraduate entrance examination and re examination