Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 57ae428

Browse files
committedApr 25, 2025
Add solution #1655
1 parent 706d2d0 commit 57ae428

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,453 LeetCode solutions in JavaScript
1+
# 1,454 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1275,6 +1275,7 @@
12751275
1652|[Defuse the Bomb](./solutions/1652-defuse-the-bomb.js)|Easy|
12761276
1653|[Minimum Deletions to Make String Balanced](./solutions/1653-minimum-deletions-to-make-string-balanced.js)|Medium|
12771277
1654|[Minimum Jumps to Reach Home](./solutions/1654-minimum-jumps-to-reach-home.js)|Medium|
1278+
1655|[Distribute Repeating Integers](./solutions/1655-distribute-repeating-integers.js)|Hard|
12781279
1657|[Determine if Two Strings Are Close](./solutions/1657-determine-if-two-strings-are-close.js)|Medium|
12791280
1668|[Maximum Repeating Substring](./solutions/1668-maximum-repeating-substring.js)|Easy|
12801281
1669|[Merge In Between Linked Lists](./solutions/1669-merge-in-between-linked-lists.js)|Medium|
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* 1655. Distribute Repeating Integers
3+
* https://leetcode.com/problems/distribute-repeating-integers/
4+
* Difficulty: Hard
5+
*
6+
* You are given an array of n integers, nums, where there are at most 50 unique values in
7+
* the array. You are also given an array of m customer order quantities, quantity, where
8+
* quantity[i] is the amount of integers the ith customer ordered. Determine if it is possible
9+
* to distribute nums such that:
10+
* - The ith customer gets exactly quantity[i] integers,
11+
* - The integers the ith customer gets are all equal, and
12+
* - Every customer is satisfied.
13+
*
14+
* Return true if it is possible to distribute nums according to the above conditions.
15+
*/
16+
17+
/**
18+
* @param {number[]} nums
19+
* @param {number[]} quantity
20+
* @return {boolean}
21+
*/
22+
var canDistribute = function(nums, quantity) {
23+
const frequencyMap = new Map();
24+
for (const num of nums) {
25+
frequencyMap.set(num, (frequencyMap.get(num) || 0) + 1);
26+
}
27+
28+
const frequencies = Array.from(frequencyMap.values()).sort((a, b) => b - a);
29+
quantity.sort((a, b) => b - a);
30+
31+
return canSatisfy(0, frequencies);
32+
33+
function canSatisfy(index, counts) {
34+
if (index === quantity.length) return true;
35+
36+
for (let i = 0; i < counts.length; i++) {
37+
if (counts[i] >= quantity[index]) {
38+
counts[i] -= quantity[index];
39+
if (canSatisfy(index + 1, counts)) return true;
40+
counts[i] += quantity[index];
41+
}
42+
}
43+
44+
return false;
45+
}
46+
};

0 commit comments

Comments
 (0)
Please sign in to comment.