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 e…
贪心:尽量将gas[i]-cost[i]>0的放在前面,gas[i]-cost[i]<0的放在后面.(路程的前面有汽油剩下,耗汽油的放在路程的后面). 能否全程通过的 条件 是:sum(gas[i])>=sum(cost[i]),不满足这个条件就不能全程走一遍: 起点 i 满足的 必要 条件 是:额外剩下的gas(additional_gas)要大于等于0,即 gas[i]-cost[i]>=0: 另外需满足: 找出某站点 i_save ,经过这个站点剩下的gas最多,找出某站点…
题目描述 在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升. 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升.你从其中的一个加油站出发,开始时油箱为空. 如果你可以绕环路行驶一周,则返回出发时加油站的编号,否则返回 -1. 说明: 如果题目有解,该答案即为唯一答案. 输入数组均为非空数组,且长度相同. 输入数组中的元素均为非负数. 示例 1: 输入: gas = [1,2,3,4,5] cost = [3,4,5…
[题目] 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…
本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie Best Time to Buy and Sell Stock Total Accepted: 13234 Total Submissions: 43145 Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to…
贪心算法中,是以自顶向下的方式使用最优子结构,贪心算法会先做选择,在当时看起来是最优的选择,然后再求解一个结果的子问题. 贪心算法是使所做的选择看起来都是当前最佳的,期望通过所做的局部最优选择来产生一个全局最优解 如最小生成树.Dijkstra单源最短路径 动态规划:https://www.cnblogs.com/AntonioSu/p/11864508.html 分治算法:https://www.cnblogs.com/AntonioSu/p/11865159.html…
桌上有 n 堆力扣币,每堆的数量保存在数组 coins 中.我们每次可以选择任意一堆,拿走其中的一枚或者两枚,求拿完所有力扣币的最少次数. 示例 1: 输入:[4,2,1] 输出:4 解释:第一堆力扣币最少需要拿 2 次,第二堆最少需要拿 1 次,第三堆最少需要拿 1 次,总共 4 次即可拿完. 示例 2: 输入:[2,3,10] 输出:8 限制: 1 <= n <= 4 1 <= coins[i] <= 10 来源:力扣(LeetCode) 链接:https://leetcode…
用最少数量的箭引爆气球 力扣题目链接(opens new window) 在二维空间中有许多球形的气球.对于每个气球,提供的输入是水平方向上,气球直径的开始和结束坐标.由于它是水平的,所以纵坐标并不重要,因此只要知道开始和结束的横坐标就足够了.开始坐标总是小于结束坐标. 一支弓箭可以沿着 x 轴从不同点完全垂直地射出.在坐标 x 处射出一支箭,若有一个气球的直径的开始和结束坐标为 xstart,xend, 且满足 xstart ≤ x ≤ xend,则该气球会被引爆.可以射出的弓箭的数量没有限制…
监控二叉树 力扣题目链接(opens new window) 给定一个二叉树,我们在树的节点上安装摄像头. 节点上的每个摄影头都可以监视其父对象.自身及其直接子对象. 计算监控树的所有节点所需的最小摄像头数量. 示例 1: 输入:[0,0,null,0,0] 输出:1 解释:如图所示,一台摄像头足以监控所有节点. 示例 2: 输入:[0,0,null,0,null,0,null,null,0] 输出:2 解释:需要至少两个摄像头来监视树的所有节点. 上图显示了摄像头放置的有效位置之一. 提示:…
1. jump game Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. For…