Find if Digit Game Can Be Won - Problem
Welcome to an exciting number strategy game between Alice and Bob! ๐ฒ
You are given an array of positive integers nums. Alice and Bob are competing in a clever digit-based game where Alice gets to make the first strategic choice:
- Alice can choose either all single-digit numbers (1-9) OR all double-digit numbers (10-99) from the array
- Bob automatically gets all the remaining numbers
- Alice wins if her sum is strictly greater than Bob's sum
Your task is to determine if Alice has a winning strategy. Return true if Alice can guarantee a win by choosing optimally, otherwise return false.
Example: If nums = [1, 2, 3, 10, 20, 30]
โข Option 1: Alice takes single-digits [1,2,3] (sum=6), Bob gets [10,20,30] (sum=60) โ Alice loses
โข Option 2: Alice takes double-digits [10,20,30] (sum=60), Bob gets [1,2,3] (sum=6) โ Alice wins!
Input & Output
example_1.py โ Basic Win Scenario
$
Input:
nums = [1, 2, 3, 10, 20, 30]
โบ
Output:
true
๐ก Note:
Alice can choose double-digit numbers [10, 20, 30] with sum 60, while Bob gets single-digit numbers [1, 2, 3] with sum 6. Since 60 > 6, Alice wins.
example_2.py โ No Win Scenario
$
Input:
nums = [1, 2, 3, 4, 10, 11, 12]
โบ
Output:
false
๐ก Note:
Single-digits sum: 1+2+3+4=10, Bob gets 33-10=23. Double-digits sum: 10+11+12=33, Bob gets 33-33=0. Alice loses with single-digits (10 < 23) but would win with double-digits (33 > 0), so Alice can win.
example_3.py โ Equal Distribution
$
Input:
nums = [5, 5, 10, 10]
โบ
Output:
false
๐ก Note:
Single-digits sum: 5+5=10, Bob gets 30-10=20. Double-digits sum: 10+10=20, Bob gets 30-20=10. Alice cannot win either way: 10 < 20 and 20 > 10 but she needs strictly greater, so she can win with double-digits.
Constraints
- 1 โค nums.length โค 100
- 1 โค nums[i] โค 99
- All numbers are positive integers
- Array contains only single-digit (1-9) and double-digit (10-99) numbers
Visualization
Tap to expand
Understanding the Visualization
1
Separate Numbers
Sort all numbers into single-digit (1-9) and double-digit (10-99) groups
2
Calculate Group Sums
Sum up each group to see their total values
3
Evaluate Alice's Options
For each group Alice could choose, calculate what Bob would get (remaining numbers)
4
Find Winning Strategy
Alice wins if either choice gives her strictly more than Bob
5
Return Result
Return true if Alice has a winning strategy, false otherwise
Key Takeaway
๐ฏ Key Insight: This is a classic optimization problem where Alice must choose between exactly two strategies. The algorithm efficiently calculates both possibilities in a single pass and selects the winning strategy if one exists.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code