Skip to content

[pull] main from doocs:main #196

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

Merged
merged 4 commits into from
Mar 23, 2023
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
10 changes: 5 additions & 5 deletions solution/0800-0899/0879.Profitable Schemes/Solution.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
class Solution:
def profitableSchemes(self, n: int, minProfit: int, group: List[int], profit: List[int]) -> int:
def profitableSchemes(
self, n: int, minProfit: int, group: List[int], profit: List[int]
) -> int:
mod = 10**9 + 7
m = len(group)
f = [[[0] * (minProfit + 1) for _ in range(n + 1)]
for _ in range(m + 1)]
f = [[[0] * (minProfit + 1) for _ in range(n + 1)] for _ in range(m + 1)]
for j in range(n + 1):
f[0][j][0] = 1
for i, (x, p) in enumerate(zip(group, profit), 1):
for j in range(n + 1):
for k in range(minProfit + 1):
f[i][j][k] = f[i - 1][j][k]
if j >= x:
f[i][j][k] = (f[i][j][k] + f[i - 1]
[j - x][max(0, k - p)]) % mod
f[i][j][k] = (f[i][j][k] + f[i - 1][j - x][max(0, k - p)]) % mod
return f[m][n][minProfit]
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ def dfs(pos: int, mask: int, lead: bool, limit: bool) -> int:
if i == 0 and lead:
ans += dfs(pos - 1, mask, lead, limit and i == up)
else:
ans += dfs(pos - 1, mask | 1 << i,
False, limit and i == up)
ans += dfs(pos - 1, mask | 1 << i, False, limit and i == up)
return ans

nums = []
Expand Down
71 changes: 39 additions & 32 deletions solution/1000-1099/1033.Moving Stones Until Consecutive/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,17 @@

<!-- 这里可写通用的实现逻辑 -->

**方法一:脑筋急转弯**
**方法一:分类讨论**

- 若 $3$ 个数已经相邻,则不用移动,直接返回结果 $[0,0]$;
- 若 $3$ 个数中存在两数之差小于 $3$,最小只需移动 $1$ 次;
- 其他情况最小只需移动 $2$ 次;
- 两边逐个往中间靠,就是最大移动次数 $c - a - 2$。
我们先将 $a, b, c$ 排序,记为 $x, y, z$,即 $x \lt y \lt z$。

接下来分情况讨论:

1. 如果 $z - x \leq 2$,说明 $3$ 个数已经相邻,不用移动,结果为 $[0, 0]$;
1. 否则,如果 $y - x \lt 3$,或者 $z - y \lt 3$,说明有两个数只间隔一个位置,我们只需要把另一个数移动到这两个数的中间,最小移动次数为 $1$;其他情况,最小移动次数为 $2$;
1. 最大移动次数就是两边的数字逐个往中间靠,最多移动 $z - x - 2$ 次。

时间复杂度 $O(1)$,空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand All @@ -63,16 +68,13 @@
```python
class Solution:
def numMovesStones(self, a: int, b: int, c: int) -> List[int]:
a, b, c = sorted([a, b, c])
ans = [0] * 2
if c - a == 2:
return ans
if b - a < 3 or c - b < 3:
ans[0] = 1
else:
ans[0] = 2
ans[1] = c - a - 2
return ans
x, z = min(a, b, c), max(a, b, c)
y = a + b + c - x - z
mi = mx = 0
if z - x > 2:
mi = 1 if y - x < 3 or z - y < 3 else 2
mx = z - x - 2
return [mi, mx]
```

