当前位置:网站首页>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 .
边栏推荐
- Helix swarm Chinese package is released, and perforce further improves the user experience in China
- Deep learning neural network case (handwritten digit recognition)
- How to rapidly deploy application software under SaaS
- MP3是如何诞生的?
- 这几年爆火的智能物联网(AIoT),到底前景如何?
- go-zero微服务实战系列(九、极致优化秒杀性能)
- Kubernets pod exists finalizers are always in terminating state
- numpy笔记
- [learning notes] matroid
- Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing
猜你喜欢
华为云数据库DDS产品深度赋能
Force button brush question 01 (reverse linked list + sliding window +lru cache mechanism)
Kubernets Pod 存在 Finalizers 一直处于 Terminating 状态
UFO: Microsoft scholars have proposed a unified transformer for visual language representation learning to achieve SOTA performance on multiple multimodal tasks
都在说DevOps,你真正了解它吗?
近一亿美元失窃,Horizon跨链桥被攻击事件分析
Introduction to asynchronous task capability of function calculation - task trigger de duplication
Redis 发布和订阅
Analysis of nearly 100 million dollars stolen and horizon cross chain bridge attacked
They are all talking about Devops. Do you really understand it?
随机推荐
31年前的Beyond演唱会,是如何超清修复的?
Implementation of web chat room
Analysis of nearly 100 million dollars stolen and horizon cross chain bridge attacked
%S format character
近一亿美元失窃,Horizon跨链桥被攻击事件分析
LeetCode 1200 最小絕對差[排序] HERODING的LeetCode之路
Dialogue with ye Yanxiu, senior consultant of Longzhi and atlassian certification expert: where should Chinese users go when atlassian products enter the post server era?
重排数组
Details of FPGA underlying resources
Unity脚本API—GameObject游戏对象、Object 对象
.Net 应用考虑x64生成
What are the concepts of union, intersection, difference and complement?
怎么判断外盘期货平台正规,资金安全?
Width and alignment
Live broadcast preview | PostgreSQL kernel Interpretation Series II: PostgreSQL architecture
They are all talking about Devops. Do you really understand it?
直播预告 | PostgreSQL 内核解读系列第二讲:PostgreSQL 体系结构
谈SaaS下如何迅速部署应用软件
Ffprobe common commands
%s格式符