Skip to content

Commit 02b1213

Browse files
committed
Add neetcode discourse video LC easy solution
1 parent 98e443b commit 02b1213

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

src/leetcode/easy/easy.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- [Climbing Stairs](climbing-stairs)
1111
- [Contains Duplicate](contains-duplicate)
1212
- [Counting Bits](counting-bits)
13+
- [Find Minimum Operations to Make All Elements Divide by Three](find-minimum-operations-to-make-all-elements-divide-by-three)
1314
- [Invert Binary Tree](invert-binary-tree)
1415
- [Is Palindrome](is-palindrome)
1516
- [Is Same Tree](is-same-tree)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
https://leetcode.com/problems/find-minimum-operations-to-make-all-elements-divisible-by-three
3+
4+
You are given an integer array nums. In one operation, you can add or subtract 1 from any element of nums.
5+
6+
Return the minimum number of operations to make all elements of nums divisible by 3.
7+
8+
Example 1:
9+
Input: nums = [1,2,3,4]
10+
Output: 3
11+
12+
Explanation:
13+
All array elements can be made divisible by 3 using 3 operations:
14+
Subtract 1 from 1.
15+
Add 1 to 2.
16+
Subtract 1 from 4.
17+
18+
Example 2:
19+
Input: nums = [3,6,9]
20+
Output: 0
21+
22+
Constraints:
23+
1 <= nums.length <= 50
24+
1 <= nums[i] <= 50
25+
*/
26+
27+
function minimumOperations(nums) {
28+
let ops = 0;
29+
30+
nums.forEach((num) => {
31+
if ([1, 2].includes(num % 3)) {
32+
ops += 1;
33+
}
34+
});
35+
36+
return ops;
37+
}
38+
39+
module.exports = { minimumOperations };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const {
2+
minimumOperations,
3+
} = require("./findMinimumOperationsToMakeAllElementsDivideByThree");
4+
5+
describe("minimumOperations()", () => {
6+
test("returns the minimum number of operations to make all elements divisible by 3", () => {
7+
expect(minimumOperations([1, 2, 3, 4])).toBe(3);
8+
});
9+
10+
test("returns the minimum number of operations to make all elements divisible by 3", () => {
11+
expect(minimumOperations([3, 6, 9])).toBe(0);
12+
});
13+
});

0 commit comments

Comments
 (0)