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

feat: add solutions to lcp problems: No.50,51 #1451

Merged
merged 1 commit into from
Aug 15, 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
94 changes: 93 additions & 1 deletion lcp/LCP 50. 宝石补给/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,114 @@

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

**方法一:模拟**

我们直接模拟宝石的赠送过程,最后返回最大值和最小值的差值即可。

时间复杂度 $O(m + n)$,其中 $m$ 和 $n$ 分别是数组 `gem` 和 `operations` 的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

### **Python3**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```python

class Solution:
def giveGem(self, gem: List[int], operations: List[List[int]]) -> int:
for x, y in operations:
v = gem[x] >> 1
gem[y] += v
gem[x] -= v
return max(gem) - min(gem)
```

### **Java**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```java
class Solution {
public int giveGem(int[] gem, int[][] operations) {
for (var op : operations) {
int x = op[0], y = op[1];
int v = gem[x] >> 1;
gem[y] += v;
gem[x] -= v;
}
int mx = 0, mi = 1 << 30;
for (int x : gem) {
mx = Math.max(mx, x);
mi = Math.min(mi, x);
}
return mx - mi;
}
}
```

### **C++**

```cpp
class Solution {
public:
int giveGem(vector<int>& gem, vector<vector<int>>& operations) {
for (auto& op : operations) {
int x = op[0], y = op[1];
int v = gem[x] >> 1;
gem[y] += v;
gem[x] -= v;
}
int mx = *max_element(gem.begin(), gem.end());
int mi = *min_element(gem.begin(), gem.end());
return mx - mi;
}
};
```

### **Go**

```go
func giveGem(gem []int, operations [][]int) int {
for _, op := range operations {
x, y := op[0], op[1]
v := gem[x] >> 1
gem[y] += v
gem[x] -= v
}
mx, mi := 0, 1<<30
for _, x := range gem {
mx = max(mx, x)
mi = min(mi, x)
}
return mx - mi
}

func max(a, b int) int {
if a > b {
return a
}
return b
}

func min(a, b int) int {
if a < b {
return a
}
return b
}
```

### **TypeScript**

```ts
function giveGem(gem: number[], operations: number[][]): number {
for (const [x, y] of operations) {
const v = gem[x] >> 1;
gem[y] += v;
gem[x] -= v;
}
return Math.max(...gem) - Math.min(...gem);
}
```

### **...**
Expand Down
14 changes: 14 additions & 0 deletions lcp/LCP 50. 宝石补给/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
public:
int giveGem(vector<int>& gem, vector<vector<int>>& operations) {
for (auto& op : operations) {
int x = op[0], y = op[1];
int v = gem[x] >> 1;
gem[y] += v;
gem[x] -= v;
}
int mx = *max_element(gem.begin(), gem.end());
int mi = *min_element(gem.begin(), gem.end());
return mx - mi;
}
};
28 changes: 28 additions & 0 deletions lcp/LCP 50. 宝石补给/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
func giveGem(gem []int, operations [][]int) int {
for _, op := range operations {
x, y := op[0], op[1]
v := gem[x] >> 1
gem[y] += v
gem[x] -= v
}
mx, mi := 0, 1<<30
for _, x := range gem {
mx = max(mx, x)
mi = min(mi, x)
}
return mx - mi
}

func max(a, b int) int {
if a > b {
return a
}
return b
}

func min(a, b int) int {
if a < b {
return a
}
return b
}
16 changes: 16 additions & 0 deletions lcp/LCP 50. 宝石补给/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
public int giveGem(int[] gem, int[][] operations) {
for (var op : operations) {
int x = op[0], y = op[1];
int v = gem[x] >> 1;
gem[y] += v;
gem[x] -= v;
}
int mx = 0, mi = 1 << 30;
for (int x : gem) {
mx = Math.max(mx, x);
mi = Math.min(mi, x);
}
return mx - mi;
}
}
7 changes: 7 additions & 0 deletions lcp/LCP 50. 宝石补给/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Solution:
def giveGem(self, gem: List[int], operations: List[List[int]]) -> int:
for x, y in operations:
v = gem[x] >> 1
gem[y] += v
gem[x] -= v
return max(gem) - min(gem)
8 changes: 8 additions & 0 deletions lcp/LCP 50. 宝石补给/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function giveGem(gem: number[], operations: number[][]): number {
for (const [x, y] of operations) {
const v = gem[x] >> 1;
gem[y] += v;
gem[x] -= v;
}
return Math.max(...gem) - Math.min(...gem);
}
166 changes: 165 additions & 1 deletion lcp/LCP 51. 烹饪料理/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,186 @@

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

**方法一:二进制枚举**

我们注意到,料理的数量 $n$ 不超过 $8$,因此,我们可以使用二进制枚举的方法枚举所有的料理方案。

每种料理都有两种选择:制作或者不制作。因此,我们可以使用一个长度为 $8$ 的二进制数来表示一种方案,其中第 $i$ 位为 $1$ 表示制作第 $i$ 道料理,为 $0$ 表示不制作第 $i$ 道料理。

我们在 $[0, 2^n)$ 的范围内枚举所有料理方案,对于每种方案,我们计算其美味度和饱腹感,如果饱腹感不小于 $limit$,并且食材数量足够制作这些料理,同时其美味度大于当前答案,我们就更新答案。

枚举结束后,我们就可以得到最大的美味度。

时间复杂度 $(2^n \times n)$,其中 $n$ 是料理的数量。我们需要枚举所有的料理方案,对于每种方案,我们需要 $O(n)$ 的时间计算其美味度和饱腹感,因此总时间复杂度为 $O(2^n \times n)$。空间复杂度 $O(1)$。

<!-- tabs:start -->

### **Python3**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```python

