File tree 2 files changed +39
-0
lines changed
2 files changed +39
-0
lines changed Original file line number Diff line number Diff line change 294
294
372|[ Super Pow] ( ./0372-super-pow.js ) |Medium|
295
295
373|[ Find K Pairs with Smallest Sums] ( ./0373-find-k-pairs-with-smallest-sums.js ) |Medium|
296
296
374|[ Guess Number Higher or Lower] ( ./0374-guess-number-higher-or-lower.js ) |Medium|
297
+ 375|[ Guess Number Higher or Lower II] ( ./0375-guess-number-higher-or-lower-ii.js ) |Medium|
297
298
378|[ Kth Smallest Element in a Sorted Matrix] ( ./0378-kth-smallest-element-in-a-sorted-matrix.js ) |Medium|
298
299
380|[ Insert Delete GetRandom O(1)] ( ./0380-insert-delete-getrandom-o1.js ) |Medium|
299
300
383|[ Ransom Note] ( ./0383-ransom-note.js ) |Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 375. Guess Number Higher or Lower II
3
+ * https://leetcode.com/problems/guess-number-higher-or-lower-ii/
4
+ * Difficulty: Medium
5
+ *
6
+ * We are playing the Guessing Game. The game will work as follows:
7
+ * 1. I pick a number between 1 and n.
8
+ * 2. You guess a number.
9
+ * 3. If you guess the right number, you win the game.
10
+ * 4. If you guess the wrong number, then I will tell you whether the number I picked is higher
11
+ * or lower, and you will continue guessing.
12
+ * 5. Every time you guess a wrong number x, you will pay x dollars. If you run out of money,
13
+ * you lose the game.
14
+ *
15
+ * Given a particular n, return the minimum amount of money you need to guarantee a win
16
+ * regardless of what number I pick.
17
+ */
18
+
19
+ /**
20
+ * @param {number } n
21
+ * @return {number }
22
+ */
23
+ var getMoneyAmount = function ( n ) {
24
+ const dp = new Array ( n + 1 ) . fill ( ) . map ( ( ) => new Array ( n + 1 ) . fill ( 0 ) ) ;
25
+
26
+ for ( let i = 2 ; i <= n ; i ++ ) {
27
+ for ( let j = 1 ; j <= n - i + 1 ; j ++ ) {
28
+ const k = j + i - 1 ;
29
+ let min = Infinity ;
30
+ for ( let pivot = j ; pivot < k ; pivot ++ ) {
31
+ min = Math . min ( min , pivot + Math . max ( dp [ j ] [ pivot - 1 ] , dp [ pivot + 1 ] [ k ] ) ) ;
32
+ }
33
+ dp [ j ] [ k ] = min ;
34
+ }
35
+ }
36
+
37
+ return dp [ 1 ] [ n ] ;
38
+ } ;
You can’t perform that action at this time.
0 commit comments