Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Written by Prashant Basnet
👋 Welcome to my Signature, a space between logic and curiosity.
I’m a Software Development Engineer who loves turning ideas into systems that work beautifully.
This space captures the process: the bugs, breakthroughs, and “aha” moments that keep me building.
You are given a non-negative integer array nums. In one operation, you must:
Example 1:
Example 2:
First Approach:
Action: 1. choose a minim num 2. substract it 3. choose another minimum number 4. substract it 5. choose minimum 5. substract it It's a recursive operation, we can utlize a dynicamic programming herefunction findMin(array) { let min = Infinity for (let i = 0; i < array.length; i++) { if (0 < array[i]) { min = Math.min(min, array[i]) } } return min }function reducer(array) { let min = findMin(array) let max = Math.max(...array) if (max === 0) { return count } for (let i = 0; i < array.length; i++) { if (0 < array[i]) { array[i] -= min } } count++ reducer(array) } reducer(nums) return countAnalysis:
The quadratic complexity comes from:
Why Unique Elements = Operations:
Key Point:
var minimumOperations = function(nums) { nums.sort((a, b) => a -b) let prev = 0 let count = 0 for(let i = 0; i < nums.length; i++){ if(prev<nums[i]){ count++ prev = nums[i] } } return count };var minimumOperations = function(nums) { const set = new Set(nums) return set.has(0)? set.size - 1 : set.size };