[๐Ÿ“ iOS TIL] #15 ... Algorithm ๋ฌธ์ œ ํ’€์ด ๐Ÿถ

TaeUkยท2024๋…„ 3์›” 21์ผ
0

๐ŸŽ iOS TIL

๋ชฉ๋ก ๋ณด๊ธฐ
14/18
post-thumbnail

[level 1] ํ–‰๋ ฌ์˜ ๋ง์…ˆ - 12950

  • ๋ฌธ์ œ ์„ค๋ช…
    ํ–‰๋ ฌ์˜ ๋ง์…ˆ์€ ํ–‰๊ณผ ์—ด์˜ ํฌ๊ธฐ๊ฐ€ ๊ฐ™์€ ๋‘ ํ–‰๋ ฌ์˜ ๊ฐ™์€ ํ–‰, ๊ฐ™์€ ์—ด์˜ ๊ฐ’์„ ์„œ๋กœ ๋”ํ•œ ๊ฒฐ๊ณผ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. 2๊ฐœ์˜ ํ–‰๋ ฌ arr1๊ณผ arr2๋ฅผ ์ž…๋ ฅ๋ฐ›์•„, ํ–‰๋ ฌ ๋ง์…ˆ์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.
๋ฌธ์ œ ํ’€์ด

func solution(_ arr1:[[Int]], _ arr2:[[Int]]) -> [[Int]] {
    var sumArr = [Int]()
    var ansArr = [[Int]]()
    
    for i in 0..<arr1.count {
        for j in 0..<arr1[0].count {
            sumArr.append(arr1[i][j] + arr2[i][j])
        }
        ansArr.append(sumArr)
        
        sumArr = []
    }
    
    return ansArr
}

๊ฐ€๊ธ‰์  ๊ณ ์ฐจํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ œ ํ’€์ด๋ฅผ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ž‘์„ฑํ•ด๋ณด๊ณ  ์‹ถ์—ˆ๋‹ค.

ํ•˜์ง€๋งŒ ๊ทธ๋ ‡๊ฒŒ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ์—ฌ๋Ÿฌ ๊ณผ์ •์„ ๊ฑฐ์ณค์ง€๋งŒ, ๊ฒฐ๊ตญ์—” ์‹คํŒจํ•˜์˜€๋‹ค...

๊ทธ๋ž˜์„œ ๋‹จ์ˆœํ•˜๊ฒŒ ๊ฐ ํ–‰๋ ฌ์˜ ์ธ๋ฑ์Šค ๊ฐ’์„ ๋”ํ•˜์—ฌ sumArr์— ์ €์žฅํ•˜์—ฌ ์ตœ์ข… ๋‹ต์•ˆ์„ ์ €์žฅํ•˜๋Š” ansArr์— ์ €์žฅํ•ด์ฃผ์—ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๋งˆ์ง€๋ง‰์œผ๋กœ sumArr๋ฅผ ๋‹ค์‹œ ์ดˆ๊ธฐํ™”ํ•˜์—ฌ ๋ถˆํ•„์š”ํ•œ ๊ฐ’์ด ๋ˆ„์ ๋˜์ง€ ์•Š๋„๋ก ๋งŒ๋“ค์—ˆ๋‹ค.


[level 1] 2018โ€…KAKAOโ€…BLINDโ€…RECRUITMENT [1์ฐจ] ๋น„๋ฐ€์ง€๋„ - 17681

๋ฌธ์ œ ํ’€์ด

func solution(_ n: Int, _ arr1: [Int], _ arr2: [Int]) -> [String] {
    var result = [String]()
    
    for i in 0..<n {
        var line = ""
        
        for j in 0..<n {
            let index = n - 1 - j
            let bit1 = (arr1[i] >> index) & 1
            let bit2 = (arr2[i] >> index) & 1
            let bit = bit1 | bit2
            
            line += (bit == 1) ? "#" : " "
        }
        
        result.append(line)
    }
    
    return result
}

