We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
💬 문제
https://school.programmers.co.kr/learn/courses/30/lessons/67257
💬 Idea
<calbyFomula 메서드 동작 방식>
index
index + 1
💬 풀이
import Foundation func solution(_ expression:String) -> Int64 { let numArr = expression.split(whereSeparator: { $0 == "+" || $0 == "*" || $0 == "-" }).map({ Int($0)! }) let operArr = expression.split(whereSeparator: { $0.isNumber }).map({ String($0) }) let formulaPermutation = permutation(Array(Set(operArr)), Set(operArr).count) var maxValue = 0 for i in formulaPermutation { maxValue = max(maxValue, calbyFormula(i, operArr, numArr)) } return Int64(maxValue) } // 우선순위 수식 기반으로 값을 계산하는 메서드 func calbyFormula(_ priority: [String], _ operArr: [String], _ numArr: [Int]) -> Int { var numArr = numArr var operArr = operArr for i in priority { while operArr.contains(String(i)) { let idx = operArr.firstIndex(of: String(i)) ?? -1 if i == "*" { numArr[idx] = numArr[idx] * numArr[idx + 1] } else if i == "+" { numArr[idx] = numArr[idx] + numArr[idx + 1] } else { numArr[idx] = numArr[idx] - numArr[idx + 1] } numArr.remove(at: idx + 1) operArr.remove(at: idx) } } return abs(numArr[0]) } // 순열 생성 메서드 func permutation(_ array: [String], _ n: Int) -> Set<[String]> { var result = Set<[String]>() if array.count < n { return result } var visited = Array(repeating: false, count: array.count) func cycle(_ now: [String]) { if now.count == n { result.insert(now) return } for i in 0..<array.count { if visited[i] { continue } else { visited[i] = true cycle(now + [array[i]]) visited[i] = false } } } cycle([]) return result }
The text was updated successfully, but these errors were encountered:
#161 - 수식 최대화 문제 풀이
5d48c58
6c1b671
hwangJi-dev
No branches or pull requests
💬 문제
https://school.programmers.co.kr/learn/courses/30/lessons/67257
💬 Idea
<calbyFomula 메서드 동작 방식>
index
,index + 1
2개의 숫자가 해당 연산기호로 연산되어야 할 숫자이므로 해당 두 인덱스에 해당하는 숫자를 연산기호에 맞게 연산해준다.💬 풀이
The text was updated successfully, but these errors were encountered: