๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
3๏ธโƒฃ Swift/Problem Solving

[Swift Algorithm] ๋ชจ์˜๊ณ ์‚ฌ Programmers(Lv.1)

by seolhee2750 2021. 6. 18.
๋ฌธ์ œ ์„ค๋ช…

์ˆ˜ํฌ์ž๋Š” ์ˆ˜ํ•™์„ ํฌ๊ธฐํ•œ ์‚ฌ๋žŒ์˜ ์ค€๋ง์ž…๋‹ˆ๋‹ค. ์ˆ˜ํฌ์ž ์‚ผ์ธ๋ฐฉ์€ ๋ชจ์˜๊ณ ์‚ฌ์— ์ˆ˜ํ•™ ๋ฌธ์ œ๋ฅผ ์ „๋ถ€ ์ฐ์œผ๋ ค ํ•ฉ๋‹ˆ๋‹ค. ์ˆ˜ํฌ์ž๋Š” 1๋ฒˆ ๋ฌธ์ œ๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰ ๋ฌธ์ œ๊นŒ์ง€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฐ์Šต๋‹ˆ๋‹ค.

1๋ฒˆ ์ˆ˜ํฌ์ž๊ฐ€ ์ฐ๋Š” ๋ฐฉ์‹: 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, ...
2๋ฒˆ ์ˆ˜ํฌ์ž๊ฐ€ ์ฐ๋Š” ๋ฐฉ์‹: 2, 1, 2, 3, 2, 4, 2, 5, 2, 1, 2, 3, 2, 4, 2, 5, ...
3๋ฒˆ ์ˆ˜ํฌ์ž๊ฐ€ ์ฐ๋Š” ๋ฐฉ์‹: 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, ...

1๋ฒˆ ๋ฌธ์ œ๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰ ๋ฌธ์ œ๊นŒ์ง€์˜ ์ •๋‹ต์ด ์ˆœ์„œ๋Œ€๋กœ ๋“ค์€ ๋ฐฐ์—ด answers๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๊ฐ€์žฅ ๋งŽ์€ ๋ฌธ์ œ๋ฅผ ๋งžํžŒ ์‚ฌ๋žŒ์ด ๋ˆ„๊ตฌ์ธ์ง€ ๋ฐฐ์—ด์— ๋‹ด์•„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

 

์ œํ•œ ์กฐ๊ฑด
  • ์‹œํ—˜์€ ์ตœ๋Œ€ 10,000 ๋ฌธ์ œ๋กœ ๊ตฌ์„ฑ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค.
  • ๋ฌธ์ œ์˜ ์ •๋‹ต์€ 1, 2, 3, 4, 5์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค.
  • ๊ฐ€์žฅ ๋†’์€ ์ ์ˆ˜๋ฅผ ๋ฐ›์€ ์‚ฌ๋žŒ์ด ์—ฌ๋Ÿฟ์ผ ๊ฒฝ์šฐ, returnํ•˜๋Š” ๊ฐ’์„ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•ด์ฃผ์„ธ์š”.

 

์ž…์ถœ๋ ฅ ์˜ˆ
answers return
[1,2,3,4,5] [1]
[1,3,2,4,2] [1,2,3]

 

๋‚ด ๋ฌธ์ œ ํ’€์ด
func solution(_ answers:[Int]) -> [Int] {
    let stu1 = [1, 2, 3, 4, 5]
    let stu2 = [2, 1, 2, 3, 2, 4, 2, 5]
    let stu3 = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5]
    var count = [0, 0, 0]
    var result = [Int]()
    
    for i in 0..<answers.count {
        if answers[i] == stu1[i % 5] {
            count[0] += 1
        }
        if answers[i] == stu2[i % 8] {
            count[1] += 1
        }
        if answers[i] == stu3[i % 10] {
            count[2] += 1
        }
    }
    
    for i in 0..<3 {
        if count.max() == count[i] {
            result.append(i+1)
        }
    }
    
    return result.sorted()
}
  • ๋จผ์ € ์„ธ ํ•™์ƒ์˜ ์ฐ๊ธฐ ํŒจํ„ด์„ ๋ฐฐ์—ด๋กœ ์ƒ์„ฑํ•ด์ฃผ์—ˆ๋‹ค.
  • ๋‹ต์•ˆ ๋ฐฐ์—ด์˜ ๊ธธ์ด๋งŒํผ ์„ธ ํ•™์ƒ์˜ ํŒจํ„ด์„ ๋Œ๋ ค์ฃผ๋ฉฐ ์ •๋‹ต์ด ๋งž๋Š” ํšŸ์ˆ˜๋ฅผ count์— ์ถ”๊ฐ€ํ•ด์ฃผ์—ˆ๋‹ค.
  • for๋ฌธ์œผ๋กœ ์ œ์ผ ๋งŽ์ด ๋งž์ถ˜ ์‚ฌ๋žŒ์ด ๋ˆ„๊ตฌ์ธ์ง€, ์—ฌ๋Ÿฌ๋ช…์ด๋ผ๋ฉด ๋˜ ๋ˆ„๊ตฌ์ธ์ง€,, result์— ์ถ”๊ฐ€ํ•ด์ฃผ์—ˆ๋‹ค.

 

์ธ์ƒ์ ์ธ ํ’€์ด
import Foundation

func solution(_ answers:[Int]) -> [Int] {
    let answer = (
        a: [1, 2, 3, 4, 5], // index % 5
        b: [2, 1, 2, 3, 2, 4, 2, 5], // index % 8
        c: [3, 3, 1, 1, 2, 2, 4, 4, 5, 5] // index % 10
    )
    var point = [1:0, 2:0, 3:0]

    for (i, v) in answers.enumerated() {
        if v == answer.a[i % 5] { point[1] = point[1]! + 1 }
        if v == answer.b[i % 8] { point[2] = point[2]! + 1 }
        if v == answer.c[i % 10] { point[3] = point[3]! + 1 }
    }

    return point.sorted{ $0.key < $1.key }.filter{ $0.value == point.values.max() }.map{ $0.key }
}
  • dictionary๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ’€์ดํ–ˆ๋‹ค.
  • filter๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ œ์ผ ๋งŽ์ด ๋งž์ถ˜ ํ•™์ƒ์„ ๊ตฌํ•ด์ฃผ์—ˆ๋‹ค.

 

๐Ÿ’ก ํ”ผ๋“œ๋ฐฑ
  • ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ํ•œ ๋ฒˆ๋„ ์‚ฌ์šฉํ•ด ๋ณธ์ ์ด ์—†์–ด์„œ ์ƒ๊ฐํ•˜์ง€ ๋ชปํ•œ ํ’€์ด์ธ๋ฐ, ๊ณต๋ถ€ํ•ด๋ด์•ผ ํ•  ๊ฒƒ ๊ฐ™๋‹ค.!
  • ๋”•์…”๋„ˆ๋ฆฌ์™€ ๊ณ ์ฐจํ•จ์ˆ˜๋ฅผ ์ž˜ ์‚ฌ์šฉํ•˜๋‹ˆ ์ง๊ด€์ ์ด๊ณ  ํšจ์œจ์ ์ธ ์ฝ”๋“œ๊ฐ€ ๋œ๋‹ค,,!

 



๋ฌธ์ œ

https://programmers.co.kr/learn/courses/30/lessons/42840

 

๋Œ“๊ธ€