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

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์˜์–ด ๋๋ง์ž‡๊ธฐ Swift

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

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์˜์–ด ๋๋ง์ž‡๊ธฐ

3 [tank, kick, know, wheel, land, dream, mother, robot, tank] [3,3] 5 [hello, observe, effect, take, either, recognize, encourage, ensure, establish, hang, gather, refer, reference, estimate, executive] [0,0]

programmers.co.kr

Foma's ํ’€์ด

์ค‘๋ณต์„ ํ™•์ธํ•  ๋ณ€์ˆ˜ (for๋ฌธ์„ 1๋ถ€ํ„ฐ ๋Œ๋ฆด๊ฒƒ์ด๋ฏ€๋กœ 0๋ฒˆ์งธ ๋‹จ์–ด๋Š” ๋ฏธ๋ฆฌ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค.) -> var w = [words[0]]

 

๋ฒˆํ˜ธ๋ฅผ ์ €์žฅํ•  ๋ณ€์ˆ˜ -> var number = Int()

 

์ฐจ๋ก€๋ฅผ ์ €์žฅํ•  ๋ณ€์ˆ˜ ->  var time = Int()

 

for๋ฌธ์œผ๋กœ 1๋ถ€ํ„ฐ words์˜ ๊ฐฏ์ˆ˜๋งŒํผ ๋ฐ˜๋ณตํ•ด์ค๋‹ˆ๋‹ค. -> for i in 1..<words.count { ... }

 

์กฐ๊ฑด 1 ๋ฐ˜๋ณต๋œ ๋‹จ์–ด ๊ฒ€์‚ฌ

 

w์•ˆ์— ํ˜„์žฌ ๋‹จ์–ด(words[i])๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธ - > if w.contains(words[i]) { ... }

 

์žˆ๋‹ค๋ฉด number๋ฅผ ํ˜„์žฌ ๋‹จ์–ด์˜ ์ธ๋ฑ์Šค + 1 (0๋ฒˆ์งธ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋ฏ€๋กœ + 1์„ ํ•ด์ค๋‹ˆ๋‹ค.) ๋‚˜๋ˆ„๊ธฐ n์˜ ๋‚˜๋จธ์ง€๋กœ ์„ค์ •ํ•ด์ค๋‹ˆ๋‹ค. ->   number = (i+1)%n

 

(i+1)/n๊ฐ’์ด ๋”ฑ ๋–จ์–ด์ง€์ง€ ์•Š๋Š”๋‹ค๋ฉด ๋‹ค์Œ ์ฐจ๋ก€๊ฐ€ ์‹œ์ž‘๋œ๊ฒƒ์ด๋ฏ€๋กœ time์„ (i+1)/n + 1๋กœ ์•„๋‹ˆ๋ผ๋ฉด (i+1)/n์œผ๋กœ ์„ค์ •ํ•ด์ค๋‹ˆ๋‹ค. -> 

if Double((i+1)/n) < Double(i+1)/Double(n) {

                time = (i+1)/+ 1

            }else{

                time = (i+1)/n

            }

 

๊ทธ๋ฆฌ๊ณค for๋ฌธ์„ ์ง„ํ–‰ํ•  ํ•„์š”๊ฐ€ ์—†์œผ๋ฏ€๋กœ ๋ฉˆ์ถฐ์ค๋‹ˆ๋‹ค.  ->   break

 

์•„๋‹ˆ๋ผ๋ฉด w์— ๋‹จ์–ด๋ฅผ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค. - >  w.append(words[i])

 

์กฐ๊ฑด 2 ๊ทธ ์ „ ๋‹จ์–ด์˜ ๋งˆ์ง€๋ง‰ ๊ธ€์ž์™€ ํ˜„์žฌ ๋‹จ์–ด์˜ ์ฒซ๋ฒˆ์งธ ๋‹จ์–ด ๊ฒ€์‚ฌ

 

์ „ ๋‹จ์–ด์˜ ๋งˆ์ง€๋ง‰ ๊ธ€์ž์™€ ํ˜„์žฌ ๋‹จ์–ด์˜ ์ฒซ๋ฒˆ์งธ ๊ธ€์ž๊ฐ€ ๊ฐ™์€์ง€ ํ™•์ธ - > if words[i-1].last! != words[i].first!{...}

 

์กฐ๊ฑด 1๊ณผ ๋™์ผํ•˜๊ฒŒ ์ž‘์„ฑ ->  number = (i+1)%n

            if Double((i+1)/n) < Double(i+1)/Double(n) {

                time = (i+1)/+ 1

            }else{

                time = (i+1)/n

            }

            break

}

 

time์ด 0์ด๋ผ๋ฉด ํ‹€๋ฆฐ ๋‹จ์–ด๊ฐ€ ์—†๋‹ค๋Š” ๋œป(๋งŒ์•ฝ ์žˆ๋‹ค๋ฉด ๋ฌด์กฐ๊ฑด 1์ด์ƒ)์ด๋ฏ€๋กœ [0,0]์„ ๋ฐ˜ํ™˜ - > if time == 0 {

        return [0,0]

    }

 

number๊ฐ€ 0์ด๋ผ๋Š”๊ฑด (i+1)%n๊ฐ€ 0์ด๋ฏ€๋กœ n๋ฒˆ์งธ ์‚ฌ๋žŒ์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ n์„ 0์ด ์•„๋‹ˆ๋ผ๋ฉด ๋‚˜๋จธ์ง€๋ฅผ ๋ฐ˜ํ™˜ํ•ด์ค๋‹ˆ๋‹ค. ->

return number == 0 ? [n,time] : [number,time]

 

์ „์ฒด์ฝ”๋“œ

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
import Foundation
 
func solution(_ n:Int, _ words:[String]) -> [Int] {
    
    var w = [words[0]]
    var number = Int()
    var time = Int()
    
    for i in 1..<words.count {
        if w.contains(words[i]) {
            number = (i+1)%n
            if Double((i+1)/n) < Double(i+1)/Double(n) {
                time = (i+1)/+ 1
            }else{
                time = (i+1)/n
            }
            break
        }
        if words[i-1].last! != words[i].first!{
            number = (i+1)%n
            if Double((i+1)/n) < Double(i+1)/Double(n) {
                time = (i+1)/+ 1
            }else{
                time = (i+1)/n
            }
            break
        }
        w.append(words[i])
    }
    
    if time == 0 {
        return [0,0]
    }
    
    return number == 0 ? [n,time] : [number,time]
}

 

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€