Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cead117

Browse files
authored
feat: add python solution to lc problem: No.1900 (doocs#1812)
No.1900.Earliest and Latest Rounds Where Players Compete
1 parent 4b52e58 commit cead117

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

‎solution/1900-1999/1900.The Earliest and Latest Rounds Where Players Compete/README.md‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,34 @@
7474
<!-- 这里可写当前语言的特殊实现逻辑 -->
7575

7676
```python
77+
class Solution:
78+
def earliestAndLatest(
79+
self, n: int, firstPlayer: int, secondPlayer: int
80+
) -> List[int]:
81+
# dp[i][j][k] := (earliest, latest) pair w/ firstPlayer is i-th player from
82+
# Front, secondPlayer is j-th player from end, and there're k people
83+
@functools.lru_cache(None)
84+
def dp(l: int, r: int, k: int) -> List[int]:
85+
if l == r:
86+
return [1, 1]
87+
if l > r:
88+
return dp(r, l, k)
89+
90+
a = math.inf
91+
b = -math.inf
92+
93+
# Enumerate all possible positions
94+
for i in range(1, l + 1):
95+
for j in range(l - i + 1, r - i + 1):
96+
if not l + r - k // 2 <= i + j <= (k + 1) // 2:
97+
continue
98+
x, y = dp(i, j, (k + 1) // 2)
99+
a = min(a, x + 1)
100+
b = max(b, y + 1)
101+
102+
return [a, b]
103+
104+
return dp(firstPlayer, n - secondPlayer + 1, n)
77105

78106
```
79107

‎solution/1900-1999/1900.The Earliest and Latest Rounds Where Players Compete/README_EN.md‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,34 @@ There is no way to make them compete in any other round.
6868
### **Python3**
6969

7070
```python
71+
class Solution:
72+
def earliestAndLatest(
73+
self, n: int, firstPlayer: int, secondPlayer: int
74+
) -> List[int]:
75+
# dp[i][j][k] := (earliest, latest) pair w/ firstPlayer is i-th player from
76+
# Front, secondPlayer is j-th player from end, and there're k people
77+
@functools.lru_cache(None)
78+
def dp(l: int, r: int, k: int) -> List[int]:
79+
if l == r:
80+
return [1, 1]
81+
if l > r:
82+
return dp(r, l, k)
83+
84+
a = math.inf
85+
b = -math.inf
86+
87+
# Enumerate all possible positions
88+
for i in range(1, l + 1):
89+
for j in range(l - i + 1, r - i + 1):
90+
if not l + r - k // 2 <= i + j <= (k + 1) // 2:
91+
continue
92+
x, y = dp(i, j, (k + 1) // 2)
93+
a = min(a, x + 1)
94+
b = max(b, y + 1)
95+
96+
return [a, b]
97+
98+
return dp(firstPlayer, n - secondPlayer + 1, n)
7199

72100
```
73101

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution:
2+
def earliestAndLatest(
3+
self, n: int, firstPlayer: int, secondPlayer: int
4+
) -> List[int]:
5+
# dp[i][j][k] := (earliest, latest) pair w/ firstPlayer is i-th player from
6+
# Front, secondPlayer is j-th player from end, and there're k people
7+
@functools.lru_cache(None)
8+
def dp(l: int, r: int, k: int) -> List[int]:
9+
if l == r:
10+
return [1, 1]
11+
if l > r:
12+
return dp(r, l, k)
13+
14+
a = math.inf
15+
b = -math.inf
16+
17+
# Enumerate all possible positions
18+
for i in range(1, l + 1):
19+
for j in range(l - i + 1, r - i + 1):
20+
if not l + r - k // 2 <= i + j <= (k + 1) // 2:
21+
continue
22+
x, y = dp(i, j, (k + 1) // 2)
23+
a = min(a, x + 1)
24+
b = max(b, y + 1)
25+
26+
return [a, b]
27+
28+
return dp(firstPlayer, n - secondPlayer + 1, n)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /