Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: [5,3,4,5]Output: trueExplanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Note:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
Solution 1: min-max (TLE)
Time complexity: O(2^n)
Space complexity: O(n)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Author: Huahua
// Running time: TLE 26/46 passed.
classSolution{
public:
boolstoneGame(vector<int>&piles){
returnscore(piles,0,piles.size()-1)>0;
}
private:
intscore(constvector<int>&piles,intl,intr){
if(l==r)returnpiles[l];
returnmax(piles[l]-score(piles,l+1,r),
piles[r]-score(piles,l,r-1));
}
};
Solution 2: min-max + memorization
Time complexity: O(n^2)
Space complexity: O(n^2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Author: Huahua
// Running time: 12 ms
classSolution{
public:
boolstoneGame(vector<int>&piles){
constintn=piles.size();
m_=vector<vector<int>>(n,vector<int>(n,INT_MIN));
returnscore(piles,0,n-1)>0;
}
private:
vector<vector<int>>m_;
intscore(constvector<int>&piles,intl,intr){
if(l==r)returnpiles[l];
if(m_[l][r]==INT_MIN)
m_[l][r]=max(piles[l]-score(piles,l+1,r),
piles[r]-score(piles,l,r-1));
returnm_[l][r];
}
};
Solution 3: min-max + DP
Time complexity: O(n^2)
Space complexity: O(n^2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Author: Huahua
// Running time: 8 ms
classSolution{
public:
boolstoneGame(vector<int>&piles){
constintn=piles.size();
// dp[i][j] := max(your_stones - op_stones) for piles[i] ~ piles[j]
Given a non-empty, singly linked list with head node head, return a middle node of linked list.
If there are two middle nodes, return the second middle node.
Example 1:
Input: [1,2,3,4,5]Output: Node 3 from this list (Serialization: [3,4,5])
The returned node has value 3. (The judge's serialization of this node is [3,4,5]).
Note that we returned a ListNode object ans, such that:
ans.val = 3, ans.next.val = 4, ans.next.next.val = 5, and ans.next.next.next = NULL.
Example 2:
Input: [1,2,3,4,5,6]Output: Node 4 from this list (Serialization: [4,5,6])
Since the list has two middle nodes with values 3 and 4, we return the second one.
Note:
The number of nodes in the given list will be between 1 and 100.
Given a list of non-overlapping axis-aligned rectangles rects, write a function pick which randomly and uniformily picks an integer point in the space covered by the rectangles.
Note:
An integer point is a point that has integer coordinates.
A point on the perimeter of a rectangle is included in the space covered by the rectangles.
ith rectangle = rects[i] = [x1,y1,x2,y2], where [x1, y1] are the integer coordinates of the bottom-left corner, and [x2, y2] are the integer coordinates of the top-right corner.
length and width of each rectangle does not exceed 2000.
1 <= rects.length <= 100
pick return a point as an array of integer coordinates [p_x, p_y]
The input is two lists: the subroutines called and their arguments. Solution‘s constructor has one argument, the array of rectangles rects. pick has no arguments. Arguments are always wrapped with a list, even if there aren’t any.
Given an array w of positive integers, where w[i] describes the weight of index i, write a function pickIndex which randomly picks an index in proportion to its weight.
The input is two lists: the subroutines called and their arguments. Solution‘s constructor has one argument, the array w. pickIndex has no arguments. Arguments are always wrapped with a list, even if there aren’t any.
Solution: Binary Search
Convert PDF to CDF
Uniformly sample a value s in [1, sum(weights)].
Use binary search to find first index such that PDF[index] >= s.