Skip to content

Commit 8bb85bb

Browse files
refactor 486
1 parent fc2318d commit 8bb85bb

File tree

1 file changed

+26
-22
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+26
-22
lines changed

src/main/java/com/fishercoder/solutions/_486.java

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,30 +32,34 @@
3232
*/
3333
public class _486 {
3434

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+
}
5154

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];
5762
}
58-
return mem[start][end];
5963
}
6064

6165
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy