suvera-dev ๐Ÿฅฆ

20230329 TIL ๋ณธ๋ฌธ

Private/TIL

20230329 TIL

suvera 2023. 3. 30. 03:11

์–ด์ œ ์˜ฌ๋ฆฐ TIL๊ณผ ํฌ์ŠคํŒ…์— ๋ช‡๋ช‡ ๋ถ„๋“ค์ด ์‘์›์˜ ๋Œ“๊ธ€์„ ๋‹ฌ์•„์ค˜์„œ ๋„ˆ๋ฌด ํž˜์ด ๋˜์—ˆ์Šต๋‹ˆ๋‹ค !!!

๊ฐ์‚ฌํ•ด์šฉ .. ๐Ÿฅฒ ์•ˆ๊ทธ๋ž˜๋„ ์š”์ฆ˜ ์‚ฌ๋žŒ๋„ ์ž˜ ์•ˆ๋งŒ๋‚˜๊ณ , ์ด๋Ÿฐ์ €๋Ÿฐ ๊ฑฑ์ •์— ์กฐ๊ธˆ ํž˜๋“ค์—ˆ๋Š”๋ฐ 

๋•๋ถ„์— ์˜ค๋Š˜๋„ ์—ด์‹œ๋ฏธ ๊ณต๋ถ€ ํ•˜๊ตฌ.. ํ‹ฐ์Šคํ† ๋ฆฌ๋ฅผ ์“ฐ๋Ÿฌ ์™”์Šต๋‹ˆ๋‹ค >< ! 

 

 

1. ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ’€์ด - ๋‘ ํ ํ•ฉ ๊ฐ™๊ฒŒ ๋งŒ๋“ค๊ธฐ

 

- ์ฒ˜์Œ์— ๋ฌธ์ œ๋ฅผ ๋ณด๊ณ , removeFirst ์“ฐ๋ฉด ๋ฌด์กฐ๊ฑด ์‹œ๊ฐ„์ดˆ๊ณผ ๋‚˜๊ฒ ๊ตฌ๋‚˜ ์‹ถ์–ด์„œ ํฌ์ธํ„ฐ๋ฅผ ํ™œ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.

- ๊ทธ๋Ÿฐ๋ฐ ์ด๋ ‡๊ฒŒ ํ’€์—ˆ์Œ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ .. 11๋ฒˆ์ด๋ž‘ 28๋ฒˆ์—์„œ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚˜๋„ค์š”.... ๐Ÿ˜€

    var sum1 = queue1.reduce(0,+)
    var sum2 = queue2.reduce(0,+)

    let sum = sum1 + sum2

    let target = sum / 2

    var queue1 = queue1
    var queue2 = queue2

    var moveCount = 0

    var pointer1 = 0
    var pointer2 = 0
    
    if sum % 2 != 0 {
        return -1
    }

    while sum1 != target {

        let popNum1 = queue1[pointer1]
        let popNum2 = queue2[pointer2]

        if sum1 < sum2 {
            queue1.append(popNum2)
            pointer2 += 1
            sum1 += popNum2
            sum2 -= popNum2
        } else {
            queue2.append(popNum1)
            pointer1 += 1
            sum1 -= popNum1
            sum2 += popNum1
        }

        if pointer1 >= queue1.count || pointer2 >= queue2.count {
            return -1
        }
        
        moveCount += 1
    }

    return moveCount

 

- ๊ฒฐ๊ตญ, ํ’€์ด๋ฅผ ์ฐพ์•„๋ณด๋‹ˆ ์ „๋ถ€๋‹ค 2๊ฐœ์˜ ํ๋ฅผ ํ•ฉ์ณ์„œ ํ•˜๋‚˜์˜ ๋ฐฐ์—ด๋กœ ๋งŒ๋“ ๋‹ค์Œ, ๊ฑฐ๊ธฐ์„œ ํˆฌ ํฌ์ธํ„ฐ๋ฅผ ๋Œ๋ฆฌ๋”๋ผ๊ตฌ์š” !

