11package com .fishercoder .solutions ;
22
33/**
4+ * 375. Guess Number Higher or Lower II
5+ *
46 * We are playing the Guess Game. The game is as follows:
5-
6- I pick a number from 1 to n. You have to guess which number I picked.
7-
8- Every time you guess wrong, I'll tell you whether the number I picked is higher or lower.
9-
10- However, when you guess a particular number x, and you guess wrong, you pay $x. You win the game when you guess the number I picked.
7+ * I pick a number from 1 to n. You have to guess which number I picked.
8+ * Every time you guess wrong, I'll tell you whether the number I picked is higher or lower.
9+ * However, when you guess a particular number x, and you guess wrong, you pay $x. You win the game when you guess the number I picked.
1110
1211 Example:
1312
@@ -31,47 +30,51 @@ Take a small example (n = 3). What do you end up paying in the worst case?
3130 As a follow-up, how would you modify your code to solve the problem of minimizing the expected loss, instead of the worst-case loss?
3231 */
3332public class _375 {
34- public int getMoneyAmount (int n ) {
35- int [][] table = new int [n + 1 ][n + 1 ];
36- return dp (table , 1 , n );
37- }
38-
39- private int dp (int [][] table , int s , int e ) {
40- if (s >= e ) {
41- return 0 ;
42- }
43- if (table [s ][e ] != 0 ) {
44- return table [s ][e ];
33+ public static class Solution1 {
34+ public int getMoneyAmount (int n ) {
35+ int [][] table = new int [n + 1 ][n + 1 ];
36+ return dp (table , 1 , n );
4537 }
46- int res = Integer .MAX_VALUE ;
47- for (int i = s ; i <= e ; i ++) {
48- int temp = i + Math .max (dp (table , s , i - 1 ), dp (table , i + 1 , e ));
49- res = Math .min (res , temp );
38+
39+ private int dp (int [][] table , int s , int e ) {
40+ if (s >= e ) {
41+ return 0 ;
42+ }
43+ if (table [s ][e ] != 0 ) {
44+ return table [s ][e ];
45+ }
46+ int res = Integer .MAX_VALUE ;
47+ for (int i = s ; i <= e ; i ++) {
48+ int temp = i + Math .max (dp (table , s , i - 1 ), dp (table , i + 1 , e ));
49+ res = Math .min (res , temp );
50+ }
51+ table [s ][e ] = res ;
52+ return res ;
5053 }
51- table [s ][e ] = res ;
52- return res ;
5354 }
5455
55- public int getMoneyAmount2 (int n ) {
56- if (n == 1 ) {
57- return 0 ;
58- }
59- int [][] dp = new int [n + 1 ][n + 1 ];
60- for (int x = 1 ; x < n ; x ++) {
61- for (int i = 0 ; i + x <= n ; i ++) {
62- int j = i + x ;
63- dp [i ][j ] = Integer .MAX_VALUE ;
64- for (int k = i ; k <= j ; k ++) {
65- dp [i ][j ] = Math .min (dp [i ][j ], k + Math .max (k - 1 >= i ? dp [i ][k - 1 ] : 0 , j >= k + 1 ? dp [k + 1 ][j ] : 0 ));
56+ public static class Solution2 {
57+ public int getMoneyAmount (int n ) {
58+ if (n == 1 ) {
59+ return 0 ;
60+ }
61+ int [][] dp = new int [n + 1 ][n + 1 ];
62+ for (int x = 1 ; x < n ; x ++) {
63+ for (int i = 0 ; i + x <= n ; i ++) {
64+ int j = i + x ;
65+ dp [i ][j ] = Integer .MAX_VALUE ;
66+ for (int k = i ; k <= j ; k ++) {
67+ dp [i ][j ] = Math .min (dp [i ][j ], k + Math .max (k - 1 >= i ? dp [i ][k - 1 ] : 0 , j >= k + 1 ? dp [k + 1 ][j ] : 0 ));
68+ }
6669 }
6770 }
68- }
69- for (int i = 0 ; i < n + 1 ; i ++) {
70- for (int j = 0 ; j < n + 1 ; j ++) {
71- System .out .print (dp [i ][j ] + " " );
71+ for (int i = 0 ; i < n + 1 ; i ++) {
72+ for (int j = 0 ; j < n + 1 ; j ++) {
73+ System .out .print (dp [i ][j ] + " " );
74+ }
75+ System .out .println ();
7276 }
73- System . out . println () ;
77+ return dp [ 1 ][ n ] ;
7478 }
75- return dp [1 ][n ];
7679 }
7780}
0 commit comments