File tree 2 files changed +60
-0
lines changed
2 files changed +60
-0
lines changed Original file line number Diff line number Diff line change 837
837
2535|[ Difference Between Element Sum and Digit Sum of an Array] ( ./2535-difference-between-element-sum-and-digit-sum-of-an-array.js ) |Easy|
838
838
2542|[ Maximum Subsequence Score] ( ./2542-maximum-subsequence-score.js ) |Medium|
839
839
2559|[ Count Vowel Strings in Ranges] ( ./2559-count-vowel-strings-in-ranges.js ) |Medium|
840
+ 2560|[ House Robber IV] ( ./2560-house-robber-iv.js ) |Medium|
840
841
2570|[ Merge Two 2D Arrays by Summing Values] ( ./2570-merge-two-2d-arrays-by-summing-values.js ) |Easy|
841
842
2579|[ Count Total Number of Colored Cells] ( ./2579-count-total-number-of-colored-cells.js ) |Medium|
842
843
2618|[ Check if Object Instance of Class] ( ./2618-check-if-object-instance-of-class.js ) |Medium|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 2560. House Robber IV
3
+ * https://leetcode.com/problems/house-robber-iv/
4
+ * Difficulty: Medium
5
+ *
6
+ * There are several consecutive houses along a street, each of which has some money inside.
7
+ * There is also a robber, who wants to steal money from the homes, but he refuses to steal
8
+ * from adjacent homes.
9
+ *
10
+ * The capability of the robber is the maximum amount of money he steals from one house of
11
+ * all the houses he robbed.
12
+ *
13
+ * You are given an integer array nums representing how much money is stashed in each house.
14
+ * More formally, the ith house from the left has nums[i] dollars.
15
+ *
16
+ * You are also given an integer k, representing the minimum number of houses the robber will
17
+ * steal from. It is always possible to steal at least k houses.
18
+ *
19
+ * Return the minimum capability of the robber out of all the possible ways to steal at least
20
+ * k houses.
21
+ */
22
+
23
+ /**
24
+ * @param {number[] } nums
25
+ * @param {number } k
26
+ * @return {number }
27
+ */
28
+ var minCapability = function ( nums , k ) {
29
+ let left = Math . min ( ...nums ) ;
30
+ let right = Math . max ( ...nums ) ;
31
+
32
+ while ( left < right ) {
33
+ const mid = Math . floor ( ( left + right ) / 2 ) ;
34
+
35
+ if ( canRob ( nums , mid , k ) ) {
36
+ right = mid ;
37
+ } else {
38
+ left = mid + 1 ;
39
+ }
40
+ }
41
+
42
+ return left ;
43
+ } ;
44
+
45
+ function canRob ( nums , capability , k ) {
46
+ let count = 0 ;
47
+ let i = 0 ;
48
+
49
+ while ( i < nums . length ) {
50
+ if ( nums [ i ] <= capability ) {
51
+ count ++ ;
52
+ i += 2 ;
53
+ } else {
54
+ i ++ ;
55
+ }
56
+ }
57
+
58
+ return count >= k ;
59
+ }
You can’t perform that action at this time.
0 commit comments