**Leetcode 134. Gas Station

2024-05-28 03:58
文章标签 leetcode 134 gas station

本文主要是介绍**Leetcode 134. Gas Station,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

https://leetcode.com/problems/gas-station/description/

两个结论还是很容易猜到的:

(1) sum(gas[i] - cost[i]) >=0的时候才能跑完

(2)假设最小的sum(gas[i] - cost[i])位置是k,那么应该把k+1作为起点

(3)我不是从(2)的角度考虑的 我是从最大子段和的角度考虑,具有最大子段和的位置应该作为起点。然后看是不是每个位置都能保证到达。

关于(1)(2)的证明:https://leetcode.com/problems/gas-station/discuss/42572/Proof-of-%22if-total-gas-is-greater-than-total-cost-there-is-a-solution%22.-C++

写的挺好的

We prove the following statement.
If sum of all gas[i]-cost[i] is greater than or equal to 0, then there is a start position you can travel the whole circle.
Let i be the index such that the the partial sum

gas[0]-cost[0]+gas[1]-cost[1]+...+gas[i]-cost[i]

is the smallest, then the start position should be start=i+1 ( start=0 if i=n-1). Consider any other partial sum, for example,

gas[0]-cost[0]+gas[1]-cost[1]+...+gas[i]-cost[i]+gas[i+1]-cost[i+1]

Since gas[0]-cost[0]+gas[1]-cost[1]+...+gas[i]-cost[i] is the smallest, we must have

gas[i+1]-cost[i+1]>=0

in order for gas[0]-cost[0]+gas[1]-cost[1]+...+gas[i]-cost[i]+gas[i+1]-cost[i+1] to be greater.
The same reasoning gives that

 gas[i+1]-cost[i+1]>=0gas[i+1]-cost[i+1]+gas[i+2]-cost[i+2]>=0.......gas[i+1]-cost[i+1]+gas[i+2]-cost[i+2]+...+gas[n-1]-cost[n-1]>=0

What about for the partial sums that wraps around?

gas[0]-cost[0]+gas[1]-cost[1]+...+gas[j]-cost[j] + gas[i+1]-cost[i+1]+...+gas[n-1]-cost[n-1]
>=
gas[0]-cost[0]+gas[1]-cost[1]+...+gas[i]-cost[i] + gas[i+1]-cost[i+1]+...+gas[n-1]-cost[n-1]
>=0

The last inequality is due to the assumption that the entire sum of gas[k]-cost[k] is greater than or equal to 0.
So we have that all the partial sums

gas[i+1]-cost[i+1]>=0,
gas[i+1]-cost[i+1]+gas[i+2]-cost[i+2]>=0,
gas[i+1]-cost[i+1]+gas[i+2]-cost[i+2]+...+gas[n-1]-cost[n-1]>=0,
...
gas[i+1]-cost[i+1]+...+gas[n-1]-cost[n-1] + gas[0]-cost[0]+gas[1]-cost[1]+...+gas[j]-cost[j]>=0,
...

Thus i+1 is the position to start. Coding using this reasoning is as follows:

class Solution {
public:int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {int n = gas.size();int total(0), subsum(INT_MAX), start(0);for(int i = 0; i < n; ++i){total += gas[i] - cost[i];if(total < subsum) {subsum = total;start = i + 1;}}return (total < 0) ?  -1 : (start%n); }
};


我的AC代码:

class Solution {
public:int get_idx(int pos, int n) {return (pos + n) % n;}int get_st_pos(vector<int>& gas, vector<int>& cost) {int min_v = gas[0] - cost[0], min_idx = 0;for (int i = 1; i < gas.size(); i++) {int tmp = gas[i] - cost[i];if (tmp < min_v) {min_v = tmp;min_idx = i;}}int st = get_idx(min_idx+1, gas.size());int ans = gas[st] - cost[st], sum_st = st, sum_en = st, sum = gas[st] - cost[st];int last_st = st, idx = st;for (int i = st; i < st + gas.size(); i++) {idx = get_idx(i, gas.size());if (sum < 0) {sum = gas[idx] - cost[idx];last_st = idx;} if (sum > ans) {sum_st = last_st;sum_en = idx;ans = sum;}}return sum_st;}int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {if (gas.size() <= 1) return gas[0] >= cost[0] ? 0 : -1;int st = get_st_pos(gas, cost), idx = 0, max_pos = 0, max_v = 0;int dp[gas.size() + 1];for (int i = st; i <= st + gas.size(); i++) {idx = get_idx(i, gas.size());if ( i == st ) {dp[i] = gas[i];continue;}if ( i == st + gas.size() ) {dp[idx] = dp[get_idx(i-1, gas.size())] - cost[ get_idx(i-1, gas.size()) ];if (dp[idx] < 0) return -1;continue;}dp[idx] = dp[get_idx(i-1, gas.size())] - cost[ get_idx(i-1, gas.size()) ] ;if (dp[idx] < 0) return -1;dp[idx] += + gas[ idx ];}return st;}
};




