๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“– Problem Solution/Programmers

Swift ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ˆ˜ํฌ์ž

by Fomagran ๐Ÿ’ป 2020. 2. 6.
728x90
๋ฐ˜์‘ํ˜•

์ˆ˜ํฌ์ž๋Š” ์ˆ˜ํ•™์„ ํฌ๊ธฐํ•œ ์‚ฌ๋žŒ์˜ ์ค€๋ง์ž…๋‹ˆ๋‹ค. ์ˆ˜ํฌ์ž ์‚ผ์ธ๋ฐฉ์€ ๋ชจ์˜๊ณ ์‚ฌ์— ์ˆ˜ํ•™ ๋ฌธ์ œ๋ฅผ ์ „๋ถ€ ์ฐ์œผ๋ ค ํ•ฉ๋‹ˆ๋‹ค. ์ˆ˜ํฌ์ž๋Š” 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ํ•˜๋Š” ๊ฐ’์„ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•ด์ฃผ์„ธ์š”.

์ž…์ถœ๋ ฅ ์˜ˆ

์ž…๋ ฅ ์ถœ๋ ฅ
[1,2,3,4,5] [1]
[1,3,2,4,2] [1,2,3]

ํ•ต์‹ฌ:

1.1,2,3์˜ ๊ฐ ํŒจํ„ด์„ ์•Œ์•„๋‚ธ๋‹ค.

1 -> 5๋กœ ๋‚˜๋ˆด์„ ๋•Œ ๋‚˜๋จธ์ง€ 5์ผ ๊ฒฝ์šฐ์—” 5

2 ->ํ™€์ˆ˜์ธ ๊ฒฝ์šฐ์—” 2 ์ง์ˆ˜์ผ๊ฒฝ์šฐ์—” 8๋กœ ๋‚˜๋ˆด์„ ๋•Œ ๋‚˜๋จธ์ง€๊ฐ€ ๊ฐ๊ฐ 2,4,6,8

3 -> 5๋กœ ๋‚˜๋ˆด์„ ๋•Œ ๋‚˜๋จธ์ง€๊ฐ€ 1,2,3,4 5์ผ ๊ฒฝ์šฐ์—” 0

2.ํŒจํ„ด์„ ์ด์šฉํ•˜์—ฌ answers์–ด๋ ˆ์ด ๊ฐ’์— ๋งž๊ฒŒ ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๊ณ  count๋ณ€์ˆ˜๋ฅผ ์ƒ์„ฑํ•ด answers์™€ ๊ฐ ๋ฐฐ์—ด์„ ๋น„๊ตํ•ด์ค€๋‹ค. ๋งŒ์•ฝ ๊ฐ’์ด ๊ฐ™์„ ๋• count ์ˆซ์ž๋ฅผ +1ํ•ด์ค€๋‹ค.

3.๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์•Œ์•„๋‚ด ๋น„๊ตํ•œ๋‹ค. -> ์ด 10๊ฐ€์ง€

a,b,c ๋ชจ๋‘ ๊ฐ™์„ ๋•Œ 1

a,b,c ์ค‘ ๊ฐ€์žฅ ํฐ ์ˆ˜๊ฐ€ ์žˆ์„ ๋•Œ 3

3๊ฐœ ์ค‘ ๋‘๊ฐœ๊ฐ€ ๊ฐ™๊ณ  ๊ทธ ๋‘์ˆ˜๊ฐ€ ๋” ํด๋•Œ 3

3๊ฐœ ์ค‘ ๋‘๊ฐœ๊ฐ€ ๊ฐ™๊ณ  ๊ทธ ๋‘์ˆ˜๊ฐ€ ๋” ์ž‘์„ ๋•Œ 3

