Problem
You are given an array of positive integers nums
.
Alice and Bob are playing a game. In the game, Alice can choose either all single-digit numbers or all double-digit numbers from nums
, and the rest of the numbers are given to Bob. Alice wins if the sum of her numbers is strictly greater than the sum of Bob’s numbers.
Return true
if Alice can win this game, otherwise, return false
.
Example 1:
Input: nums = [1,2,3,4,10]
Output: false
Explanation:
Alice cannot win by choosing either single-digit or double-digit numbers.
Example 2:
Input: nums = [1,2,3,4,5,14]
Output: true
Explanation:
Alice can win by choosing single-digit numbers which have a sum equal to 15.
Example 3:
Input: nums = [5,5,5,25]
Output: true
Explanation:
Alice can win by choosing double-digit numbers which have a sum equal to 25.
Constraints:
1 <= nums.length <= 100
1 <= nums[i] <= 99
Solution
First attempt : Time complexity O(n)
/**
* @param {number[]} nums
* @return {boolean}
*/
var canAliceWin = function(nums) {
let doubledigitsum = 0;
let singledigitsum = 0;
for(let i = 0; i < nums.length; i++){
if(nums[i] < 10){
singledigitsum = singledigitsum + nums[i];
}
else{
doubledigitsum = doubledigitsum + nums[i];
}
}
console.log(singledigitsum, doubledigitsum);
if(singledigitsum == doubledigitsum){
return false;
}
return true;
};
A freelance web developer with a decade of experience in creating high-quality, scalable web solutions. His expertise spans PHP, WordPress, Node.js, MySQL, MongoDB, and e-commerce development, ensuring a versatile approach to each project. Aadi’s commitment to client satisfaction is evident in his track record of over 200 successful projects, marked by innovation, efficiency, and a customer-centric philosophy.
As a professional who values collaboration and open communication, Aadi has built a reputation for delivering projects that exceed expectations while adhering to time and budget constraints. His proactive and problem-solving mindset makes him an ideal partner for anyone looking to navigate the digital landscape with a reliable and skilled developer.