Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,34 @@
<!-- 这里可写当前语言的特殊实现逻辑 -->

```python
class Solution:
def earliestAndLatest(
self, n: int, firstPlayer: int, secondPlayer: int
) -> List[int]:
# dp[i][j][k] := (earliest, latest) pair w/ firstPlayer is i-th player from
# Front, secondPlayer is j-th player from end, and there're k people
@functools.lru_cache(None)
def dp(l: int, r: int, k: int) -> List[int]:
if l == r:
return [1, 1]
if l > r:
return dp(r, l, k)

a = math.inf
b = -math.inf

# Enumerate all possible positions
for i in range(1, l + 1):
for j in range(l - i + 1, r - i + 1):
if not l + r - k // 2 <= i + j <= (k + 1) // 2:
continue
x, y = dp(i, j, (k + 1) // 2)
a = min(a, x + 1)
b = max(b, y + 1)

return [a, b]

return dp(firstPlayer, n - secondPlayer + 1, n)

```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,34 @@ There is no way to make them compete in any other round.
### **Python3**

```python
class Solution:
def earliestAndLatest(
self, n: int, firstPlayer: int, secondPlayer: int
) -> List[int]:
# dp[i][j][k] := (earliest, latest) pair w/ firstPlayer is i-th player from
# Front, secondPlayer is j-th player from end, and there're k people
@functools.lru_cache(None)
def dp(l: int, r: int, k: int) -> List[int]:
if l == r:
return [1, 1]
if l > r:
return dp(r, l, k)

a = math.inf
b = -math.inf

# Enumerate all possible positions
for i in range(1, l + 1):
for j in range(l - i + 1, r - i + 1):
if not l + r - k // 2 <= i + j <= (k + 1) // 2:
continue
x, y = dp(i, j, (k + 1) // 2)
a = min(a, x + 1)
b = max(b, y + 1)

return [a, b]

return dp(firstPlayer, n - secondPlayer + 1, n)

```

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Solution:
def earliestAndLatest(
self, n: int, firstPlayer: int, secondPlayer: int
) -> List[int]:
# dp[i][j][k] := (earliest, latest) pair w/ firstPlayer is i-th player from
# Front, secondPlayer is j-th player from end, and there're k people
@functools.lru_cache(None)
def dp(l: int, r: int, k: int) -> List[int]:
if l == r:
return [1, 1]
if l > r:
return dp(r, l, k)

a = math.inf
b = -math.inf

# Enumerate all possible positions
for i in range(1, l + 1):
for j in range(l - i + 1, r - i + 1):
if not l + r - k // 2 <= i + j <= (k + 1) // 2:
continue
x, y = dp(i, j, (k + 1) // 2)
a = min(a, x + 1)
b = max(b, y + 1)

return [a, b]

return dp(firstPlayer, n - secondPlayer + 1, n)