Leetcode三题:#120、#55、#134

#120 Triangle

题目描述

Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.

For example, given the following triangle

[
     [2],
    [3,4],
   [6,5,7],
  [4,1,8,3]
]

The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11).

Note:
Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle.

题目大意

有一个数字三角形,找出顶端到底端路径上数字和的最小值。

解题思路

设F(i, j)为第i行第j个元素到底端路径的最小值,由于每次要么往左下走,要么往右下走,则F(i, j) = min(F(i+1, j), F(i+1, j+1)) + num[i],注意此题不能用类似贪心算法的思路去做。下面附上通过的代码:

class Solution(object):
    def minimumTotal(self, triangle):
        """
        :type triangle: List[List[int]]
        :rtype: int
        """
        for i in range(len(triangle)-2, -1, -1):
            for j in range(i + 1):
                triangle[i][j] += min(triangle[i+1][j], triangle[i+1][j+1]);
        return triangle[0][0];

#55 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.

Example 1:

Input: [2,3,1,1,4]
Output: true
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.

Example 2:

Input: [3,2,1,0,4]
Output: false
Explanation: You will always arrive at index 3 no matter what. Its maximum
             jump length is 0, which makes it impossible to reach the last index.

题目大意

给出一个数组,每个位置上的数代表从这个位置上跳出的最长距离,问能否从第一个位置跳到最后一个位置。

解题思路

初看此题,我想到的方法是开一个布尔数组来储存每个位置能否到达,通过模拟算法(每到一个位置就把它能到达的地方置为True),遍历整个数组后检查最后一位的状态,这种算法最坏情况下的复杂度为O(n^2)。后面我想到了一种更好的方法,每次只需维护能到达的最后位置,因为每一跳能到达的区间是连续的,如果能跳到某个位置,那它之前的都能跳到,下面给出改进后的代码。

class Solution(object):
    def canJump(self, nums):
        """
        :type nums: List[int]
        :rtype: bool
        """
        p = 0
        for i in range(len(nums)):
            if i > p:
                return False
            if p >= len(nums) - 1:
                return True
            if i + nums[i] > p:
                p = i + nums[i]

#134 Gas Station

题目描述

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 in the clockwise direction, otherwise return -1.

Note:
If there exists a solution, it is guaranteed to be unique.
Both input arrays are non-empty and have the same length.
Each element in the input arrays is a non-negative integer.

Example 1:

Input: 
gas  = [1,2,3,4,5]
cost = [3,4,5,1,2]
Output: 3
Explanation:
Start at station 3 (index 3) and fill up with 4 unit of gas. Your tank = 0 + 4 = 4
Travel to station 4. Your tank = 4 - 1 + 5 = 8
Travel to station 0. Your tank = 8 - 2 + 1 = 7
Travel to station 1. Your tank = 7 - 3 + 2 = 6
Travel to station 2. Your tank = 6 - 4 + 3 = 5
Travel to station 3. The cost is 5. Your gas is just enough to travel back to station 3.
Therefore, return 3 as the starting index.

Example 2:

Input: 
gas  = [2,3,4]
cost = [3,4,3]
Output: -1
Explanation:
You can't start at station 0 or 1, as there is not enough gas to travel to the next station.
Let's start at station 2 and fill up with 4 unit of gas. Your tank = 0 + 4 = 4
Travel to station 0. Your tank = 4 - 3 + 2 = 3
Travel to station 1. Your tank = 3 - 3 + 3 = 3
You cannot travel back to station 2, as it requires 4 unit of gas but you only have 3.
Therefore, you can't travel around the circuit once no matter where you start.

题目大意

一段环形路段上有一些加油站,每个加油站里储存着一些油,给出行驶相邻加油站间路程所需要的油量,问一辆刚开始没有油车从某个加油站出发能否回到起点位置,如果能给出起点位置,否则返回-1。

解题思路

这题的朴素思想是以每个加油站为起点都试一遍,复杂度为O(n^2),但实际上,如果以某个加油站i为起点,能行驶到i+1,i+2,…,i+k-1,但是行驶不到i+k,那我们下一步只需要从以i+k为起点考虑就行了。假设以i+1为起点,因为i号加油站中的油肯定大于i到i+1的消耗,如果去掉这一段,只会导致在i+k-1时的油量更少,从而也到达不了i+k,所以下一步只需考虑起点为i+k的情况。

class Solution(object):
    def canCompleteCircuit(self, gas, cost):
        """
        :type gas: List[int]
        :type cost: List[int]
        :rtype: int
        """
        totalGas = 0
        start = 0
        less = 0
        for i in range(len(gas)):
            totalGas += gas[i] - cost[i]
            if totalGas < 0:
                start = i + 1
                less += totalGas
                totalGas = 0
        if totalGas + less < 0:
            return -1
        else:
            return start
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_36326096/article/details/80301717
个人分类: 程序设计基础
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