๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป iOSun
article thumbnail

๋ฌธ์ œ ์„ค๋ช…

 

๋ฌธ์ œ ํ’€์ด

ํ• ์ธ์œจ์ด ๋†’๋‹ค๊ณ  ์ข‹์€ ๊ฒƒ๋„ ์•„๋‹ˆ๊ณ  ๊ธฐ์ค€์•ก์ด ๋‚ฎ๋‹ค๊ณ  ์ข‹์€ ๊ฒƒ๋„ ์•„๋‹Œ ๊ฒƒ ๊ฐ™์•˜๋‹ค.

๋”ฐ๋ผ์„œ ํ• ์ธํ•˜๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋”ฐ์ง€๋ฉฐ ๊ณ„์‚ฐํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— DFS๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค.

 

 

DFS(ํ• ์ธ๊ฐœ์ˆ˜, [ํ• ์ธํผ์„ผํŠธ๋“ค])

๋กœ ๋Œ๋ฆฌ๋ฉฐ ํ• ์ธ๊ฐœ์ˆ˜๊ฐ€ ์ด๋ชจํ‹ฐ์ฝ˜ ๊ฐœ์ˆ˜๋ž‘ ๊ฐ™์„ ๋•Œ ๋ฉˆ์ถ”๊ณ 

์ด๋ชจํ‹ฐ์ฝ˜ ์‚ฌ์šฉ์ž์˜ ์ˆ˜์™€ ์ด์•ก์„ ๊ณ„์‚ฐํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ–ˆ๋‹ค.

 

 

 

 

์ฝ”๋“œ

import Foundation

func solution(_ users:[[Int]], _ emoticons:[Int]) -> [Int] {
    
    // ๊ฐ€์ž…์ž ์ˆ˜, ํŒ๋งค์•ก ๊ณ„์‚ฐํ•ด์„œ ๋ฆฌํ„ด ex) [1, 5100]
    func calcResult(_ percents: [Int]) -> [Int] {
        var answer = [0, 0]
        for i in 0..<users.count {
            var total = 0
            for j in 0..<emoticons.count {
                // ๊ธฐ์ค€ ํ• ์ธ ๊ธˆ์•ก ์ด์ƒ์ด๋ฉด ๊ตฌ๋งค
                if users[i][0] <= percents[j] {
                    total += Int(Double(emoticons[j]) * Double(100-percents[j]) * 0.01)
                }
            }
            // ๋งŒ์•ฝ ์ผ์ • ๊ธˆ์•ก ์ด์ƒ์ด๋ฉด ์ด๋ชจํ‹ฐ์ฝ˜ ํ”Œ๋Ÿฌ์Šค ๊ฐ€์ž…
            if total >= users[i][1] {
                answer[0] += 1
            }
            else {
                answer[1] += total
            }
        }
        return answer
    }
    
    var answer = [0, 0]
    let percentList = [10, 20, 30, 40]
    
    func DFS(_ count: Int, _ percents: [Int]) {
        // ์ด๋ชจํ‹ฐ์ฝ˜๋“ค์˜ ํ• ์ธ๋ฅ ์ด ๊ฒฐ์ • ๋˜์—ˆ์œผ๋ฉด
        if count == emoticons.count {
            let result = calcResult(percents)
            // ํ˜„์žฌ๋ณด๋‹ค ์ด๋ชจํ‹ฐ์ฝ˜ ๊ฐ€์ž…์ž์ˆ˜๊ฐ€ ํฌ๋ฉด ๋ฌด์กฐ๊ฑด ๋ฐ”๊ฟˆ
            if answer[0] < result[0] {
                answer = result
            }
            
            // ํ˜„์žฌ๋ณด๋‹ค ์ด๋ชจํ‹ฐ์ฝ˜ ๊ฐ€์ž…์ž์ˆ˜๋Š” ๊ฐ™์ง€๋งŒ ์ด ๊ธˆ์•ก์ด ํฐ ๊ฒฝ์šฐ ๋ฐ”๊ฟˆ
            else if answer[0] == result[0] && answer[1] < result[1] {
                answer = result
            }
        }
        else {
            // 10, 20, 30, 40 ํผ์„ผํŠธ ๊ณ„์† DFS ๋Œ๋ฆผ
            for i in 0..<4 {
                var arr = percents
                arr.append(percentList[i])
                DFS(count+1, arr)
            }
        }
    }
    
    DFS(0, [])
    
    return answer
}

 

ํ›„๊ธฐ

ํ’€์ด ์‹œ๊ฐ„ ์•ฝ 1์‹œ๊ฐ„

 

1. 

ํ• ์ธ์•ก์ด 10%, 20%, 30%, 40% ์ค‘ ํ•˜๋‚˜๋ผ๊ณ  ๋ฌธ์ œ์— ์จ์žˆ์—ˆ๋Š”๋ฐ ์ œ๋Œ€๋กœ ์•ˆ์ฝ์–ด์„œ ์บ์น˜๋ฅผ ๋ชปํ–ˆ๋‹ค..ใ…‹ใ…‹ใ…‹

์ด๊ฒƒ๋•œ์— ๋„์ €ํžˆ ๊ฐ์ด ์•ˆ์žกํ˜€์„œ ํ—ค๋งค๋‹ค๊ฐ€ ์‹œ๊ฐ„ ์žก์•„๋จน์Œ ใ… 

 

๋ฌธ์ œ๋ฅผ ์ž˜ ์ฝ์ž...

 

2.

calcResult ์ œ๋Œ€๋กœ ์งฐ๋Š”์ง€ ๋ณด๋ ค๊ณ  ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์—์„œ ํ”„๋ฆฐํŠธ ์ฐ์–ด๋ณด๋Š”๋ฐ ์ž๊พธ dump ์—๋Ÿฌ ๋œธ

์•„๋งˆ ํ…Œ์ผ€๋งˆ๋‹ค ์ด๋ชจํ‹ฐ์ฝ˜ ๊ฐœ์ˆ˜๊ฐ€ ๋‹ค๋ฅธ๋ฐ calcResult([10, 20]) ์ด๋Ÿฐ์‹์œผ๋กœ ํ• ์ธ ๊ฐœ์ˆ˜๋Š” ๊ณ ์ •ํ•ด์„œ ๊ทธ๋Ÿฐ๋“ฏ..?

๊ฒฐ๋ก ์€ ๋งž๊ฒŒ ๊ตฌํ˜„ํ–ˆ๋Š”๋ฐ ์ด๊ฒƒ๋„ ์—๋Ÿฌ๋– ์„œ ๋‹นํ™ฉํ•˜๋ฉฐ ์‹œ๊ฐ„ ๋‚ญ๋น„ ํ•จ

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์—์„œ ๋ฌธ์ œ ํ’€ ๋•Œ ๊ณ„์† ํ”„๋ฆฐํŠธํ•˜๋Š” ์Šต๊ด€์ด ์žˆ๋Š”๋ฐ ์–ด๋Š์ •๋„ ๊ตฌํ˜„ํ•˜๊ณ  ์ฐ์–ด๋ณด๋Š” ๊ฒŒ ์ข‹์„๋“ฏํ•˜๋‹ค.

profile

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป iOSun

@iosun

ํฌ์ŠคํŒ…์ด ์ข‹์•˜๋‹ค๋ฉด "์ข‹์•„์š”โค๏ธ" ๋˜๋Š” "๊ตฌ๋…๐Ÿ‘๐Ÿป" ํ•ด์ฃผ์„ธ์š”!

profile on loading

Loading...