๋งจ ์ฒ˜์Œ ๋ฌธ์ œ๋ฅผ ํ’€๊ณ , ๋‹ค์Œ์œผ๋กœ ํ’€ ๋ฌธ์ œ๋ฅผ ์ฐพ๋‹ค๊ฐ€ ์žฌ๋ฐŒ๋Š” ๋ฌธ์ œ๋ฅผ ์ฐพ์•˜๋‹ค!

์ง€๊ธˆ๊นŒ์ง€ ํ‘ผ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ๋ฌธ์ œ์™€ ๊ตฌ์„ฑ์ด ๋‹ค๋ฅธ ๊ฒƒ์— ํฅ๋ฏธ๋ฅผ ๋Š๊ผˆ๋‹ค!

์œ„ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด ๊ฐ๊ฐ์˜ ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค ๊ฐ’์— ์ ‘๊ทผํ•˜์—ฌ ๋น„ํŠธ ์—ฐ์‚ฐ์ž๋ฅผ ํ™œ์šฉํ•œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

๊ทธ๋ฆญ ์—ฐ์‚ฐํ•œ ๋น„ํŠธ๊ฐ’์„ ์‚ผํ•ญ์—ฐ์‚ฐ์ž๋ฅผ ํ™œ์šฉํ•˜์—ฌ "#"์™€ " "์„ ๊ตฌ๋ถ„ํ•˜์˜€๋‹ค.


var ansArr: [String] = []
var ans: String = ""
    
var map1 = arr1.map{ String($0, radix: 2) }
var map2 = arr2.map{ String($0, radix: 2) }
    
for i in 0..<n {
	while map1[i].count != n {
		map1[i].insert("0", at: map1[i].startIndex)
	}
	while map2[i].count != n {
		map2[i].insert("0", at: map2[i].startIndex)
	}
}    

์ฒ˜์Œ๋ถ€ํ„ฐ ์ €๋Ÿฌํ•œ ๋ฐฉ์‹์œผ๋กœ ์ ‘๊ทผํ•œ ๊ฒƒ์ด ์•„๋‹Œ, ๊ฐ ๋ฐฐ์—ด์˜ ๊ฐ’์„ radix๋ฅผ ํ†ตํ•ด 2์ง„์ˆ˜๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ํ™œ์šฉํ•˜์˜€๋‹ค.

์ดํ›„, 2์ง„์ˆ˜๋กœ ๋ณ€ํ™˜๋œ ๊ฐ’ ์ค‘์—์„œ n์˜ ์ž๋ฆฟ์ˆ˜ ๋งŒํผ ๋น„ํŠธ ๊ธธ์ด๊ฐ€ ์งง์€ ๊ฐ’์€ รฌnsert`๋ฅผ ํ™œ์šฉํ•˜์—ฌ 0์„ ์ถ”๊ฐ€ํ•ด์ฃผ์—ˆ๋‹ค.

์žฌ๋ฐŒ์–ด ๋ณด์—ฌ ๋ฌธ์ œ ํ’€์ด๋ฅผ ์‹œ๋„ํ•˜์˜€๋Š”๋ฐ, ์ƒ๊ฐ๋ณด๋‹ค ๋„ˆ๋ฌด ์–ด๋ ค์› ๋‹ค...

๊ทธ๋ฆฌ๊ณ  ๋ฌธ์ œ๋ฅผ ๋‹ค ํ’€๊ณ , ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์˜ ํ’€์ด๋ฅผ ๋ณด๋‹ˆ ์•„์ง ๊ฐˆ ๊ธธ์ด ๋ฉ€๋‹ค๋Š”๊ฑธ ๋Š๋‚„ ์ˆ˜ ์žˆ์—ˆ๋‹ค... ๐Ÿ™ˆ

0๊ฐœ์˜ ๋Œ“๊ธ€