返回介绍

solution / 1900-1999 / 1953.Maximum Number of Weeks for Which You Can Work / README_EN

发布于 2024-06-17 01:03:12 字数 4648 浏览 0 评论 0 收藏 0

1953. Maximum Number of Weeks for Which You Can Work

中文文档

Description

There are n projects numbered from 0 to n - 1. You are given an integer array milestones where each milestones[i] denotes the number of milestones the ith project has.

You can work on the projects following these two rules:

  • Every week, you will finish exactly one milestone of one project. You must work every week.
  • You cannot work on two milestones from the same project for two consecutive weeks.

Once all the milestones of all the projects are finished, or if the only milestones that you can work on will cause you to violate the above rules, you will stop working. Note that you may not be able to finish every project's milestones due to these constraints.

Return _the maximum number of weeks you would be able to work on the projects without violating the rules mentioned above_.

 

Example 1:

Input: milestones = [1,2,3]
Output: 6
Explanation: One possible scenario is:
​​​​- During the 1st week, you will work on a milestone of project 0.
- During the 2nd week, you will work on a milestone of project 2.
- During the 3rd week, you will work on a milestone of project 1.
- During the 4th week, you will work on a milestone of project 2.
- During the 5th week, you will work on a milestone of project 1.
- During the 6th week, you will work on a milestone of project 2.
The total number of weeks is 6.

Example 2:

Input: milestones = [5,2,1]
Output: 7
Explanation: One possible scenario is:
- During the 1st week, you will work on a milestone of project 0.
- During the 2nd week, you will work on a milestone of project 1.
- During the 3rd week, you will work on a milestone of project 0.
- During the 4th week, you will work on a milestone of project 1.
- During the 5th week, you will work on a milestone of project 0.
- During the 6th week, you will work on a milestone of project 2.
- During the 7th week, you will work on a milestone of project 0.
The total number of weeks is 7.
Note that you cannot work on the last milestone of project 0 on 8th week because it would violate the rules.
Thus, one milestone in project 0 will remain unfinished.

 

Constraints:

  • n == milestones.length
  • 1 <= n <= 105
  • 1 <= milestones[i] <= 109

Solutions

Solution 1

class Solution:
  def numberOfWeeks(self, milestones: List[int]) -> int:
    mx, s = max(milestones), sum(milestones)
    rest = s - mx
    return rest * 2 + 1 if mx > rest + 1 else s
class Solution {
  public long numberOfWeeks(int[] milestones) {
    int mx = 0;
    long s = 0;
    for (int e : milestones) {
      s += e;
      mx = Math.max(mx, e);
    }
    long rest = s - mx;
    return mx > rest + 1 ? rest * 2 + 1 : s;
  }
}
class Solution {
public:
  long long numberOfWeeks(vector<int>& milestones) {
    int mx = *max_element(milestones.begin(), milestones.end());
    long long s = accumulate(milestones.begin(), milestones.end(), 0LL);
    long long rest = s - mx;
    return mx > rest + 1 ? rest * 2 + 1 : s;
  }
};
func numberOfWeeks(milestones []int) int64 {
  mx := slices.Max(milestones)
  s := 0
  for _, x := range milestones {
    s += x
  }
  rest := s - mx
  if mx > rest+1 {
    return int64(rest*2 + 1)
  }
  return int64(s)
}

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

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

发布评论

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