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 swift implementation to lcp problem: No.51 #3786

Merged
merged 1 commit into from
Nov 19, 2024
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
40 changes: 40 additions & 0 deletions lcp/LCP 51. 烹饪料理/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,46 @@ function perfectMenu(
}
```

#### Swift

```swift
class Solution {
func perfectMenu(_ materials: [Int], _ cookbooks: [[Int]], _ attribute: [[Int]], _ limit: Int) -> Int {
let n = cookbooks.count
var ans = -1

for mask in 0..<(1 << n) {
var a = 0, b = 0
var cnt = [Int](repeating: 0, count: 5)

for i in 0..<n {
if (mask >> i & 1) == 1 {
a += attribute[i][0]
b += attribute[i][1]
for j in 0..<cookbooks[i].count {
cnt[j] += cookbooks[i][j]
}
}
}

var ok = true
for i in 0..<5 {
if cnt[i] > materials[i] {
ok = false
break
}
}

if b >= limit && a > ans && ok {
ans = a
}
}

return ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
35 changes: 35 additions & 0 deletions lcp/LCP 51. 烹饪料理/Solution.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Solution {
func perfectMenu(_ materials: [Int], _ cookbooks: [[Int]], _ attribute: [[Int]], _ limit: Int) -> Int {
let n = cookbooks.count
var ans = -1

for mask in 0..<(1 << n) {
var a = 0, b = 0
var cnt = [Int](repeating: 0, count: 5)

for i in 0..<n {
if (mask >> i & 1) == 1 {
a += attribute[i][0]
b += attribute[i][1]
for j in 0..<cookbooks[i].count {
cnt[j] += cookbooks[i][j]
}
}
}

var ok = true
for i in 0..<5 {
if cnt[i] > materials[i] {
ok = false
break
}
}

if b >= limit && a > ans && ok {
ans = a
}
}

return ans
}
}
Loading