[๐Ÿ“ iOS TIL] #16 ... Algorithm ๋ฌธ์ œ ํ’€์ด ๐Ÿปโ€โ„๏ธ

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

๐ŸŽ iOS TIL

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

[level 1] 3์ง„๋ฒ• ๋’ค์ง‘๊ธฐ - 68935

  • ๋ฌธ์ œ ์„ค๋ช…
    ์ž์—ฐ์ˆ˜ n์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. n์„ 3์ง„๋ฒ• ์ƒ์—์„œ ์•ž๋’ค๋กœ ๋’ค์ง‘์€ ํ›„, ์ด๋ฅผ ๋‹ค์‹œ 10์ง„๋ฒ•์œผ๋กœ ํ‘œํ˜„ํ•œ ์ˆ˜๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.
๋ฌธ์ œ ํ’€์ด

import Foundation

func solution(_ n:Int) -> Int {
    var inVal = String(n, radix: 3)
    var ansArr = inVal.map{ Int(String($0))! }
    var ansVal: Int = 0
    
    for i in 0..<ansArr.count {
        ansVal += ansArr[i] * Int(pow(3.0, Double(i)))
    }
    
    return ansVal
}

๋งจ ์ฒ˜์Œ ๋ฌธ์ œ๋ฅผ ๋ดค์„ ๋•Œ, ํ•ด๋‹น ํ’€์ด์™€๋Š” ๋‹ค๋ฅด๊ฒŒ ์ ‘๊ทผํ•˜์˜€๋‹ค.

์ž…๋ ฅ๊ฐ’(10์ง„๋ฒ•)์„ 3์ง„๋ฒ• ์ˆ˜๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๊ฐ’์„ ๋’ค์ง‘๊ณ , ์ดํ›„ ๋‹ค์‹œ 10์ง„๋ฒ•์œผ๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ์ตœ์ข…๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๊ตฌ์ƒํ•˜์˜€๋‹ค.

var inVal = String(n, radix: 3)
var ansArr = inVal.map{ Int(String($0))! }
ansArr.reverse()
var ansStr = Int(ansArr.map(String.init).joined())!

์œ„ ์ฝ”๋“œ์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋“ฏ์ด 3์ง„๋ฒ• ์ˆ˜๋กœ ๋ณ€ํ™˜๋œ ๊ฐ’์„ ๋’ค์ง‘์–ด 10์ง„๋ฒ•์œผ๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ ์ „ ๋‹จ๊ณ„๊นŒ์ง€ ์˜จ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

ํ•˜์ง€๋งŒ ํ•ด๋‹น ํŒŒํŠธ์—์„œ ์ง„์ „์ด ์—†์–ด ๋งจ ์œ„์˜ ํ’€์ด ๋ฐฉ๋ฒ•์œผ๋กœ ์ˆ˜์ •ํ•˜์˜€๋‹ค.

์ž…๋ ฅ๊ฐ’์„ mapํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ [Int] ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜ํ•ด์ฃผ๊ณ , ์ฒซ๋ฒˆ์งธ ์ธ๋ฑ์Šค ๊ฐ’๋ถ€ํ„ฐ 3์˜ ์ œ๊ณฑ๊ฐ’์„ ๊ณฑํ•˜์—ฌ ์ฃผ์—ˆ๋‹ค.

๊ทธ๋Ÿฌ๋ฉด ๊ตณ์ด ์ž…๋ ฅ๊ฐ’์„ ๋’ค์ง‘์„ ํ•„์š”๋„ ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค!


[level 1] ์ง์‚ฌ๊ฐํ˜• ๋ณ„์ฐ๊ธฐ - 12969

  • ๋ฌธ์ œ ์„ค๋ช…
    ์ด ๋ฌธ์ œ์—๋Š” ํ‘œ์ค€ ์ž…๋ ฅ์œผ๋กœ ๋‘ ๊ฐœ์˜ ์ •์ˆ˜ n๊ณผ m์ด ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.
    ๋ณ„(*) ๋ฌธ์ž๋ฅผ ์ด์šฉํ•ด ๊ฐ€๋กœ์˜ ๊ธธ์ด๊ฐ€ n, ์„ธ๋กœ์˜ ๊ธธ์ด๊ฐ€ m์ธ ์ง์‚ฌ๊ฐํ˜• ํ˜•ํƒœ๋ฅผ ์ถœ๋ ฅํ•ด๋ณด์„ธ์š”.
๋ฌธ์ œ ํ’€์ด

import Foundation

let n = readLine()!.components(separatedBy: [" "]).map { Int($0)! }
let (a, b) = (n[0], n[1])

var ans: String = ""

for i in 0..<a {
    ans.append("*")
}

for j in 0..<b {
    print(ans)
}

