返回介绍

solution / 3000-3099 / 3038.Maximum Number of Operations With the Same Score I / README_EN

发布于 2024-06-17 01:02:57 字数 3971 浏览 0 评论 0 收藏 0

3038. Maximum Number of Operations With the Same Score I

中文文档

Description

Given an array of integers called nums, you can perform the following operation while nums contains at least 2 elements:

  • Choose the first two elements of nums and delete them.

The score of the operation is the sum of the deleted elements.

Your task is to find the maximum number of operations that can be performed, such that all operations have the same score.

Return _the maximum number of operations possible that satisfy the condition mentioned above_.

 

Example 1:

Input: nums = [3,2,1,4,5]
Output: 2
Explanation: We perform the following operations:
- Delete the first two elements, with score 3 + 2 = 5, nums = [1,4,5].
- Delete the first two elements, with score 1 + 4 = 5, nums = [5].
We are unable to perform any more operations as nums contain only 1 element.

Example 2:

Input: nums = [3,2,6,1,4]
Output: 1
Explanation: We perform the following operations:
- Delete the first two elements, with score 3 + 2 = 5, nums = [6,1,4].
We are unable to perform any more operations as the score of the next operation isn't the same as the previous one.

 

Constraints:

  • 2 <= nums.length <= 100
  • 1 <= nums[i] <= 1000

Solutions

Solution 1: Traversal

First, we calculate the sum of the first two elements, denoted as $s$. Then we traverse the array, taking two elements at a time. If their sum is not equal to $s$, we stop the traversal. Finally, we return the number of operations performed.

The time complexity is $O(n)$, where $n$ is the length of the array $nums$. The space complexity is $O(1)$.

class Solution:
  def maxOperations(self, nums: List[int]) -> int:
    s = nums[0] + nums[1]
    ans, n = 0, len(nums)
    for i in range(0, n, 2):
      if i + 1 == n or nums[i] + nums[i + 1] != s:
        break
      ans += 1
    return ans
class Solution {
  public int maxOperations(int[] nums) {
    int s = nums[0] + nums[1];
    int ans = 0, n = nums.length;
    for (int i = 0; i + 1 < n && nums[i] + nums[i + 1] == s; i += 2) {
      ++ans;
    }
    return ans;
  }
}
class Solution {
public:
  int maxOperations(vector<int>& nums) {
    int s = nums[0] + nums[1];
    int ans = 0, n = nums.size();
    for (int i = 0; i + 1 < n && nums[i] + nums[i + 1] == s; i += 2) {
      ++ans;
    }
    return ans;
  }
};
func maxOperations(nums []int) (ans int) {
  s, n := nums[0]+nums[1], len(nums)
  for i := 0; i+1 < n && nums[i]+nums[i+1] == s; i += 2 {
    ans++
  }
  return
}
function maxOperations(nums: number[]): number {
  const s = nums[0] + nums[1];
  const n = nums.length;
  let ans = 0;
  for (let i = 0; i + 1 < n && nums[i] + nums[i + 1] === s; i += 2) {
    ++ans;
  }
  return ans;
}

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

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

发布评论

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