- ์ฐธ๊ณ ํ•ด์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ๊ฐœ์„ ํ•ด์คฌ์Šต๋‹ˆ๋‹ค ! 

    
    var sum1 = queue1.reduce(0,+)
    var sum2 = queue2.reduce(0,+)

    let sum = sum1 + sum2

    let target = sum / 2

    var queue = queue1 + queue2

    var moveCount = 0

    var pointer1 = 0
    var pointer2 = queue1.count
    
    if sum % 2 != 0 {
        return -1
    }

    while sum1 != target {
        
        if pointer2 >= queue.count || pointer1 >= pointer2 {
            return -1
        }

        let popNum1 = queue[pointer1]
        let popNum2 = queue[pointer2]

        if sum1 < sum2 {
            pointer2 += 1
            sum1 += popNum2
            sum2 -= popNum2
        } else {
            pointer1 += 1
            sum1 -= popNum1
            sum2 += popNum1
        }
        
        moveCount += 1
    }

    return moveCount

- ๋†“์ณค๋˜ ๋ถ€๋ถ„์€ ํ 2๊ฐœ๋ฅผ ์ด์–ด ๋ถ™์ด๊ฒŒ ๋˜๋ฉด, ์•ž์—์„œ ๋นผ์ฃผ๊ณ  ๋’ค๋กœ ๋ถ™์—ฌ์ฃผ๋Š” ์‹์˜ ์ด๋™์„ ๊ตฌํ˜„ํ•ด ๋‚ผ ์ˆ˜ ์žˆ๋‹ค๋Š” ์  !

- ๊ทธ๋ฆฌ๊ณ  ์–ด์ฐจํ”ผ ํ•˜๋‚˜์˜ ํ๋ฅผ ๋ชจ๋‘ ํ™•์ธํ•  ๋•Œ๊นŒ์ง€ ํ•ฉ์ด ์•ˆ๋งž์œผ๋ฉด -1์„ return ํ•˜๋ฉด ๋˜๊ธฐ ๋•Œ๋ฌธ์—

queue์˜ ์š”์†Œ๋“ค์„ ๋”ฐ๋กœ ๊ฑด๋“œ๋ฆด ํ•„์š”์—†์ด ๊ตฌํ˜„ํ•ด์•ผ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ์•ˆ๋‚œ๋‹ค๋Š” ์‚ฌ์‹ค... 

 

 

 

2. ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ’€์ด - ๋ณด์„์‡ผํ•‘

๋‘ ํ ํ•ฉ ๊ฐ™๊ฒŒ ๋งŒ๋“ค๊ธฐ์™€ ๋™์ผํ•œ ์œ ํ˜•์œผ๋กœ,

์•Œ๊ณ ๋ฆฌ์ฆ˜ ์Šคํ„ฐ๋”” ์ง€์ • ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค. 

 

ํ•ต์‹ฌ : ์ง„์—ด๋œ ๋ชจ๋“  ์ข…๋ฅ˜์˜ ๋ณด์„์„ ์ ์–ด๋„ 1๊ฐœ ์ด์ƒ ํฌํ•จํ•˜๋Š” ๊ฐ€์žฅ ์งง์€ ๊ตฌ๊ฐ„์„ ์ฐพ์•„์„œ ๊ตฌ๋งค

๊ฐ€์žฅ ์งง์€ ๊ตฌ๊ฐ„์˜ ์‹œ์ž‘ ์ง„์—ด๋Œ€ ๋ฒˆํ˜ธ์™€ ๋ ์ง„์—ด๋Œ€ ๋ฒˆํ˜ธ๋ฅผ ์ฐจ๋ก€๋Œ€๋กœ ๋ฐฐ์—ด์— ๋‹ด์•„์„œ return ํ•˜๋„๋ก ํ•˜๋ฉฐ,