์œ„์˜ ํ’€์ด๋ณด๋‹ค ๊ดœ์ฐฎ์€ ๋ฐฉ๋ฒ•์„ ์ฐพ์•„๋ดค์ง€๋งŒ, ์•„์‰ฝ๊ฒŒ๋„ ์‹์ƒํ•œ ๋ฐฉ์‹์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ๋‹ค...

for๋ฌธ์„ 2๋ฒˆ ํ™œ์šฉํ•˜์—ฌ, * ๋ฌธ์ž๋ฅผ ans์— ์ถ”๊ฐ€ํ•˜์—ฌ ๊ฐ€๋กœ ๊ธธ์ด๋ฅผ ์ €์žฅํ•ด์ฃผ๊ณ  ์„ธ๋กœ ๊ธธ์ด์— ๋งž์ถฐ ์—ฌ๋Ÿฌ๋ฒˆ ์ถœ๋ ฅํ•ด์ฃผ์—ˆ๋‹ค.

๋‹ค์Œ์— ๋”์šฑ ๊น”๋”ํ•œ ํ’€์ด ๋ฐฉ๋ฒ•์„ ์ฐพ์•„๋ด์•ผ๊ฒ ๋‹ค.


[level 1] ๋ฌธ์ž์—ด ๋‹ค๋ฃจ๊ธฐ ๊ธฐ๋ณธ - 12918

  • ๋ฌธ์ œ ์„ค๋ช…
    ๋ฌธ์ž์—ด s์˜ ๊ธธ์ด๊ฐ€ 4 ํ˜น์€ 6์ด๊ณ , ์ˆซ์ž๋กœ๋งŒ ๊ตฌ์„ฑ๋ผ์žˆ๋Š”์ง€ ํ™•์ธํ•ด์ฃผ๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•˜์„ธ์š”. ์˜ˆ๋ฅผ ๋“ค์–ด s๊ฐ€ "a234"์ด๋ฉด False๋ฅผ ๋ฆฌํ„ดํ•˜๊ณ  "1234"๋ผ๋ฉด True๋ฅผ ๋ฆฌํ„ดํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.
๋ฌธ์ œ ํ’€์ด

func solution(_ s:String) -> Bool {
    var ansCounter: Int = 0
    var mapArr = s.map{ Int(String($0)) }
    
    !mapArr.contains(nil) ? (ansCounter += 1) : (ansCounter += 0)
    mapArr.count == 4 || mapArr.count == 6 ? (ansCounter += 1) : (ansCounter += 0)
    
    return ansCounter == 2 ? true : false
}

์˜ค๋Š˜ ํ‘ผ ๋ฌธ์ œ ์ค‘์—์„œ ํ•ด๋‹น ๋ฌธ์ œ๋ฅผ ๊ฐ€์žฅ ์ž˜ ํ‘ผ ๊ฒƒ ๊ฐ™๋‹ค!

์šฐ์„  ์ž…๋ ฅ๊ฐ’์„ map ํ•จ์ˆ˜๋ฅผ ํ™œ์šฉํ•˜์—ฌ, [Int]์œผ๋กœ ๋ณ€ํ™˜ํ•˜์˜€๋‹ค. ์ด๋•Œ, ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ๊ธ€์ž๊ฐ€ ์ž…๋ ฅ๋˜๋ฉด nil๊ฐ’์ด ์ €์žฅ๋œ๋‹ค.

๋”ฐ๋ผ์„œ ์ด๋Ÿฌํ•œ ์ ์„ ํ™œ์šฉํ•˜์—ฌ ๋ฐฐ์—ด ๋‚ด์— nil์ด ์กด์žฌํ•˜๋ฉด ์นด์šด๋”์— 1์ด ์ถ”๊ฐ€๋˜๋„๋ก ํ•˜์˜€๊ณ , ๋ฐฐ์—ด์˜ ๊ธธ์ด๊ฐ€ 4๋‚˜ 6์ผ ๋•Œ ์นด์šด๋”๊ฐ€ ํ•œ๋ฒˆ ๋” ๋™์ž‘ํ•˜๋„๋ก ๋งŒ๋“ค์—ˆ๋‹ค.

๋งˆ์ง€๋ง‰์œผ๋กœ ์นด์šดํ„ฐ๊ฐ€ 2์ผ ๋•Œ, ์‚ผํ•ญ์—ฐ์‚ฐ์ž๋ฅผ ํ™œ์šฉํ•˜์—ฌ true๊ฐ€ ๋ฐ˜ํ™˜๋˜๋„๋ก ๋งŒ๋“ค์—ˆ๋‹ค.

ํ•ด๋‹น ๋ฌธ์ œ๋ฅผ ์ œ์ถœํ•˜๊ณ  ์ง€๊ธˆ๊นŒ์ง€ ๋ฐ›์•„๋ณธ์  ์—†๋Š” ๋ฌด๋ ค 12์ ์„ ๋ฐ›์•˜๋‹ค!!!

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