Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Solution for 1900. The Earliest and Latest Rounds Where Players Compete. and updated the code in README_EN and README #1812

Merged
merged 7 commits into from
Oct 15, 2023
Merged
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
@@ -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)

```

Original file line number Diff line number Diff line change
@@ -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)

```

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)