๋งŒ์•ฝ ๊ฐ€์žฅ ์งง์€ ๊ตฌ๊ฐ„์ด ์—ฌ๋Ÿฌ ๊ฐœ๋ผ๋ฉด ์‹œ์ž‘ ์ง„์—ด๋Œ€ ๋ฒˆํ˜ธ๊ฐ€ ๊ฐ€์žฅ ์ž‘์€ ๊ตฌ๊ฐ„์„ return ํ•ฉ๋‹ˆ๋‹ค.

 

-> ๊ตฌ๊ฐ„์„ ์ฐพ๋Š” ๋ฌธ์ œ์ด๋‹ˆ ํˆฌ ํฌ์ธํ„ฐ๋‚˜ ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ๋ฅผ ์ด์šฉํ•˜๋ฉด ๋  ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐํ–ˆ๊ณ ,

ํฌ๊ธฐ๊ฐ€ ๊ณ ์ •๋˜์–ด์žˆ์ง€ ์•Š์œผ๋‹ˆ ํˆฌํฌ์ธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ’€์–ด์•ผ๊ฒ ๋‹ค ์ƒ๊ฐ์„ ํ•ด์จ์š”.

 

๊ทธ๋Ÿฐ๋ฐ ์œ„์˜ ๋ฌธ์ œ์˜ ๊ฒฝ์šฐ sum์œผ๋กœ ๋”ํ•˜๊ณ  ๋นผ์ฃผ๋ฉด ๋˜์ง€๋งŒ,

๋ณด์„ ์‡ผํ•‘์˜ ๊ฒฝ์šฐ String๊ฐ’์ด๊ธฐ ๋•Œ๋ฌธ์— ์–ด๋–ป๊ฒŒ ๊ด€๋ฆฌ๋ฅผ ํ•ด์ฃ ์•ผ ํ• ์ง€ ๊ณ ๋ฏผํ–ˆ์Šต๋‹ˆ๋‹ค.

 

์ฒ˜์Œ์—๋Š” ๋งค๋ฒˆ start์™€ end๋ฅผ updateํ•˜๋ฉด์„œ Set(gems[start...end]) ๋ฅผ ๊ณ„์‚ฐํ•ด์„œ ํฌํ•จ๋˜์–ด์žˆ๋Š”์ง€ ํ™•์ธํ–ˆ์Šต๋‹ˆ๋‹ค.

๊ทผ๋ฐ ๋‘ํํ•ฉ๊ฐ™ ํ’€๋•Œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ while ๋ฌธ ๋‚ด๋ถ€์—์„œ ์ €๋ ‡๊ฒŒ ๋งค๋ฒˆ Set์„ ๋งŒ๋“ค์–ด๋‚ธ๋‹ค๋ฉด,

์‹œ๊ฐ„ ๋ณต์žก๋„๊ฐ€ ์ตœ์•…์˜ ๊ฒฝ์šฐ O(N^2) ์ด ๋˜์–ด๋ฒ„๋ฆฝ๋‹ˆ๋‹ค. ( GPT๊ฐ€ ์•Œ๋ ค์คŒ ! ๋˜‘๋˜‘์ž‰ )

 

๊ทธ๋ž˜์„œ ํฌ์ธํ„ฐ๋ฅผ ์˜ฎ๊ธฐ๋ฉด์„œ ์ƒˆ๋กญ๊ฒŒ ์ถ”๊ฐ€๋˜๊ฑฐ๋‚˜ ์ œ๊ฑฐ๋˜๋Š” ๋ณด์„์„ ๋‹ด์„ ๋ณ„๋„์˜ ์ž๋ฃŒํ˜•์ด ํ•„์š”ํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค.

GPT์˜ ํžŒํŠธ๋ฅผ ์ฐธ๊ณ ํ•ด์„œ, Dictionary๋ฅผ ๋งŒ๋“ค์–ด์คฌ์Šต๋‹ˆ๋‹ค. 

