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

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์†Œ์ˆ˜์ฐพ๊ธฐ Level 2 Swift

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

๋ฌธ์ œ ์„ค๋ช…

ํ•œ์ž๋ฆฌ ์ˆซ์ž๊ฐ€ ์ ํžŒ ์ข…์ด ์กฐ๊ฐ์ด ํฉ์–ด์ ธ์žˆ์Šต๋‹ˆ๋‹ค. ํฉ์–ด์ง„ ์ข…์ด ์กฐ๊ฐ์„ ๋ถ™์—ฌ ์†Œ์ˆ˜๋ฅผ ๋ช‡ ๊ฐœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š”์ง€ ์•Œ์•„๋‚ด๋ ค ํ•ฉ๋‹ˆ๋‹ค.

๊ฐ ์ข…์ด ์กฐ๊ฐ์— ์ ํžŒ ์ˆซ์ž๊ฐ€ ์ ํžŒ ๋ฌธ์ž์—ด numbers๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ข…์ด ์กฐ๊ฐ์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ์†Œ์ˆ˜๊ฐ€ ๋ช‡ ๊ฐœ์ธ์ง€ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ

  • numbers๋Š” ๊ธธ์ด 1 ์ด์ƒ 7 ์ดํ•˜์ธ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.
  • numbers๋Š” 0~9๊นŒ์ง€ ์ˆซ์ž๋งŒ์œผ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
  • 013์€ 0, 1, 3 ์ˆซ์ž๊ฐ€ ์ ํžŒ ์ข…์ด ์กฐ๊ฐ์ด ํฉ์–ด์ ธ์žˆ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

numbers return
17 3
011 2

ํ’€์ด: ์šฐ์„  numbers๊ฐ€ ์ฃผ์–ด์กŒ์„ ๊ฒฝ์šฐ ๋ชจ๋“  ์กฐํ•ฉ์„ ์ฐพ๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ด๋Ÿฌํ•œ ๊ณผ์ •์ด ํ•„์š”ํ•˜๋‹ค.

ex) numbers๊ฐ€ 123456์ผ๊ฒฝ์šฐ

์ฐจ๋ก€๋กœ ๋งจ ์•ž์ž๋ฆฌ์— ์ˆซ์ž๋“ค์„ ๋Œ€์ž…ํ•˜๋ฉด์„œ n-1์˜ ์ž๋ฆฌ ์ˆซ์ž๋“ค์„ ๋’ค์— ๋ฐฐ์น˜ํ•œ๋‹ค.

- 1 2 3 4 5 6

- 11 12 13 14 15 16 , 21 22 23 24 25 26 .... 66

- 111 112 113 114 116 121 122 123 124 125 126 .... 666

์ด๋ ‡๊ฒŒ ๋ชจ๋“  ์ˆซ์ž๋“ค์„ ๋‚˜์—ดํ•˜๋ฉด ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธฐ๋Š”๋ฐ

11, 111 222 666๊ณผ ๊ฐ™์ด ์ˆซ์ž๋Š” ํ•˜๋‚˜๋งŒ ์žˆ๋Š”๋ฐ ์กฐํ•ฉ์ƒ์—” ์—ฌ๋Ÿฌ ์ˆซ์ž๊ฐ€ ์žˆ๋Š” ๊ฒƒ์ด๋‹ค.

์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ๋งจ ์ฒ˜์Œ numbers์— ๊ฐ ์ˆซ์ž๋“ค์ด ๋ช‡๊ฐœ ์žˆ๋Š”์ง€(์ค‘๋ณต์ด ์žˆ๋Š”์ง€)๋ฅผ ์ฐพ๋Š”๋‹ค.

๊ทธ๋ฆฌ๊ณค ์กฐํ•ฉ ์ƒ์˜ ์ˆซ์ž๊ฐ€ ๊ทธ ์ˆซ์ž๋ณด๋‹ค ๋งŽ์€ ๊ฒƒ์€ ์ถ”๊ฐ€ํ•ด์ฃผ์ง€ ์•Š๊ณ  ์ ๊ฑฐ๋‚˜ ๊ฐ™์€ ๊ฒƒ๋“ค์„ ์ถ”๊ฐ€ํ•ด์ค๋‹ˆ๋‹ค.

์ค‘๋ณต์„ ์—†์• ๊ธฐ ์œ„ํ•ด Set๋ฅผ ์‚ฌ์šฉํ•ด์ค€ ๋’ค ๊ทธ๋ ‡๊ฒŒ ๊ฑธ๋Ÿฌ์ ธ ๋‚˜์˜จ ์ˆซ์ž๋“ค์„ ์†Œ์ˆ˜๊ฐ€ ๋งž๋Š”์ง€ ํ™•์ธํ•ด์ค๋‹ˆ๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import Foundation
 
func solution(_ numbers:String-> Int {
    var answer = 0
    let map = numbers.compactMap{String($0)}
    var overlap2 = [String:Int]()
    var overlap1 = [String]()
    var set:Set<Int> = []
    
    //์ˆซ์ž ์ค‘ ์ค‘๋ณต์ด ์žˆ๋Š”์ง€ ์ฐพ๊ธฐ
    for i in map {
        set.insert(Int(i)!)
        if overlap1.contains(i){
            overlap2[i]! += 1
            continue
        }
        overlap2[i] = 1
        overlap1.append(i)
    }
    
    //์ˆซ์ž ๋ชจ๋“  ์กฐํ•ฉ
    var array = Array(repeating: [String](), count: numbers.count)
    array[0= map
    if numbers.count > 1 {
        for i in 1...numbers.count-1{
            for j in 0..<array[i-1].count{
                for k in 0..<numbers.count{
                    let a = "\(map[k])\(array[i-1][j])"
                    if a.map({String($0)}).filter({$0 == map[k]}).count <= overlap2[map[k]]!{
                        array[i].append(a)
                        set.insert(Int(a)!)
                    }
                }
            }
        }
    }
    
    //์†Œ์ˆ˜ ์ฐพ๊ธฐ
    for n in set{
        var count = 0
        if n == 1 || n == 0{
            continue
        }
        for i in 2...n{
        if n%i == 0{
            count += 1
            if count > 1{
                break
            }
            }
        }
        if count == 1 {
            answer += 1
        }
    }
    return answer
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€