这篇关于**Leetcode 134. Gas Station的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1009478

相关文章

LeetCode--231 2的幂

题目 给定一个整数,编写一个函数来判断它是否是 2 的幂次方。 示例 示例 1:输入: 1输出: true解释: 20 = 1示例 2:输入: 16输出: true解释: 24 = 16示例 3:输入: 218输出: false class Solution {public:bool isPowerOfTwo(int n) {if (n <= 0) return fals

LeetCode--234 回文链表

题目 请判断一个链表是否为回文链表。 示例 示例 1:输入: 1->2输出: false示例 2:输入: 1->2->2->1输出: true /*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode(int x) : val

LeetCode--220 存在重复元素 III

题目 给定一个整数数组,判断数组中是否有两个不同的索引 i 和 j,使得 nums [i] 和 nums [j] 的差的绝对值最大为 t,并且 i 和 j 之间的差的绝对值最大为 ķ。 示例 示例 1:输入: nums = [1,2,3,1], k = 3, t = 0输出: true示例 2:输入: nums = [1,0,1,1], k = 1, t = 2输出: true示例

LeetCode--217 存在重复元素

题目 给定一个整数数组,判断是否存在重复元素。如果任何值在数组中出现至少两次,函数返回 true。如果数组中每个元素都不相同,则返回 false。 示例 示例 1:输入: [1,2,3,1]输出: true示例 2:输入: [1,2,3,4]输出: false示例 3:输入: [1,1,1,3,3,4,3,2,4,2]输出: true class Solution {p

LeetCode--214 最短回文串

题目 给定一个字符串 s,你可以通过在字符串前面添加字符将其转换为回文串。找到并返回可以用这种方式转换的最短回文串。 示例 示例 1:输入: "aacecaaa"输出: "aaacecaaa"示例 2:输入: "abcd"输出: "dcbabcd" 思路: 我们需要添加多少个字符与给定字符串的前缀子串回文的长度有关. 也就是说去掉其前缀的回文子串,我们只需要补充剩下的子串的逆序

LeetCode--206 反转链表

题目 反转一个单链表。 示例 示例:输入: 1->2->3->4->5->NULL输出: 5->4->3->2->1->NULL class Solution {public:ListNode* reverseList(ListNode* head) {if (head == nullptr || head->next == nullptr){return head;}ListNo

LeetCode--204 计数质数

题目 统计所有小于非负整数 n 的质数的数量。 示例 示例:输入: 10输出: 4解释: 小于 10 的质数一共有 4 个, 它们是 2, 3, 5, 7 。 class Solution {public:int countPrimes(int n) {if (n <= 2) return 0;int cnt = 0;vector<bool> isPrime(n, true);

LeetCode--198 打家劫舍

题目 你是一个专业的小偷,计划偷窃沿街的房屋。每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警。给定一个代表每个房屋存放金额的非负整数数组,计算你在不触动警报装置的情况下,能够偷窃到的最高金额。 示例 示例 1:输入: [1,2,3,1]输出: 4解释: 偷窃 1 号房屋 (金额 =

LeetCode--171 Excel表列序号

题目 给定一个Excel表格中的列名称,返回其相应的列序号。例如,A -> 1B -> 2C -> 3...Z -> 26AA -> 27AB -> 28 ... 示例 示例 1:输入: "A"输出: 1示例 2:输入: "AB"输出: 28示例 3:输入: "ZY"输出: 701 class Solution {public:int titleToNumber(strin

LeetCode--155 最小栈

题目 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈。push(x) -- 将元素 x 推入栈中。pop() -- 删除栈顶的元素。top() -- 获取栈顶元素。getMin() -- 检索栈中的最小元素。 示例 MinStack minStack = new MinStack();minStack.push(-2);minStack.push