새소식

인기 검색어

프로그래머스/Lv.2

[Swift] 수식 최대화

  • -
import Foundation

// 방법 1
func calculate(_ num: [Int64], _ expression: [String], _ priority: [String]) -> Int64 {
    var num = num
    var tmp:[Int64] = []
    var expression = expression
    
    for i in 0..<3 {
        for _ in 0..<expression.count {
            tmp.append(num.removeFirst())
            let exp = expression.removeFirst()
            if exp == priority[i] {
                switch exp {
                case "*":
                    num.insert(tmp.removeLast() * num.removeFirst(), at: 0)
                case "+":
                    num.insert(tmp.removeLast() + num.removeFirst(), at: 0)
                case "-":
                    num.insert(tmp.removeLast() - num.removeFirst(), at: 0)
                default:
                    break
                }
            } else { expression.append(exp) }
        }
        num = tmp + num
        tmp = []
    }
    
    return abs(num.removeFirst())
}

func solution(_ expression:String) -> Int64 {
    var result: [Int64] = []
    let expressionPriority = [
        ["*", "+", "-"],
        ["*", "-", "+"],
        ["+", "*", "-"],
        ["+", "-", "*"],
        ["-", "*", "+"],
        ["-", "+", "*"]
    ]
    
    do {
        let numberRegex = try NSRegularExpression(pattern: "[0-9]*")
        let expressionRegex = try NSRegularExpression(pattern: "[*+-]")
        let numArray = numberRegex.matches(in: expression, range: NSRange(location: 0, length: expression.count)).compactMap {Int64(expression[Range($0.range, in: expression)!])}
        let expressionArray = expressionRegex.matches(in: expression, range: NSRange(location: 0, length: expression.count)).compactMap {String(expression[Range($0.range, in: expression)!])}
        for priority in expressionPriority {
            result.append(calculate(numArray, expressionArray, priority))
        }
    } catch {
        print("error")
    }
    return result.max()!
}


// 방법2
enum Operation: Character {
    case multiple = "*"
    case plus = "+"
    case minus = "-"

    func operate(_ n1: Int64, _ n2: Int64) -> Int64 {
        switch self {
            case .multiple: return n1 * n2
            case .plus: return n1 + n2
            case .minus: return n1 - n2
        }
    }
}

func operate(numbers: [Int64], opers: [Character], prior: [Operation]) -> Int64 {
    var numbers = numbers
    var opers = opers
    for oper in prior {
        while let index = opers.firstIndex(of: oper.rawValue) {
            numbers[index] = oper.operate(numbers[index], numbers[index+1])
            numbers.remove(at: index+1)
            opers.remove(at: index)
        }
    }
    return abs(numbers[0])
}

func solution2(_ expression:String) -> Int64 {
    let numbers = expression.components(separatedBy: ["*", "+", "-"]).map{abs(Int64($0)!)}
    let opers = Array(expression.filter({ Int(String($0)) == nil}))
    let myOper = "*+-"
    var maxValue: Int64 = 0

    for first in myOper {
        for second in myOper {
            guard first != second else { continue }
            let third = "*+-".filter({$0 != first && $0 != second})[0]
            let result = operate(numbers: numbers, opers: opers, prior: [
                Operation(rawValue: first)!,
                Operation(rawValue: second)!,
                Operation(rawValue: third)!
            ])
            maxValue = max(maxValue, result)
        }
    }
    return maxValue
}

 

'프로그래머스 > Lv.2' 카테고리의 다른 글

[Swift] 배달  (0) 2023.06.17
[Swift] 행렬 테두리 회전하기  (0) 2023.06.17
[Swift] 무인도 여행  (0) 2023.06.17
[Swift] 괄호 변환  (0) 2023.06.17
[Swift] 전력망 둘로 나누기  (0) 2023.06.17
Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.