LeetCode 3232: Find if Digit Game Can Be Won (One-Digit vs Two-Digit Sum Comparison)
LeetCode 3232ArrayMathToday we solve LeetCode 3232 - Find if Digit Game Can Be Won.
Source: https://leetcode.com/problems/find-if-digit-game-can-be-won/
English
Problem Summary
Given an integer array nums, Alice may choose all one-digit numbers or all two-digit numbers. She wins if the chosen group sum is strictly greater than the other group sum. Return whether Alice can win.
Key Idea
There are only two relevant buckets: one-digit (< 10) and two-digit (>= 10, under this problem's constraints). Compute both sums once; Alice wins if they are not equal, because she can choose the larger-sum bucket.
Algorithm
1) Initialize sumOne = 0, sumTwo = 0.
2) Scan each number x in nums.
3) If x < 10, add to sumOne, else add to sumTwo.
4) Return sumOne != sumTwo.
Complexity
Single pass over array: O(n) time, O(1) extra space.
Reference Implementations (Java / Go / C++ / Python / JavaScript)
class Solution {
public boolean canAliceWin(int[] nums) {
int sumOne = 0, sumTwo = 0;
for (int x : nums) {
if (x < 10) sumOne += x;
else sumTwo += x;
}
return sumOne != sumTwo;
}
}func canAliceWin(nums []int) bool {
sumOne, sumTwo := 0, 0
for _, x := range nums {
if x < 10 {
sumOne += x
} else {
sumTwo += x
}
}
return sumOne != sumTwo
}class Solution {
public:
bool canAliceWin(vector<int>& nums) {
int sumOne = 0, sumTwo = 0;
for (int x : nums) {
if (x < 10) sumOne += x;
else sumTwo += x;
}
return sumOne != sumTwo;
}
};class Solution:
def canAliceWin(self, nums: list[int]) -> bool:
sum_one = 0
sum_two = 0
for x in nums:
if x < 10:
sum_one += x
else:
sum_two += x
return sum_one != sum_twovar canAliceWin = function(nums) {
let sumOne = 0, sumTwo = 0;
for (const x of nums) {
if (x < 10) sumOne += x;
else sumTwo += x;
}
return sumOne !== sumTwo;
};中文
题目概述
给定整数数组 nums。Alice 可以选择“所有一位数”或者“所有两位数”作为自己的得分集合。若所选集合元素和严格大于另一集合元素和,则 Alice 获胜。返回她是否能获胜。
核心思路
题目只比较两类数字:一位数与两位数。分别累加两组和后,只要两者不相等,Alice 就能选择较大和的那一组获胜;若相等则无法“严格大于”。
算法步骤
1)初始化 sumOne = 0、sumTwo = 0。
2)遍历数组中的每个数字 x。
3)若 x < 10,累加到 sumOne,否则累加到 sumTwo。
4)返回 sumOne != sumTwo。
复杂度分析
仅一次线性扫描,时间复杂度 O(n),额外空间复杂度 O(1)。
参考实现(Java / Go / C++ / Python / JavaScript)
class Solution {
public boolean canAliceWin(int[] nums) {
int sumOne = 0, sumTwo = 0;
for (int x : nums) {
if (x < 10) sumOne += x;
else sumTwo += x;
}
return sumOne != sumTwo;
}
}func canAliceWin(nums []int) bool {
sumOne, sumTwo := 0, 0
for _, x := range nums {
if x < 10 {
sumOne += x
} else {
sumTwo += x
}
}
return sumOne != sumTwo
}class Solution {
public:
bool canAliceWin(vector<int>& nums) {
int sumOne = 0, sumTwo = 0;
for (int x : nums) {
if (x < 10) sumOne += x;
else sumTwo += x;
}
return sumOne != sumTwo;
}
};class Solution:
def canAliceWin(self, nums: list[int]) -> bool:
sum_one = 0
sum_two = 0
for x in nums:
if x < 10:
sum_one += x
else:
sum_two += x
return sum_one != sum_twovar canAliceWin = function(nums) {
let sumOne = 0, sumTwo = 0;
for (const x of nums) {
if (x < 10) sumOne += x;
else sumTwo += x;
}
return sumOne !== sumTwo;
};
Comments