๊ทธ๋ฆฌ๊ณ  ๋ณด์„์˜ ์ข…๋ฅ˜๊ฐ€ ๋ถ€์กฑํ•˜๋ฉด right๋ฅผ ๋Š˜๋ ค์ฃผ๊ณ , ์ถฉ๋ถ„ํ•˜๋ฉด left๋ฅผ ๋Š˜๋ ค์ฃผ๋Š” ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค.

์ด ๊ณผ์ •์—์„œ Dictionary์— ๋ณด์„์˜ ์ข…๋ฅ˜๋ฅผ ์ €์žฅํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค !

๋งค๋ฒˆ Set์„ ๋งŒ๋“ค์ง€ ์•Š๊ณ  ๋”•์…”๋„ˆ๋ฆฌ ๊ฐ’์„ ์กฐํšŒ๋งŒ ํ•ด์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์š”์ฆ˜์—” ๋ฐ”๋กœ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด์˜ ์ „์ฒด๋ฅผ ๋ณด๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, GPT๋ฅผ ํ™œ์šฉํ•ด์„œ 

์ €์˜ ๋‹ต์•ˆ์—์„œ ํ‹€๋ฆฐ ์ ์„ ์•Œ๋ ค๋‹ฌ๋ผ๊ณ  ํ•œ๋‹ค์Œ์— ๊ทธ๊ฑธ ๋ฐ”ํƒ•์œผ๋กœ ๊ณ ์ณ๋ณด๋Š” ์‹์œผ๋กœ ํ•˜๊ณ  ์žˆ์–ด์š” !

 

func solution(_ gems:[String]) -> [Int] {
    let gemSet = Set(gems)
    var gemDict = [String: Int]()
    var left = 0
    var right = 0
    var minLength = gems.count
    var result: [Int] = [left+1, gems.count]
    
    while true {
        if gemDict.count == gemSet.count {
            // ํ˜„์žฌ ๊ตฌ๊ฐ„์ด ๋ชจ๋“  ๋ณด์„์„ ํฌํ•จํ•˜๋Š” ๊ฒฝ์šฐ
            if right - left < minLength {
                minLength = right - left
                result = [left+1, right]
            }
            
            if let count = gemDict[gems[left]] {
                if count == 1 {
                    gemDict.removeValue(forKey: gems[left])
                } else {
                    gemDict[gems[left]] = count - 1
                }
            }
            left += 1
        } else if right == gems.count {
            break
        } else {
            // ํ˜„์žฌ ๊ตฌ๊ฐ„์ด ๋ชจ๋“  ๋ณด์„์„ ํฌํ•จํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ
            if let count = gemDict[gems[right]] {
                gemDict[gems[right]] = count + 1
            } else {
                gemDict[gems[right]] = 1
            }
            right += 1
        }
    }
    
    return result
}

์ด๋ ‡๊ฒŒ ํ–ˆ๋”๋‹ˆ, ํ†ต๊ณผํ–ˆ์Šต๋‹ˆ๋‹ค ! ํ•˜์ง€๋งŒ, ์ฝ”๋“œ๋ฅผ ๋” ๊น”๋”ํ•˜๊ฒŒ ๊ณ ์น  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ€ํƒ€์š”.

๋‹ค์Œ์— ๋‹ค์‹œ ํ’€์–ด๋ด์•ผ๊ฒ ์–ด์š” ! 

 

์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ณต๋ถ€๋ฅผ ์—ด์‹ฌํžˆ ํ•˜๊ณ  ์žˆ๊ธดํ•œ๋ฐ ์ƒ๊ฐ๋ณด๋‹ค ํ•˜๋ฃจ์— ๋งŽ์€ ๋ฌธ์ œ๋ฅผ ํ’€์ง„ ๋ชปํ•˜๋„ค์š”.. ใ…œใ…œ

