Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 0000c47

Browse files
committed
Add solution #2598
1 parent e235c8b commit 0000c47

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1942,6 +1942,7 @@
19421942
2595|[Number of Even and Odd Bits](./solutions/2595-number-of-even-and-odd-bits.js)|Easy|
19431943
2596|[Check Knight Tour Configuration](./solutions/2596-check-knight-tour-configuration.js)|Medium|
19441944
2597|[The Number of Beautiful Subsets](./solutions/2597-the-number-of-beautiful-subsets.js)|Medium|
1945+
2598|[Smallest Missing Non-negative Integer After Operations](./solutions/2598-smallest-missing-non-negative-integer-after-operations.js)|Medium|
19451946
2600|[K Items With the Maximum Sum](./solutions/2600-k-items-with-the-maximum-sum.js)|Easy|
19461947
2601|[Prime Subtraction Operation](./solutions/2601-prime-subtraction-operation.js)|Medium|
19471948
2605|[Form Smallest Number From Two Digit Arrays](./solutions/2605-form-smallest-number-from-two-digit-arrays.js)|Easy|
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* 2598. Smallest Missing Non-negative Integer After Operations
3+
* https://leetcode.com/problems/smallest-missing-non-negative-integer-after-operations/
4+
* Difficulty: Medium
5+
*
6+
* You are given a 0-indexed integer array nums and an integer value.
7+
*
8+
* In one operation, you can add or subtract value from any element of nums.
9+
* - For example, if nums = [1,2,3] and value = 2, you can choose to subtract value from nums[0]
10+
* to make nums = [-1,2,3].
11+
*
12+
* The MEX (minimum excluded) of an array is the smallest missing non-negative integer in it.
13+
* - For example, the MEX of [-1,2,3] is 0 while the MEX of [1,0,3] is 2.
14+
*
15+
* Return the maximum MEX of nums after applying the mentioned operation any number of times.
16+
*/
17+
18+
/**
19+
* @param {number[]} nums
20+
* @param {number} value
21+
* @return {number}
22+
*/
23+
var findSmallestInteger = function(nums, value) {
24+
const remainderCount = new Map();
25+
26+
for (const num of nums) {
27+
const remainder = ((num % value) + value) % value;
28+
remainderCount.set(remainder, (remainderCount.get(remainder) || 0) + 1);
29+
}
30+
31+
for (let mex = 0; mex <= nums.length; mex++) {
32+
const remainder = mex % value;
33+
if (!remainderCount.has(remainder) || remainderCount.get(remainder) === 0) {
34+
return mex;
35+
}
36+
remainderCount.set(remainder, remainderCount.get(remainder) - 1);
37+
}
38+
39+
return nums.length;
40+
};

0 commit comments

Comments
 (0)