返回介绍

solution / 2700-2799 / 2708.Maximum Strength of a Group / README_EN

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

2708. Maximum Strength of a Group

中文文档

Description

You are given a 0-indexed integer array nums representing the score of students in an exam. The teacher would like to form one non-empty group of students with maximal strength, where the strength of a group of students of indices i0, i1, i2, ... , ik is defined as nums[i0] * nums[i1] * nums[i2] * ... * nums[ik​].

Return _the maximum strength of a group the teacher can create_.

 

Example 1:

Input: nums = [3,-1,-5,2,5,-9]
Output: 1350
Explanation: One way to form a group of maximal strength is to group the students at indices [0,2,3,4,5]. Their strength is 3 * (-5) * 2 * 5 * (-9) = 1350, which we can show is optimal.

Example 2:

Input: nums = [-4,-5,-4]
Output: 20
Explanation: Group the students at indices [0, 1] . Then, we’ll have a resulting strength of 20. We cannot achieve greater strength.

 

Constraints:

  • 1 <= nums.length <= 13
  • -9 <= nums[i] <= 9

Solutions

Solution 1

class Solution:
  def maxStrength(self, nums: List[int]) -> int:
    nums.sort()
    n = len(nums)
    if n == 1:
      return nums[0]
    if nums[1] == nums[-1] == 0:
      return 0
    ans, i = 1, 0
    while i < n:
      if nums[i] < 0 and i + 1 < n and nums[i + 1] < 0:
        ans *= nums[i] * nums[i + 1]
        i += 2
      elif nums[i] <= 0:
        i += 1
      else:
        ans *= nums[i]
        i += 1
    return ans
class Solution {
  public long maxStrength(int[] nums) {
    Arrays.sort(nums);
    int n = nums.length;
    if (n == 1) {
      return nums[0];
    }
    if (nums[1] == 0 && nums[n - 1] == 0) {
      return 0;
    }
    long ans = 1;
    int i = 0;
    while (i < n) {
      if (nums[i] < 0 && i + 1 < n && nums[i + 1] < 0) {
        ans *= nums[i] * nums[i + 1];
        i += 2;
      } else if (nums[i] <= 0) {
        i += 1;
      } else {
        ans *= nums[i];
        i += 1;
      }
    }
    return ans;
  }
}
class Solution {
public:
  long long maxStrength(vector<int>& nums) {
    sort(nums.begin(), nums.end());
    int n = nums.size();
    if (n == 1) {
      return nums[0];
    }
    if (nums[1] == 0 && nums[n - 1] == 0) {
      return 0;
    }
    long long ans = 1;
    int i = 0;
    while (i < n) {
      if (nums[i] < 0 && i + 1 < n && nums[i + 1] < 0) {
        ans *= nums[i] * nums[i + 1];
        i += 2;
      } else if (nums[i] <= 0) {
        i += 1;
      } else {
        ans *= nums[i];
        i += 1;
      }
    }
    return ans;
  }
};
func maxStrength(nums []int) int64 {
  sort.Ints(nums)
  n := len(nums)
  if n == 1 {
    return int64(nums[0])
  }
  if nums[1] == 0 && nums[n-1] == 0 {
    return 0
  }
  ans := int64(1)
  for i := 0; i < n; i++ {
    if nums[i] < 0 && i+1 < n && nums[i+1] < 0 {
      ans *= int64(nums[i] * nums[i+1])
      i++
    } else if nums[i] > 0 {
      ans *= int64(nums[i])
    }
  }
  return ans
}
function maxStrength(nums: number[]): number {
  nums.sort((a, b) => a - b);
  const n = nums.length;
  if (n === 1) {
    return nums[0];
  }
  if (nums[1] === 0 && nums[n - 1] === 0) {
    return 0;
  }
  let ans = 1;
  for (let i = 0; i < n; ++i) {
    if (nums[i] < 0 && i + 1 < n && nums[i + 1] < 0) {
      ans *= nums[i] * nums[i + 1];
      ++i;
    } else if (nums[i] > 0) {
      ans *= nums[i];
    }
  }
  return ans;
}

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

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

发布评论

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