์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋˜๋Š” ๊ฒƒ๋„ ๋„ˆ๋ฌด ๋งŽ๊ณ . ๋ฏธ๋ฆฌ๋ฏธ๋ฆฌ ์•ˆํ•œ๊ฑธ ๋งŽ์ด ํ›„ํšŒ์ค‘์ด์—์—ฌ.. ์ฉ.. 

ํ•˜์ง€๋งŒ ๊ณผ๊ฑฐ์˜ ์ž˜๋ชป์€ ๋นจ๋ฆฌ ์ฒญ์‚ฐํ•˜๊ณ  ใ…‹ใ…‹ ์ด์ œ์„œ๋ผ๋„ ์—ด์‹ฌํžˆ ํ’€์–ด์„œ ๋‹คํ–‰์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๊ธฐ๋กœ.. 

Lv3์€ ์•„์ง ํ˜ผ์ž ํž˜์œผ๋กœ ํ’€๊ธฐ ์–ด๋ ต๋„ค์š” ๐Ÿ˜…

 

 

 

 

3. SwiftUI ํŠœํ† ๋ฆฌ์–ผ 

https://suvera.tistory.com/56

 

SwiftUI) SwiftUI Essentials

์ตœ๊ทผ์— ๊ฐœ๋ฐœ ์ปจํผ๋Ÿฐ์Šค์— ๊ฐ”๋‹ค๊ฐ€ ์Šค์œ  ๋ฝ๋ฟŒ๊ฐ€ ์™€์„œ ์Šค์œ  ๊ณต๋ถ€๋ฅผ ์‹œ์ž‘ํ–ˆ์–ด์š”. ์ƒˆ๋กœ์šด ๊ฑธ ๊ณต๋ถ€ํ•˜๋Š” ๊ฑด ์—ญ์‹œ ์žฌ๋ฐŒ๋„ค์šค ์ž๋ฃŒ๋Š” ์• ํ”Œ ๊ณต์‹ ๋ฌธ์„œ์— ์žˆ๋Š” SwiftUI Tutorials๋กœ ํ•˜๋ ค๊ตฌ์š”. ์šฐ์„  ํ•œ๋ฒˆ ์ „์ฒด์ ์œผ

suvera.tistory.com

https://suvera.tistory.com/57

 

SwiftUI) SwiftUI Essentials - 2

SwiftUI Essentials ์ฑ•ํ„ฐ์˜ 2๋ฒˆ์งธ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค. Building Lists and Navigation - ๊ธฐ๋ณธ ๋žœ๋“œ๋งˆํฌ ์ƒ์„ธ ๋ทฐ๋ฅผ ๋งŒ๋“ค์—ˆ์œผ๋‹ˆ ์ „์ฒด ๋ชฉ๋ก๊ณผ ๊ทธ ๊ฐ๊ฐ์˜ ํ•ญ๋ชฉ์— ๋Œ€ํ•œ ์ƒ์„ธ ์œ„์น˜๋ฅผ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋„๋ก ๋งŒ๋“ค์–ด์ค„๊ฒŒ์š” ! - ๋ชจ๋“ 

suvera.tistory.com

์œ ์ง„์–ธ๋‹ˆ๋ž‘ ๊ฐ™์ด ์Šค์œ  ํŠœํ† ๋ฆฌ์–ผ ํ•˜๋ฃจ ํ•œ ์ฑ•ํ„ฐ์”ฉ 4์ผ์•ˆ์— ๋ฝ€๊ฐœ๊ธฐ ! ใ…‹ใ…‹ 

์˜ค๋Š˜ ์ฑ•ํ„ฐ1 ๊นŒ์ง€ ํ–ˆ์Šต๋‹ˆ๋‹ค์š”. ์ •๋ฆฌํ•˜๋ฉด์„œ ํ•˜๋‹ˆ๊นŒ ์ฃ„๊ธˆ ๊ท€์ฐฎ๊ธด ํ•œ๋ฐ ๋” ์ดํ•ด๊ฐ€ ์ž˜๋˜๋„ค์šฉ.

