Minimum Operations to Make Numbers Non-positive - Problem
You are given a 0-indexed integer array nums and two integers x and y.
In one operation, you must choose an index i such that 0 <= i < nums.length and perform the following:
- Decrement
nums[i]byx - Decrement values by
yat all indices except thei-th one
Return the minimum number of operations to make all the integers in nums less than or equal to zero.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [3,4,1], x = 3, y = 1
›
Output:
3
💡 Note:
Operation 1: Choose i=0, nums becomes [0,3,0]. Operation 2: Choose i=1, nums becomes [-1,0,-1]. Operation 3: Choose i=1, nums becomes [-2,-3,-2]. All numbers ≤ 0 after 3 operations.
Example 2 — Equal x and y
$
Input:
nums = [1,2,1], x = 2, y = 2
›
Output:
1
💡 Note:
Since x = y = 2, each operation decreases all numbers by 2. After 1 operation: nums becomes [-1,0,-1]. All numbers ≤ 0.
Example 3 — Large Difference
$
Input:
nums = [10], x = 5, y = 1
›
Output:
2
💡 Note:
Only one element. Operation 1: nums[0] becomes 5. Operation 2: nums[0] becomes 0. Need 2 operations to make 10 ≤ 0.
Constraints
- 1 ≤ nums.length ≤ 105
- 1 ≤ nums[i] ≤ 109
- 1 ≤ x, y ≤ 109
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array with positive numbers and operation parameters x, y
2
Operations
Each operation: decrease selected element by x, others by y
3
Goal
Make all numbers ≤ 0 with minimum operations
Key Takeaway
🎯 Key Insight: Binary search on the answer space and greedily verify feasibility for each candidate
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code