File tree 2 files changed +41
-0
lines changed
2 files changed +41
-0
lines changed Original file line number Diff line number Diff line change 792
792
2161|[ Partition Array According to Given Pivot] ( ./2161-partition-array-according-to-given-pivot.js ) |Medium|
793
793
2185|[ Counting Words With a Given Prefix] ( ./2185-counting-words-with-a-given-prefix.js ) |Easy|
794
794
2215|[ Find the Difference of Two Arrays] ( ./2215-find-the-difference-of-two-arrays.js ) |Easy|
795
+ 2226|[ Maximum Candies Allocated to K Children] ( ./2226-maximum-candies-allocated-to-k-children.js ) |Medium|
795
796
2235|[ Add Two Integers] ( ./2235-add-two-integers.js ) |Easy|
796
797
2244|[ Minimum Rounds to Complete All Tasks] ( ./2244-minimum-rounds-to-complete-all-tasks.js ) |Medium|
797
798
2270|[ Number of Ways to Split Array] ( ./2270-number-of-ways-to-split-array.js ) |Medium|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 2226. Maximum Candies Allocated to K Children
3
+ * https://leetcode.com/problems/maximum-candies-allocated-to-k-children/
4
+ * Difficulty: Medium
5
+ *
6
+ * You are given a 0-indexed integer array candies. Each element in the array denotes a pile of
7
+ * candies of size candies[i]. You can divide each pile into any number of sub piles, but you
8
+ * cannot merge two piles together.
9
+ *
10
+ * You are also given an integer k. You should allocate piles of candies to k children such that
11
+ * each child gets the same number of candies. Each child can be allocated candies from only one
12
+ * pile of candies and some piles of candies may go unused.
13
+ *
14
+ * Return the maximum number of candies each child can get.
15
+ */
16
+
17
+ /**
18
+ * @param {number[] } candies
19
+ * @param {number } k
20
+ * @return {number }
21
+ */
22
+ var maximumCandies = function ( candies , k ) {
23
+ let left = 0 ;
24
+ let right = 1e7 + 1 ;
25
+
26
+ while ( left + 1 !== right ) {
27
+ const middle = Math . floor ( ( left + right ) / 2 ) ;
28
+ let piles = 0 ;
29
+ for ( const candy of candies ) {
30
+ piles += Math . floor ( candy / middle ) ;
31
+ }
32
+ if ( piles >= k ) {
33
+ left = middle ;
34
+ } else {
35
+ right = middle ;
36
+ }
37
+ }
38
+
39
+ return left ;
40
+ } ;
You can’t perform that action at this time.
0 commit comments