|
32 | 32 | */ |
33 | 33 | public class _486 { |
34 | 34 |
|
35 | | - /**credit: https://discuss.leetcode.com/topic/76312/java-1-line-recursion-solution |
36 | | - * Explanation |
37 | | - So assuming the sum of the array it SUM, so eventually player1 and player2 will split the SUM between themselves. |
38 | | - For player1 to win, he/she has to get more than what player2 gets. |
39 | | - If we think from the prospective of one player, then what he/she gains each time is a plus, |
40 | | - while, what the other player gains each time is a minus. Eventually if player1 can have a >0 total, player1 can win. |
41 | | -
|
42 | | - Helper function simulate this process. In each round: |
43 | | - if e==s, there is no choice but have to select nums[s] |
44 | | - otherwise, this current player has 2 options: |
45 | | - --> nums[s]-helper(nums,s+1,e): this player select the front item, leaving the other player a choice from s+1 to e |
46 | | - --> nums[e]-helper(nums,s,e-1): this player select the tail item, leaving the other player a choice from s to e-1 |
47 | | - Then take the max of these two options as this player's selection, return it.*/ |
48 | | - public boolean predictTheWinner(int[] nums) { |
49 | | - return helper(nums, 0, nums.length - 1, new Integer[nums.length][nums.length]) >= 0; |
50 | | - } |
| 35 | + public static class Solution1 { |
| 36 | + /** |
| 37 | + * credit: https://discuss.leetcode.com/topic/76312/java-1-line-recursion-solution |
| 38 | + * Explanation |
| 39 | + * So assuming the sum of the array it SUM, so eventually player1 and player2 will split the SUM between themselves. |
| 40 | + * For player1 to win, he/she has to get more than what player2 gets. |
| 41 | + * If we think from the prospective of one player, then what he/she gains each time is a plus, |
| 42 | + * while, what the other player gains each time is a minus. Eventually if player1 can have a >0 total, player1 can win. |
| 43 | + * |
| 44 | + * Helper function simulate this process. In each round: |
| 45 | + * if e==s, there is no choice but have to select nums[s] |
| 46 | + * otherwise, this current player has 2 options: |
| 47 | + * --> nums[s]-helper(nums,s+1,e): this player select the front item, leaving the other player a choice from s+1 to e |
| 48 | + * --> nums[e]-helper(nums,s,e-1): this player select the tail item, leaving the other player a choice from s to e-1 |
| 49 | + * Then take the max of these two options as this player's selection, return it. |
| 50 | + */ |
| 51 | + public boolean predictTheWinner(int[] nums) { |
| 52 | + return helper(nums, 0, nums.length - 1, new Integer[nums.length][nums.length]) >= 0; |
| 53 | + } |
51 | 54 |
|
52 | | - private int helper(int[] nums, int start, int end, Integer[][] mem) { |
53 | | - if (mem[start][end] == null) { |
54 | | - mem[start][end] = start == end ? nums[end] : |
55 | | - Math.max(nums[end] - helper(nums, start, end - 1, mem), |
56 | | - nums[start] - helper(nums, start + 1, end, mem)); |
| 55 | + private int helper(int[] nums, int start, int end, Integer[][] mem) { |
| 56 | + if (mem[start][end] == null) { |
| 57 | + mem[start][end] = start == end ? nums[end] : |
| 58 | + Math.max(nums[end] - helper(nums, start, end - 1, mem), |
| 59 | + nums[start] - helper(nums, start + 1, end, mem)); |
| 60 | + } |
| 61 | + return mem[start][end]; |
57 | 62 | } |
58 | | - return mem[start][end]; |
59 | 63 | } |
60 | 64 |
|
61 | 65 | } |
0 commit comments