【leetcode】【easy】【每日一题】面试题 17.16. The Masseuse LCCI​​​​​​​

230 篇文章 0 订阅

面试题 17.16. The Masseuse LCCI

A popular masseuse receives a sequence of back-to-back appointment requests and is debating which ones to accept. She needs a break between appointments and therefore she cannot accept any adjacent requests. Given a sequence of back-to-back appoint­ ment requests, find the optimal (highest total booked minutes) set the masseuse can honor. Return the number of minutes.

Note: This problem is slightly different from the original one in the book.

Example 1:

Input:  [1,2,3,1]
Output:  4
Explanation:  Accept request 1 and 3, total minutes = 1 + 3 = 4

Example 2:

Input:  [2,7,9,3,1]
Output:  12
Explanation:  Accept request 1, 3 and 5, total minutes = 2 + 9 + 1 = 12

Example 3:

Input:  [2,1,4,5,3,1,1,3]
Output:  12
Explanation:  Accept request 1, 3, 5 and 8, total minutes = 2 + 4 + 3 + 3 = 12

题目链接:https://leetcode-cn.com/problems/the-masseuse-lcci/

 

思路

和匪徒问题一个思路,对当前值取和不取 两个值的dp。

class Solution {
public:
    int massage(vector<int>& nums) {
        int len = nums.size();
        if(len==0) return 0;
        if( len==1) return nums[0];
        int no = 0, yes = nums[0];
        for(int i=1; i<len; ++i){
            int no_tmp = max(yes, no);
            yes = no+nums[i];
            no = no_tmp;
        }
        return max(yes,no);
    }
};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值