From d173c4d6ebeef6bc8f4094fe1d72eb9a65041d28 Mon Sep 17 00:00:00 2001 From: Lanre Adedara Date: Tue, 19 Nov 2024 07:37:56 +0100 Subject: [PATCH] feat: add swift implementation to lcp problem: No.51 --- .../README.md" | 40 +++++++++++++++++++ .../Solution.swift" | 35 ++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 "lcp/LCP 51. \347\203\271\351\245\252\346\226\231\347\220\206/Solution.swift" diff --git "a/lcp/LCP 51. \347\203\271\351\245\252\346\226\231\347\220\206/README.md" "b/lcp/LCP 51. \347\203\271\351\245\252\346\226\231\347\220\206/README.md" index 747f8cd273976..187bb6c186990 100644 --- "a/lcp/LCP 51. \347\203\271\351\245\252\346\226\231\347\220\206/README.md" +++ "b/lcp/LCP 51. \347\203\271\351\245\252\346\226\231\347\220\206/README.md" @@ -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..> i & 1) == 1 { + a += attribute[i][0] + b += attribute[i][1] + for j in 0.. materials[i] { + ok = false + break + } + } + + if b >= limit && a > ans && ok { + ans = a + } + } + + return ans + } +} +``` + diff --git "a/lcp/LCP 51. \347\203\271\351\245\252\346\226\231\347\220\206/Solution.swift" "b/lcp/LCP 51. \347\203\271\351\245\252\346\226\231\347\220\206/Solution.swift" new file mode 100644 index 0000000000000..fdf10537d0c5e --- /dev/null +++ "b/lcp/LCP 51. \347\203\271\351\245\252\346\226\231\347\220\206/Solution.swift" @@ -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..> i & 1) == 1 { + a += attribute[i][0] + b += attribute[i][1] + for j in 0.. materials[i] { + ok = false + break + } + } + + if b >= limit && a > ans && ok { + ans = a + } + } + + return ans + } +} \ No newline at end of file