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

Algorithm/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] (Swift) ์ด์ง„๋ณ€ํ™˜ ๋ฐ˜๋ณตํ•˜๊ธฐ (LV.2) (feat. radix)

๊ฐ์ž ๐Ÿฅ” 2022. 10. 11. 22:06
๋ฐ˜์‘ํ˜•

๐Ÿซ  TIL - init(_: radix: )

swift์—์„œ ์ง„์ˆ˜๋ณ€ํ™˜์„ ํ•˜๋Š” ๋ฉ”์„œ๋“œ, radix์— ๋Œ€ํ•ด์„œ ๋ฐฐ์› ์Šต๋‹ˆ๋‹ค. (์•Œ๊ณ ๊ฐ€๋ฉด ์ฝ”ํ…Œ์—์„œ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉํ• ๋“ฏ!๐Ÿค”)

let num = 7
print(String(num, radix: 2))
// "111"
  • radix ๋’ค์— ์ž์œ ๋กญ๊ฒŒ 10์ง„์ˆ˜๋ฅผ ์–ด๋–ค ์ง„์ˆ˜๋กœ ๋ณ€ํ™˜ํ• ์ง€ ์ ์œผ๋ฉด ๋œ๋‹ค.
  • Stringํ˜•ํƒœ๋กœ ์ถœ๋ ฅ๋œ๋‹ค.

์™œ ์ด์ง„์ˆ˜ ์ธ๋ฐ, Int ํ˜•ํƒœ๊ฐ€ ๋˜์ง€ ์•Š๋Š”๊ฐ€? ๋‹น์—ฐํ•˜์ง€!! Int๋Š” 10์ง„์ˆ˜ ์•„๋ƒ?? 

var num = 7
print(Int(num, radix: 2))

// error

์ผ๋‹จ, ๋‚ด๊ฐ€ ์ƒ๊ฐํ–ˆ๋˜ ์ด์œ ์ค‘ ํ•˜๋‚˜๋Š”, Int๋Š” 10์ง„์ˆ˜๋ฅผ ๊ฐ€์ง„๋‹ค. ๊ทธ๋ž˜์„œ intํ˜• "111"๊ณผ ์ด์ง„์ˆ˜ "111" ์€ ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์—, Int๋กœ ๋ฐ”๋กœ ํ˜•๋ณ€ํ™˜์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค. 

๊ทธ๋ฆฌ๊ณ  ์œ„ ์ด๋ฏธ์ง€ ์ฒ˜๋Ÿผ, radix๋Š” string ํ”„๋กœํ† ์ฝœ์„ ๋”ฐ๋ฅธ๋‹ค๊ณ  ๋‚˜์™€์žˆ๋‹ค. ์ง„๋ฒ• ์ „ํ™˜์€ string์œผ๋กœ ์ถœ๋ ฅ๋œ๋‹ค๋Š” ๊ฒƒ์„ ๊ธฐ์–ตํ•˜์ž!!

 

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

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

 

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

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

programmers.co.kr

 

๐ŸŸ  ๋‚˜์˜ ์ฒซ๋ฒˆ์งธ ํ’€์ด - ์‹œ๊ฐ„์ดˆ๊ณผ

ํ…Œ์ผ€ 2, 5, 6, 8 ์‹œ๊ฐ„์ดˆ๊ณผ

  • while๋ฌธ์ด ๋„ˆ๋ฌด ๋งŽ์•„์„œ ์‹œ๊ฐ„์ดˆ๊ณผ์— ๊ฑธ๋ ธ๋‚˜?
    • while๋ฌธ์„ true๋กœ ๋‘์ง€ ์•Š๊ณ  ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ์‹์„ ๊ณ ๋ฏผํ•ด๋ณด์ž.
  • while๋ฌธ์—์„œ ํƒˆ์ถœํ•˜์ง€ ๋ชปํ•ด์„œ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚ฌ๋˜ ๊ฒƒ
    • result ๊ฐ€ 10 ์ผ๋•Œ์— ๋Œ€ํ•œ ์กฐ๊ฑด๋ฌธ์„ ์ถ”๊ฐ€ํ•ด์ฃผ๋ฉด์„œ break ๋ฌธ์„ ๊ฑธ์–ด์คŒ
  • ๊ทธ๋ฆฌ๊ณ  ๋กœ์ง์„ ์–ด๋–ป๊ฒŒ ๊ณ ์ณ๋„ 2, 5, 6, 8๋งŒ ์—๋Ÿฌ๋‚˜๋˜๋ฐ, ์ˆซ์ž๊ฐ€ ์•„์ฃผ ์ปค์ง€๋ฉด ๊ฐ๋‹น์ด ์•ˆ๋˜๋‚˜ ?
    • ์ด์ค‘ while๋ฌธ์„ ์‚ฌ์šฉํ•œ๋‹ค๊ธฐ๋ณด๋‹จ, filter๋‚˜ radix ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•ด๋ณด๋Š”๊ฒŒ ์ข‹์„๋“ฏ
    • filter - O(N) / radix - O(1)
    • radix๋ฅผ ๊ตฌํ•˜๋Š” ๋ถ€๋ถ„์—์„œ while๋ฌธ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ฒŒ ๊ณ ์ณ์•ผํ•จ.
  • ๋‘˜์งธ๋กœ, ์•„๋ž˜ ์ฝ”๋“œ๋กœ ์ž‘์„ฑํ•˜๋ฉด ์‹œ๊ฐ„์ดˆ๊ณผ
    • filter๋ฅผ ์‚ฌ์šฉํ•ด์„œ “0”์„ ์ง€์›Œ์ฃผ๋ฉด ๋” ์ข‹์„ ๊ฒƒ
  • var deleteZeroCount = copyS.components(separatedBy: "0").joined().count