๊ทธ๋ฆฌ๊ณ  ์—ญ์‹œ ๋ญ”๊ฐ€ ํ• ๋• ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์ด๋ž‘ ๊ฐ™์ด ํ•˜๋Š”๊ฑธ ๋” ์ข‹์•„ํ•˜๋Š” ๊ฒƒ ๊ฐ™์•„์š”..

์š”์ฆ˜์—” ๊ฑฐ์˜ ์Šคํ„ฐ๋”” ๊ตฌ๊ฑธ์ค‘.. ์ œ๋ฐœ ๋‚˜๋ž‘ ์Šคํ„ฐ๋”” ํ•ด์ค˜ .. ์ด๋Ÿฐ ๋Š๋‚Œ.. ? 

 

SOPT ํ• ๋•Œ๋Š” ์ •๋ง ์Šคํ„ฐ๋””ํ•  ๊ธฐํšŒ๊ฐ€ ๋„ˆ๋ฌด ๋งŽ์•˜๋Š”๋ฐ, ์š”์ฆ˜์—” ๋งˆ์Œ์— ๋งž๋Š” ์Šคํ„ฐ๋””์› ๊ตฌํ•˜๊ธฐ๊ฐ€ 

ํ•˜๋Š˜์˜ ๋ณ„๋”ฐ๊ธฐ์ธ๋“ฏ ํ•ฉ๋‹ˆ๋‹ค.. ๊ทธ๋ž˜๋„ ์Šค์•Œ๋ฟŒ ์‚ฌ๋žŒ๋“ค์ด ์žˆ์–ด์„œ ๋‹คํ–‰์ด์—์š”.. ๐Ÿ˜†

SOPT ์‚ฌ๋žŒ๋“ค ๋งŒํผ ์—ด์ •์ ์ธ ์‚ฌ๋žŒ๋“ค๋กœ ์Šคํ„ฐ๋””๋ฅผ ๊ตฌ์„ฑํ•˜๊ธฐ๊ฐ€ ์ •๋ง ํž˜๋“  ๊ฒƒ ๊ฐ™๋„ค์š”.. !!

๊ทธ๋ž˜์„œ ์ด๋ฒˆ ๋น„ํ™œ๋กœ ์Šคํ„ฐ๋”” ํ•œ๋ฒˆ ์ฐธ์—ฌํ•ด๋ณผ๊นŒํ–ˆ๋Š”๋ฐ ๊ฑฐ์˜ ๋‹ค ํ™œ๋™๊ธฐ์ˆ˜ ์ œํ•œ์ด๋”๋ผ๊ตฌ์š” ใ…‹ใ…‹ใ…‹

์ง์ ‘ ์—ด๊ธฐ์—๋Š” ๋˜ ๋ถ€๋‹ด์Šค๋Ÿฝ๊ธฐ๋„ ํ•˜๊ณ .. ๋‚ด์ผ๊นŒ์ง€๋‹ˆ ํ•œ๋ฒˆ ๊ณ ๋ฏผํ•ด๋ด์•ผ๊ฒŒ์จ์š”.. 

 

 

๋Šฆ์—ˆ์ง€๋งŒ, ๋‹ค๋“ค ์˜ค๋Š˜ ํ•˜๋ฃจ ๊ณ ์ƒํ•˜์…จ์Šต๋‹ˆ๋‹ค ๐Ÿ˜Ž

'Private > TIL' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

20230402 TIL  (0) 2023.04.02
20230401 TIL  (0) 2023.04.02
20230331 TIL  (1) 2023.04.01
20230330 TIL  (2) 2023.03.31
20230328 TIL  (6) 2023.03.29
Comments