2.1.21—线性表—Gas Station
来源:互联网 发布:mac groovy sdk 编辑:程序博客网 时间:2024/06/14 07:46
描述
There are N gas stations along a circular route, where the amount of gas at station i is gas[i].
You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its
next station (i+1). You begin the journey with an empty tank at one of the gas stations.
Return the starting gas station’s index if you can travel around the circuit once, otherwise return -1.
There are N gas stations along a circular route, where the amount of gas at station i is gas[i].
You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its
next station (i+1). You begin the journey with an empty tank at one of the gas stations.
Return the starting gas station’s index if you can travel around the circuit once, otherwise return -1.
Note: e solution is guaranteed to be unique.
#include<iostream>using namespace std;int GasStation(int gas[], int cost[], int n){int left = 0;int index = 0;int sum = 0;for (int i = 0; i < n; i++){left += gas[i] - cost[i];sum += gas[i] - cost[i];if (left < 0){index = i;left = 0;}}if (sum < 0)return -1;elsereturn index+1;}int main(){const int n = 6;int gas[n] = { 2, 4, 7, 8, 6, 2 };int cost[n] = { 100, 5, 3, 6, 5, 1 };int index = GasStation(gas, cost, n);cout << index << endl;}
阅读全文
0 0
- 2.1.21—线性表—Gas Station
- 2.1.21 Gas Station
- LeetCode134—Gas Station
- LeetCode 2.1.21 Gas Station
- LeetCode——Gas Station
- LeetCode——Gas Station
- leetcode——Gas Station
- LeetCode134——Gas Station
- Algorithms—134.Gas Station
- 2.1.20 Gas Station
- 21: Gas Station
- LeetCode OJ——Gas Station
- leetcode 134 —— Gas Station
- 循环数组——gas station
- Leetcode——134 Gas Station
- 算法系列——Gas Station
- leetcode——134——Gas Station
- Gas Station——Leetcode系列(十六)
- CC2640R2F BLE5.0 蓝牙协议栈通用访问配置文件(GAP)
- Ubuntu下制作deb包的方法详解
- 总结H5前端优化
- 数组排序的几种方法
- python
- 2.1.21—线性表—Gas Station
- Unity,UI实现滚动内容
- Java 9的前9大改进和功能,程序员不妨先了解
- 使用kibana来进行ElasticSearch的信息查询检索
- spring 生命周期最详解
- DOM中的动态NodeList与静态NodeList(getElementsByTagName()与querySelectorAll())
- C++学习笔记之类
- (4)全局变量、局部变量、常见对象(Object、String)、自定义方法
- Android之MPAndroidChart库——双柱状图,X轴显示汉字