이 포스팅은 Algorithm Problem Solving 시리즈 113 편 중 88 번째 글 입니다.
목차
브론즈2 : 브루트포스 문제이다.
풀이
Code
import Foundation
func combination<T>(_ elements: [T], _ k: Int) -> [[T]] {
var result = [[T]]()
func combi(_ index: Int, _ now: [T]) {
if now.count == k {
result.append(now)
return
}
for i in index..<elements.count {
combi(i + 1, now + [elements[i]])
}
}
combi(0, [])
return result
}
func main() {
let input = readLine()!
let seps = input.components(separatedBy: " ")
let n = Int(seps[0])!, m = Int(seps[1])!
var cards = [Int]()
cards = readLine()!.components(separatedBy: " ").map { Int($0)! }
print(combination(cards, 3).map({ $0.reduce(0, { $0 + $1 })}).filter({ $0 <= m }).max()!)
}
main()
Code2
from itertools import combinations
import sys
input = sys.stdin.readline
def main():
n, m = map(int, input().split())
cards = list(map(int, input().split()))
summations = list(map(sum, combinations(cards, 3)))
print(max(filter(lambda x: x <= m, summations)))
if __name__ == "__main__":
main()
# 10 500
# 93 181 245 214 315 36 185 138 216 295