leetcode face questions 17.16. masseur dynamic programming two dimensions thief Series 198 213 337

leetcode face questions 17.16. masseur dynamic programming two dimensions thief Series 198 213 337


leetcode 2020 March 1 question daily punch
programmer interview Golden
similar question: Thief series (leetcode198 213 337) 198 robberies

topic:

A known massage therapist will receive a steady stream of the reservation request, the reservation can be selected for each connection or not connected. Between each reservation service have time to rest, so she could not accept neighboring appointment. Given a sequence of reservation request, the reservation for the massage therapist to find the optimal set (longest total reservation), and returns the total number of minutes. Note: This problem is relatively minor changes to the original title

Example 1:
Input: [1,2,3,1]
Output: 4
Explanation: selecting a reservation number and reservation number 3, total length = 3 + 1 = 4.
Example 2:
Input: [2,7,9,3,1]
Output: 12
Explanation: selecting a reservation number, No. 3 and No. 5 reservation booking, total length = 2 + 9 + 1 = 12.
Example 3:
Input: [2,1,4,5,3,1,1,3]
Output: 12
Explanation: 1 selection number reservation, reservation No. 3, No. 5 and No. 8 reservation booking, total length = 2 + 4 + 3 + 3 = 12.

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/the-masseuse-lcci

method: Dynamic programming (two dimensions), Thief series (leetcode198 213 337)

Two dimension dynamic programming, dp [i] [0] indicates the i-th reservation does not contact the maximum reservation time, dp [i] [1] indicates the i-th maximum reservation time reservation contact. Dp value calculated from front to back, a front Suppose we have calculated dp i-1 th values, are calculated dp [i] [0] and dp [i] [1]. State transition equation: dp [i] [0] = max (dp [i-1] [0], dp [i-1] [1]), p [i] [1] = dp [i-1] [ 0] + nums [i]. When (calculated dp [i] [0/1], the previous state only dp [i-1] [0/1] related, so we can not open the array, only two variables DP0, DP1 store dp [ i-1] [0] and answers dp [i-1] [1], and then transferred to the answer to the update.) time complexityO (n)Space complexityO (1)

Code:

class Solution(object):
    def massage(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        # 两维度动态规划
        if nums==[]:
            return 0
        dp0=0
        dp1=nums[0]
        for num in nums[1:]:
            predp0=dp0
            dp0=max(dp0,dp1)
            dp1=predp0+num
        
        return max(dp0,dp1)

thought:

  1. Dynamic programming steps: design condition - determining a state transition equation of - determining an initial value (boundary value) - determining an output - (whether compressible state)
  2. By increasing the dimensions, eliminating the after-effects of the operation of the dynamic programming problem was very common. No aftereffect of understanding: 1 behind the decision will not affect the previous decision; 2, how come before the state is not important. The power button a few stock questions are the basic ideas, but ideas and setting state of this question is exactly the same.
Published 25 original articles · won praise 1 · views 283

Guess you like

Origin blog.csdn.net/weixin_43973433/article/details/105065668