func solution(_ answers:[Int]) -> [Int] {
    var a = [Int]()
    var b = [Int]()
    var c = [Int]()
    var acount = 0
    var bcount = 0
    var ccount = 0
    for number in 1...answers.count{
        if number%5 != 0{
            a.append(number%5)
        }else{
            a.append(5)
        }
        if number%2 != 0 {
            b.append(2)
        }else if number%8 == 2{
            b.append(1)
        }else if number%8 == 4{
            b.append(3)
        }else if number%8 == 6{
            b.append(4)
        }else if number%8 == 0{
            b.append(5)
        }
        if number%5 == 1{
            c.append(3)
            c.append(3)
        }else if number%5 == 2 {
            c.append(1)
            c.append(1)
        }else if number%5 == 3 {
            c.append(2)
            c.append(2)
        }else if number%5 == 4 {
            c.append(4)
            c.append(4)
        }else if number%5 == 0 {
            c.append(5)
            c.append(5)
        }
        if a[number-1] == answers[number-1] {
                   acount += 1
               }
               if b[number-1] == answers[number-1]{
                   bcount += 1
               }
               if c[number-1] == answers[number-1]{
                   ccount += 1
               }
    }
    if acount > bcount && acount > ccount{
        return [1]
    }else if bcount > acount && bcount > ccount{
        return [2]
    }else if ccount > acount && ccount > bcount{
        return [3]
    }else if acount == bcount && bcount > ccount{
        return [1,2]
    }else if acount == ccount && ccount > bcount{
        return [1,3]
    }else if ccount == bcount && bcount > acount{
        return [2,3]
    }else if acount == bcount && bcount < ccount{
        return [3]
    }else if acount == ccount && bcount > ccount{
        return [2]
    }else if bcount == ccount && acount > ccount{
        return [1]
    }else {
        return [1,2,3]
    }
    
}

๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด ์ค‘ ๊ฐ€์žฅ ๊ฐ„๊ฒฐํ•œ ๊ฒƒ

ํ•ต์‹ฌ :

1.a,b,c์— ํŒจํ„ด์„ ๋ฐ˜๋ณต๋˜๋Š” ๊ตฌ๊ฐ„๋Œ€๋กœ ๊ฐ ๊ฐ ์ €์žฅํ•ด๋†จ๋‹ค. ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ด์„œ 1,2,3์„ ๋ถ„๋ฅ˜

2.answers.enumerated()๋ผ๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ  i,v๋ฅผ ๊ฐ ๊ฐ ํ‚ค์™€ ๋ฐธ๋ฅ˜๋กœ ๊ฐ’์„ ์คŒ ex)answers[i] = v ์™€ ๊ฐ™์ด ๊ฐ’์ด ๋‚˜์˜ค๊ฒŒ ํ•จ.

3.answer = (a:[1,2,3,4,5]) ์ด๋Ÿฐ ์†Œ๊ด„ํ˜ธ ์•ˆ์— ๋”•์…”๋„ˆ๋ฆฌ ํ˜•ํƒœ๋กœ ๋„ฃ์–ด์ฃผ๋ฉด answer.key๊ฐ’์„ ๋„ฃ์–ด์ฃผ๋ฉด ๋ฐฐ์—ด์„ ์•Œ ์ˆ˜ ์žˆ์Œ

4.a 5๊ฐœ b๋Š” 8๊ฐœ c๋Š” 10๊ฐœ๊ฐ€ ๊ฐ ๊ฐ ๋ฐ˜๋ณต์ด๋ฏ€๋กœ index๋ฅผ %5 %8 %10ํ•ด์ฃผ๋ฉด ๊ฐ ๋ฐฐ์—ด์˜ ๊ฐ™์€ ๊ฐ’์ด ๋‚˜์˜จ๋‹ค. ์—ฌ๊ธฐ์„œ answers์˜ ๋ฐธ๋ฅ˜๊ฐ’๊ณผ ๊ฐ™๋‹ค๋ฉด point์˜ ๋ฐธ๋ฅ˜๊ฐ’์„ 1์”ฉ ๋”ํ•ด์ค˜๋ผ

5.sorted{ $0.key < $1.key }๋ฅผ ์ด์šฉํ•ด point์˜ ํ‚ค(1,2,3) ์ค‘ ์ˆซ์ž๊ฐ€ ์ ์€ ์ˆœ์œผ๋กœ ์†ŒํŒ…ํ•˜๊ณ  , filter{ $0.value == point.values.max()}๋ฅผ ์ด์šฉํ•ด point์•ˆ์˜ value๊ฐ€ ์ตœ๋Œ€๊ฐ’์ธ ๊ฑธ ํ•„ํ„ฐ,๊ทธ๋ฆฌ๊ณ  map{$0.key} sorting๋œ ์ˆœ์œผ๋กœ filter์— ํ†ต๊ณผ๋œ point์˜ key๋“ค์„ ๋ฐฐ์—ด๋กœ ๋‹ด์•„๋ผ

 

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 }
}
728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€