class Solution:
def perfectMenu(
self,
materials: List[int],
cookbooks: List[List[int]],
attribute: List[List[int]],
limit: int,
) -> int:
n = len(cookbooks)
ans = -1
for mask in range(1 << n):
a = b = 0
cnt = [0] * 5
for i in range(n):
if mask >> i & 1:
x, y = attribute[i]
a += x
b += y
for j, v in enumerate(cookbooks[i]):
cnt[j] += v
if b >= limit and ans < a and all(c <= d for c, d in zip(cnt, materials)):
ans = a
return ans
```

### **Java**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```java
class Solution {
public int perfectMenu(int[] materials, int[][] cookbooks, int[][] attribute, int limit) {
int n = cookbooks.length;
int ans = -1;
for (int mask = 0; mask < 1 << n; ++mask) {
int a = 0, b = 0;
int[] cnt = new int[5];
for (int i = 0; i < n; ++i) {
if ((mask >> i & 1) == 1) {
int x = attribute[i][0];
int y = attribute[i][1];
a += x;
b += y;
for (int j = 0; j < cookbooks[i].length; ++j) {
cnt[j] += cookbooks[i][j];
}
}
}
boolean ok = true;
for (int i = 0; i < 5 && ok; ++i) {
ok = cnt[i] <= materials[i];
}
if (b >= limit && ans < a && ok) {
ans = a;
}
}
return ans;
}
}
```

### **C++**

```cpp
class Solution {
public:
int perfectMenu(vector<int>& materials, vector<vector<int>>& cookbooks, vector<vector<int>>& attribute, int limit) {
int n = cookbooks.size();
int ans = -1;
for (int mask = 0; mask < 1 << n; ++mask) {
int a = 0, b = 0;
vector<int> cnt(5);
for (int i = 0; i < n; ++i) {
if (mask >> i & 1) {
int x = attribute[i][0];
int y = attribute[i][1];
a += x;
b += y;
for (int j = 0; j < cookbooks[i].size(); ++j) {
cnt[j] += cookbooks[i][j];
}
}
bool ok = true;
for (int i = 0; i < 5 && ok; ++i) {
ok = cnt[i] <= materials[i];
}
if (b >= limit && ans < a && ok) {
ans = a;
}
}
}
return ans;
}
};
```

### **Go**

```go
func perfectMenu(materials []int, cookbooks [][]int, attribute [][]int, limit int) int {
n := len(cookbooks)
ans := -1
for mask := 0; mask < 1<<n; mask++ {
a, b := 0, 0
cnt := make([]int, 5)
for i := 0; i < n; i++ {
if mask>>i&1 == 1 {
x, y := attribute[i][0], attribute[i][1]
a += x
b += y
for j, v := range cookbooks[i] {
cnt[j] += v
}
}
ok := true
for i := 0; i < 5 && ok; i++ {
ok = cnt[i] <= materials[i]
}
if ok && b >= limit && ans < a {
ans = a
}
}
}
return ans
}
```

### **TypeScript**

```ts
function perfectMenu(
materials: number[],
cookbooks: number[][],
attribute: number[][],
limit: number,
): number {
const n = cookbooks.length;
let ans = -1;
for (let mask = 0; mask < 1 << n; ++mask) {
let [a, b] = [0, 0];
const cnt: number[] = Array(5).fill(0);
for (let i = 0; i < n; ++i) {
if (((mask >> i) & 1) === 1) {
const [x, y] = attribute[i];
a += x;
b += y;
for (let j = 0; j < cookbooks[i].length; ++j) {
cnt[j] += cookbooks[i][j];
}
}
let ok = true;
for (let i = 0; i < 5 && ok; ++i) {
ok = cnt[i] <= materials[i];
}
if (b >= limit && ans < a && ok) {
ans = a;
}
}
}
return ans;
}
```

### **...**
Expand Down
Loading