返回介绍

lcci / 17.16.The Masseuse / README_EN

发布于 2024-06-17 01:04:42 字数 2673 浏览 0 评论 0 收藏 0

17.16. The Masseuse

中文文档

Description

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

Solutions

Solution 1

class Solution:
  def massage(self, nums: List[int]) -> int:
    f = g = 0
    for x in nums:
      f, g = g + x, max(f, g)
    return max(f, g)
class Solution {
  public int massage(int[] nums) {
    int f = 0, g = 0;
    for (int x : nums) {
      int ff = g + x;
      int gg = Math.max(f, g);
      f = ff;
      g = gg;
    }
    return Math.max(f, g);
  }
}
class Solution {
public:
  int massage(vector<int>& nums) {
    int f = 0, g = 0;
    for (int& x : nums) {
      int ff = g + x;
      int gg = max(f, g);
      f = ff;
      g = gg;
    }
    return max(f, g);
  }
};
func massage(nums []int) int {
  f, g := 0, 0
  for _, x := range nums {
    f, g = g+x, max(f, g)
  }
  return max(f, g)
}
function massage(nums: number[]): number {
  let f = 0,
    g = 0;
  for (const x of nums) {
    const ff = g + x;
    const gg = Math.max(f, g);
    f = ff;
    g = gg;
  }
  return Math.max(f, g);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文