forked from SamirPaulb/DSAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create 08. Guess Number Higher or Lower II.py
- Loading branch information
1 parent
20811f4
commit 01c5fe6
Showing
1 changed file
with
19 additions
and
0 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
02_Dynamic-Programming/09. DP on Grid/08. Guess Number Higher or Lower II.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# https://leetcode.com/problems/guess-number-higher-or-lower-ii/ | ||
# https://youtu.be/x--bMzT1Xhk | ||
|
||
class Solution: | ||
def getMoneyAmount(self, n: int) -> int: | ||
dp = [[-1]*(n+1) for _ in range(n+1)] | ||
@lru_cache(None) | ||
|
||
def solve(start, end): | ||
if start >= end: return 0 | ||
ans = 2**31 | ||
if dp[start][end] != -1: return dp[start][end] | ||
for i in range(start, end): | ||
ans = min(ans, i + max(solve(start, i-1), solve(i+1, end))) | ||
dp[start][end] = ans | ||
return ans | ||
# print(dp) | ||
return solve(1, n) | ||
|