|
| 1 | +# time complexity: O(n^3) |
| 2 | +# space complexity: O(n^2) |
| 3 | +from functools import lru_cache |
| 4 | +from typing import List |
| 5 | + |
| 6 | + |
| 7 | +class Solution: |
| 8 | + def maxCoins(self, nums: List[int]) -> int: |
| 9 | + if len(nums) > 1 and len(set(nums)) == 1: |
| 10 | + return (nums[0] ** 3) * (len(nums) - 2) + nums[0] ** 2 + nums[0] |
| 11 | + |
| 12 | + nums = [1] + nums + [1] |
| 13 | + |
| 14 | + @lru_cache(None) |
| 15 | + def dp(left, right): |
| 16 | + if right - left < 0: |
| 17 | + return 0 |
| 18 | + result = 0 |
| 19 | + for i in range(left, right + 1): |
| 20 | + gain = nums[left - 1] * nums[i] * nums[right + 1] |
| 21 | + remaining = dp(left, i - 1) + dp(i + 1, right) |
| 22 | + result = max(result, remaining + gain) |
| 23 | + return result |
| 24 | + |
| 25 | + return dp(1, len(nums) - 2) |
| 26 | + |
| 27 | +# time complexity: O(n^3) |
| 28 | +# space complexity: O(n^2) |
| 29 | +class Solution: |
| 30 | + def maxCoins(self, nums: List[int]) -> int: |
| 31 | + if len(nums) > 1 and len(set(nums)) == 1: |
| 32 | + return (nums[0] ** 3) * (len(nums) - 2) + nums[0] ** 2 + nums[0] |
| 33 | + nums = [1] + nums + [1] |
| 34 | + n = len(nums) |
| 35 | + dp = [[0 for _ in range(n)] for _ in range(n)] |
| 36 | + for left in range(n - 2, 0, -1): |
| 37 | + for right in range(left, n - 1): |
| 38 | + for i in range(left, right + 1): |
| 39 | + gain = nums[left - 1] * nums[i] * nums[right + 1] |
| 40 | + remaining = dp[left][i - 1] + dp[i + 1][right] |
| 41 | + dp[left][right] = max(remaining + gain, dp[left][right]) |
| 42 | + |
| 43 | + return dp[1][n - 2] |
| 44 | +''' |
| 45 | +1. What is dp_state? |
| 46 | +2. What does dp function return? |
| 47 | +3. What is the base case? |
| 48 | +4. How to calculate dp(dp_state) from dp(other_state)? |
| 49 | +
|
| 50 | +Psuedo code |
| 51 | +function dp(dp_state, memo_dict) { |
| 52 | + // check if we have seen this dp_state |
| 53 | + if dp_state in memo_dict |
| 54 | + return memo_dict[dp_state] |
| 55 | +
|
| 56 | + // base case (a case that we know the answer for already) such as dp_state is empty |
| 57 | + if dp_state is the base cases |
| 58 | + return things like 0 or null |
| 59 | + |
| 60 | + calculate dp(dp_state) from dp(other_state) |
| 61 | + |
| 62 | + save dp_state and the result into memo_dict |
| 63 | +} |
| 64 | +
|
| 65 | +function answerToProblem(input) { |
| 66 | + return dp(start_state, empty_memo_dict) |
| 67 | +} |
| 68 | +
|
| 69 | +DP(Naive) |
| 70 | +// return maximum coins obtainable if we burst all balloons in `nums`. |
| 71 | +function dp(nums, memo_dict) { |
| 72 | + // check if have we seen this dp_state |
| 73 | + if dp_state in memo_dict |
| 74 | + return memo_dict[dp_state] |
| 75 | +
|
| 76 | + // base case |
| 77 | + if nums is empty |
| 78 | + return 0 |
| 79 | + |
| 80 | + max_coins = 0 |
| 81 | + for i in 1 ... nums.length - 2: |
| 82 | + // burst nums[i] |
| 83 | + gain = nums[i - 1] * nums[i] * nums[i + 1] |
| 84 | + // burst the remaining balloons |
| 85 | + remaining = dp(nums without nums[i]) |
| 86 | + max_coins = max(max_coins, gain + remaining) |
| 87 | + |
| 88 | + save dp_state and the result into memo_dict |
| 89 | + return max_coins |
| 90 | +} |
| 91 | +
|
| 92 | +function maxCoin(nums) { |
| 93 | + nums = [1] + nums + [1] // add fake balloons |
| 94 | + return dp(nums, empty_memo_dict) |
| 95 | +} |
| 96 | +''' |
| 97 | + |
| 98 | +nums = [3, 1, 5, 8] |
| 99 | +print(Solution().maxCoins(nums)) |
| 100 | +nums = [1, 5] |
| 101 | +print(Solution().maxCoins(nums)) |
0 commit comments