### **Java**
Expand All @@ -85,9 +87,12 @@ class Solution {
int x = Math.min(a, Math.min(b, c));
int z = Math.max(a, Math.max(b, c));
int y = a + b + c - x - z;
int max = z - x - 2;
int min = y - x == 1 && z - y == 1 ? 0 : y - x <= 2 || z - y <= 2 ? 1 : 2;
return new int[] {min, max};
int mi = 0, mx = 0;
if (z - x > 2) {
mi = y - x < 3 || z - y < 3 ? 1 : 2;
mx = z - x - 2;
}
return new int[]{mi, mx};
}
}
```
Expand All @@ -98,12 +103,14 @@ class Solution {
class Solution {
public:
vector<int> numMovesStones(int a, int b, int c) {
int x = min(min(a, b), c);
int z = max(max(a, b), c);
int x = min({a, b, c});
int z = max({a, b, c});
int y = a + b + c - x - z;
if (z - x == 2) return {0, 0};
int mx = z - x - 2;
int mi = y - x < 3 || z - y < 3 ? 1 : 2;
int mi = 0, mx = 0;
if (z - x > 2) {
mi = y - x < 3 || z - y < 3 ? 1 : 2;
mx = z - x - 2;
}
return {mi, mx};
}
};
Expand All @@ -113,16 +120,16 @@ public:

```go
func numMovesStones(a int, b int, c int) []int {
x := min(min(a, b), c)
z := max(max(a, b), c)
x := min(a, min(b, c))
z := max(a, max(b, c))
y := a + b + c - x - z
if z-x == 2 {
return []int{0, 0}
}
mx := z - x - 2
mi := 2
if y-x < 3 || z-y < 3 {
mi = 1
mi, mx := 0, 0
if z-x > 2 {
mi = 2
if y-x < 3 || z-y < 3 {
mi = 1
}
mx = z - x - 2
}
return []int{mi, mx}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,13 @@
```python
class Solution:
def numMovesStones(self, a: int, b: int, c: int) -> List[int]:
a, b, c = sorted([a, b, c])
ans = [0] * 2
if c - a == 2:
return ans
if b - a < 3 or c - b < 3:
ans[0] = 1
else:
ans[0] = 2
ans[1] = c - a - 2
return ans
x, z = min(a, b, c), max(a, b, c)
y = a + b + c - x - z
mi = mx = 0
if z - x > 2:
mi = 1 if y - x < 3 or z - y < 3 else 2
mx = z - x - 2
return [mi, mx]
```

### **Java**
Expand All @@ -79,9 +76,12 @@ class Solution {
int x = Math.min(a, Math.min(b, c));
int z = Math.max(a, Math.max(b, c));
int y = a + b + c - x - z;
int max = z - x - 2;
int min = y - x == 1 && z - y == 1 ? 0 : y - x <= 2 || z - y <= 2 ? 1 : 2;
return new int[] {min, max};
int mi = 0, mx = 0;
if (z - x > 2) {
mi = y - x < 3 || z - y < 3 ? 1 : 2;
mx = z - x - 2;
}
return new int[]{mi, mx};
}
}
```
Expand All @@ -92,12 +92,14 @@ class Solution {
class Solution {
public:
vector<int> numMovesStones(int a, int b, int c) {
int x = min(min(a, b), c);
int z = max(max(a, b), c);
int x = min({a, b, c});
int z = max({a, b, c});
int y = a + b + c - x - z;
if (z - x == 2) return {0, 0};
int mx = z - x - 2;
int mi = y - x < 3 || z - y < 3 ? 1 : 2;
int mi = 0, mx = 0;
if (z - x > 2) {
mi = y - x < 3 || z - y < 3 ? 1 : 2;
mx = z - x - 2;
}
return {mi, mx};
}
};
Expand All @@ -107,16 +109,16 @@ public:

```go
func numMovesStones(a int, b int, c int) []int {
x := min(min(a, b), c)
z := max(max(a, b), c)
x := min(a, min(b, c))
z := max(a, max(b, c))
y := a + b + c - x - z
if z-x == 2 {
return []int{0, 0}
}
mx := z - x - 2
mi := 2
if y-x < 3 || z-y < 3 {
mi = 1
mi, mx := 0, 0
if z-x > 2 {
mi = 2
if y-x < 3 || z-y < 3 {
mi = 1
}
mx = z - x - 2
}
return []int{mi, mx}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
class Solution {
public:
vector<int> numMovesStones(int a, int b, int c) {
int x = min(min(a, b), c);
int z = max(max(a, b), c);
int x = min({a, b, c});
int z = max({a, b, c});
int y = a + b + c - x - z;
if (z - x == 2) return {0, 0};
int mx = z - x - 2;
int mi = y - x < 3 || z - y < 3 ? 1 : 2;
int mi = 0, mx = 0;
if (z - x > 2) {
mi = y - x < 3 || z - y < 3 ? 1 : 2;
mx = z - x - 2;
}
return {mi, mx};
}
};
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
func numMovesStones(a int, b int, c int) []int {
x := min(min(a, b), c)
z := max(max(a, b), c)
x := min(a, min(b, c))
z := max(a, max(b, c))
y := a + b + c - x - z
if z-x == 2 {
return []int{0, 0}
}
mx := z - x - 2
mi := 2
if y-x < 3 || z-y < 3 {
mi = 1
mi, mx := 0, 0
if z-x > 2 {
mi = 2
if y-x < 3 || z-y < 3 {
mi = 1
}
mx = z - x - 2
}
return []int{mi, mx}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ public int[] numMovesStones(int a, int b, int c) {
int x = Math.min(a, Math.min(b, c));
int z = Math.max(a, Math.max(b, c));
int y = a + b + c - x - z;
int max = z - x - 2;
int min = y - x == 1 && z - y == 1 ? 0 : y - x <= 2 || z - y <= 2 ? 1 : 2;
return new int[] {min, max};
int mi = 0, mx = 0;
if (z - x > 2) {
mi = y - x < 3 || z - y < 3 ? 1 : 2;
mx = z - x - 2;
}
return new int[]{mi, mx};
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
class Solution:
def numMovesStones(self, a: int, b: int, c: int) -> List[int]:
a, b, c = sorted([a, b, c])
ans = [0] * 2
if c - a == 2:
return ans
if b - a < 3 or c - b < 3:
ans[0] = 1
else:
ans[0] = 2
ans[1] = c - a - 2
return ans
x, z = min(a, b, c), max(a, b, c)
y = a + b + c - x - z
mi = mx = 0
if z - x > 2:
mi = 1 if y - x < 3 or z - y < 3 else 2
mx = z - x - 2
return [mi, mx]
Loading