import Foundation

let s = "110010101001" // ๊ฒฐ๊ณผ [3, 8]

func solution(_ s: String) -> [Int] {
    var zerocount = 0
    var rotation = 0
    var copyS = s
    var deletezero = 0
    var ans = [Int]()

    while true {
        rotation += 1
        print(rotation)
        // 0์ œ๊ฑฐํ•œ ๊ธธ์ด ๊ตฌํ•˜๊ณ , count์— ์ œ๊ฑฐ๋œ 0๊ฐœ์ˆ˜ ๋”ํ•ด์ฃผ๊ธฐ
        deletezero = deleteZeroCount(copyS)
        zerocount += copyS.count - deletezero

        // deletezero ์ด์ง„๋ฒ•์œผ๋กœ ๋ฐ”๊พธ๊ธฐ
        let result = makeBinaryScale(deletezero)
        print(result)

        if result != "10" {
            copyS = result
        } else {
            rotation += 1
            zerocount += 1
            break
        }
    }
    ans.append(rotation)
    ans.append(zerocount)
    return ans
}

func deleteZeroCount(_ s: String) -> Int {
    var result = s.components(separatedBy: "0").joined().count
    return result
}

func makeBinaryScale(_ num: Int) -> String {
    var binaryNum = num
    var result = ""
    while true {
        let quot = binaryNum / 2
        let remainder = binaryNum % 2

        if quot != 1 {
            result += String(remainder)
            binaryNum = quot
        } else if quot == 1 {
            result += String(remainder)
            result += "1"
            break
        }
    }

    return String(result.reversed())
}

print(solution(s))
//print(makeBinaryScale(111))

 

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

https://github.com/deslog/Algorithm/tree/main/Algorithm/Programmers/%EC%9D%B4%EC%A7%84%EB%B3%80%ED%99%98%EB%B0%98%EB%B3%B5%ED%95%98%EA%B8%B0(lv.2) 

 

GitHub - deslog/Algorithm: โœจ ๋ฐฑ์ค€, ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค Swift ๋ฌธ์ œํ’€์ด โœจ

โœจ ๋ฐฑ์ค€, ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค Swift ๋ฌธ์ œํ’€์ด โœจ. Contribute to deslog/Algorithm development by creating an account on GitHub.

github.com

import Foundation

let s = "110010101001" // ๊ฒฐ๊ณผ [3, 8]

func solution(_ s: String) -> [Int] {
    var zerocount = 0
    var rotation = 0
    var copyS = s

    while copyS != "1" {
        let deleteZeroNum = copyS.filter { $0 == "1" }
        zerocount += copyS.count - deleteZeroNum.count
        copyS = String(deleteZeroNum.count, radix: 2)
        rotation += 1
    }
    return [rotation, zerocount]
}

print(solution(s))

 

 

์•„,, ์ƒ๊ฐ์—†์ด ๋ฐ˜๋ณต๋ฌธ ์“ฐ๋‹ค๊ฐ€ ์‹œ๊ฐ„์ดˆ๊ณผ ๊ฑธ๋ ธ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋Š”๋ฐ, ์ƒ๊ฐ๋ณด๋‹ค while๋ฌธ์„ ์ค„์˜€๋‹ค๊ณ  ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋ฐ”๋กœ ํ•ด๊ฒฐ๋˜์ง„ ์•Š์•˜๋‹ค. ๋ฉ”์„œ๋“œ ํ•˜๋‚˜ํ•˜๋‚˜ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ๋”ฐ์ ธ๋ณด๋ฉด์„œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์•ผํ•จ์„ ๊นจ๋‹ฌ์•—๋‹ค ใ… ใ… ใ…  

์šฐ์„  ์•„๋ž˜ ์ฐธ๊ณ ๊ธ€์„ ์กฐ๊ธˆ ์ฐจ๊ทผ์ฐจ๊ทผ ์ •๋…ํ•ด๋ณด์ž.

https://demian-develop.tistory.com/30

 

iOS) Swift์˜ Time complexity์— ๊ด€ํ•œ ๊ณ ์ฐฐ

API์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„(Time complexity)์— ๋Œ€ํ•ด ์ดํ•ดํ•˜๊ณ  ์žˆ์œผ๋ฉด ๋ณด๋‹ค ์„ฑ๋Šฅ์ด ์šฐ์ˆ˜ํ•œ ์•ฑ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์— ๋Œ€ํ•ด์„œ Swift์˜ Collection Types์˜ Method๋‚˜ Property์˜ Time complexity์— ๋Œ€ํ•ด ์ •๋ฆฌํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ..

demian-develop.tistory.com

 

๋ฐ˜์‘ํ˜•