Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Algorithm/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] (Swift) Kakao - ์ˆ˜์‹ ์ตœ๋Œ€ํ™” (Lv.2)

๊ฐ์ž ๐Ÿฅ” 2022. 9. 1. 17:35
๋ฐ˜์‘ํ˜•

๐ŸŸ  ๋ฌธ์ œ ๋งํฌ

https://school.programmers.co.kr/learn/courses/30/lessons/67257

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

๐ŸŸ  ๋‚˜์˜ ํ’€์ด

์šฐ์„ ์ˆœ์œ„๋ฅผ ๋ถ€์—ฌํ•˜๊ณ , ๊ทธ์— ๋”ฐ๋ฅธ ์—ฐ์‚ฐ์„ ํ•˜๊ณ ,, ๋ณต์žกํ•ด ๋ณด์ด์ง€๋งŒ ์ดํ•ด๋งŒ ํ•˜๋ฉด ๋‹จ์ˆœํ•œ ๋ฌธ์ œ์˜€๋‹ค.

์šฐ์„  ๋‚˜๋Š” ์ฃผ์–ด์ง„ expression์„ '์ˆซ์ž'์™€ '์—ฐ์‚ฐ์ž'๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด์•ผ๊ฒ ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค. ์ด ๋ถ€๋ถ„์—์„œ ์–ด๋–ป๊ฒŒ ๋ฐ”๊พธ๋ฉด ์ข‹์„์ง€ ์ƒ๊ฐํ•˜๋‹ค๊ฐ€ ์ข‹์€ ์ฝ”๋“œ๋ฅผ ๋ฐœ๊ฒฌํ•ด์„œ ์กฐ๊ธˆ ์ฐธ๊ณ ํ–ˆ๋‹ค.

let num = "1234"
if num.isNumber {
	print(true)
}

isNumber ์ด๋ผ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•˜๋ฉด, ํ•ด๋‹น ๋ฌธ์ž๊ฐ€ ์ˆซ์ž์ธ์ง€ ํŒ๋‹จํ•˜์—ฌ Bool๊ฐ’์œผ๋กœ ๋ฆฌํ„ดํ•œ๋‹ค. ์ด ๊ฒƒ์„ ์ด์šฉํ•ด์„œ expression์— ์žˆ๋Š” 'ํ•œ ๋ฌธ์ž'์”ฉ๊บผ๋‚ด์–ด number๋ฉด, num์— ์ฐจ๊ณก์ฐจ๊ณก ์Œ“์•„์ฃผ๊ณ , ์•„๋‹ˆ๋ผ๋ฉด ์—ฐ์‚ฐ์ž์ด๋ฏ€๋กœ ์ง€๊ธˆ๊นŒ์ง€ ์Œ“์ธ num์„ ๋ฐฐ์—ด์— ๋„ฃ์–ด์ฃผ๊ณ  ์—ฐ์‚ฐ์ž๊นŒ์ง€ ๋„ฃ์–ด์ฃผ๋Š” ํ˜•์‹์œผ๋กœ ์ง„ํ–‰ํ–ˆ๋‹ค. 

 

๊ทธ๋ฆฌ๊ณ  ์ด์ œ ์–ด๋–ป๊ฒŒ ์ด ๋ฐฐ์—ด์„ ์—ฐ์‚ฐํ•  ๊ฒƒ์ธ๊ฐ€ ์ƒ๊ฐํ•ด์•ผํ•œ๋‹ค.

["100", "-", "200", "*", "300", "-", "500", "+", "20"]

์ด๋Ÿฐ ๋ฐฐ์—ด์—์„œ '์—ฐ์‚ฐ์ž'๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์•ž๋’ค ์ˆซ์ž๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์ด ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๊ทธ๋ž˜์„œ ๊ฐ operation ๋งˆ๋‹ค ๊ณ„์‚ฐ๋œ result๋ฅผ remove ์„ธ๋ฒˆ์„ ํ†ตํ•ด ๊ธฐ์กด๊ฐ’์œผ ์‚ญ์ œํ•˜๊ณ , ํ•œ๋ฒˆ append ํ•ด์ฃผ์–ด์„œ newArr๋ฅผ ๊ฐฑ์‹ ํ•ด ๋‚˜๊ฐ”๋‹ค.

๋” ์ž์„ธํ•˜๊ฑฐ๋‚˜ ์ƒ์„ธํ•œ ๊ฒƒ์€ ์ฝ”๋“œ ๋‚ด๋ถ€ ์ฃผ์„์œผ๋กœ ๋‹ฌ์•„๋‘์—ˆ๋‹ค.

 

๐ŸŸ  ์ •๋‹ต์ฝ”๋“œ

https://github.com/deslog/Algorithm/blob/main/Algorithm/Programmers/kakao_%EC%88%98%EC%8B%9D%20%EC%B5%9C%EB%8C%80%ED%99%94/main.swift

 

GitHub - deslog/Algorithm

Contribute to deslog/Algorithm development by creating an account on GitHub.

github.com

//
//  main.swift
//  Algorithm
//
//  Created by LeeJiSoo on 2022/09/01.
//

import Foundation

let expression = "100-200*300-500+20"
// result = 60420

func solution(_ expression:String) -> Int {

    // ์ตœ์ข… ๋ฆฌํ„ดํ•  result
    var resultList = [Int]()

    // input์„ List๋กœ ๋ฐ”๊ฟ”์ฃผ๊ธฐ
    var num = ""
    var arr = [String]()
    expression.forEach { char in
        if char.isNumber {
            num += String(char)
        } else {
            arr.append(num) // ์ง€๊ธˆ๊นŒ์ง€ ์Œ“์•„๋†จ๋˜ ์ˆซ์ž ์ถ”๊ฐ€
            arr.append(String(char)) // ์—ฐ์‚ฐ์ž ์ถ”๊ฐ€
            num = "" //num ์ดˆ๊ธฐํ™”
        }
    }
    arr.append(num) // ๋งˆ์ง€๋ง‰ ์ˆซ์ž ๋„ฃ์–ด์ค˜์•ผํ•ด (์ด๊ฑฐ๋•Œ๋ฌธ์— ์‹œ๊ฐ„ ๊ฐœ์žก์•„๋จน์—ˆ๋„ค)
    print(arr)

    // ์—ฐ์‚ฐ์ž
    let operatorList = [["+", "-", "*"], ["+", "*", "-"],
                        ["-", "+", "*"], ["-", "*", "+"],
                        ["*", "-", "+"], ["*", "+", "-"]]

    var newArr = arr

    operatorList.forEach { operators in
        // newArr์— 1๊ฐœ๋งŒ ๋‚จ์„๋•Œ๊นŒ์ง€ -> ์—ฐ์‚ฐํ•˜๋ฉด ๊ฒฐ๊ตญ ํ•˜๋‚˜์˜ ๊ฒฐ๊ณผ๋งŒ ๋‚จ์œผ๋‹ˆ๊นŒ
        while newArr.count > 1 {
            operators.forEach { op in
                while newArr.contains(op) { // ์—ฐ์‚ฐ์ž ์žˆ๋Š”๋Œ€๋กœ ๊ณ„์‚ฐํ•ด์•ผํ•˜๋‹ˆ๊นŒ while๋ฌธ
                    let i =  newArr.firstIndex(of: String(op))! // op ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ ์ฐพ์•„
                    var result = 0

                    if op == "+" {
                        result = Int(newArr[i-1])! + Int(newArr[i+1])!
                    } else if op == "-" {
                        result = Int(newArr[i-1])! - Int(newArr[i+1])!
                    } else if op == "*" {
                        result = Int(newArr[i-1])! * Int(newArr[i+1])!
                    }
                    // ์—ฐ์‚ฐ์ž์™€ ์•ž๋’ค ์ˆซ์ž ์ง€์›Œ์ฃผ๊ณ , ๊ทธ์ž๋ฆฌ์— ๋„ฃ์–ด์•ผํ•ด
                    newArr.remove(at: i-1)
                    newArr.remove(at: i-1)
                    newArr.remove(at: i-1)
                    newArr.insert(String(result), at: i-1)
                }
            }
        } // while newArr.count > 1 ์ข…๋ฃŒ์ง€์ 

        let temp = Int(newArr[0])!
        resultList.append(abs(temp))
        newArr = arr // ๋‹ค์Œ ์—ฐ์‚ฐ์ž ์„ธํŠธ๋ฅผ ์œ„ํ•ด์„œ ๋‹ค์‹œ ์ดˆ๊ธฐํ™”
    }

    return resultList.max()!
}

print(solution(expression))
๋ฐ˜์‘ํ˜•