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

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ํ”„๋ฆฐํ„ฐ Swift

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

๋ฌธ์ œ ์„ค๋ช…

์ผ๋ฐ˜์ ์ธ ํ”„๋ฆฐํ„ฐ๋Š” ์ธ์‡„ ์š”์ฒญ์ด ๋“ค์–ด์˜จ ์ˆœ์„œ๋Œ€๋กœ ์ธ์‡„ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์ค‘์š”ํ•œ ๋ฌธ์„œ๊ฐ€ ๋‚˜์ค‘์— ์ธ์‡„๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๋ฌธ์ œ๋ฅผ ๋ณด์™„ํ•˜๊ธฐ ์œ„ํ•ด ์ค‘์š”๋„๊ฐ€ ๋†’์€ ๋ฌธ์„œ๋ฅผ ๋จผ์ € ์ธ์‡„ํ•˜๋Š” ํ”„๋ฆฐํ„ฐ๋ฅผ ๊ฐœ๋ฐœํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด ์ƒˆ๋กญ๊ฒŒ ๊ฐœ๋ฐœํ•œ ํ”„๋ฆฐํ„ฐ๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์ธ์‡„ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

1. ์ธ์‡„ ๋Œ€๊ธฐ๋ชฉ๋ก์˜ ๊ฐ€์žฅ ์•ž์— ์žˆ๋Š” ๋ฌธ์„œ(J)๋ฅผ ๋Œ€๊ธฐ๋ชฉ๋ก์—์„œ ๊บผ๋ƒ…๋‹ˆ๋‹ค. 2. ๋‚˜๋จธ์ง€ ์ธ์‡„ ๋Œ€๊ธฐ๋ชฉ๋ก์—์„œ J๋ณด๋‹ค ์ค‘์š”๋„๊ฐ€ ๋†’์€ ๋ฌธ์„œ๊ฐ€ ํ•œ ๊ฐœ๋ผ๋„ ์กด์žฌํ•˜๋ฉด J๋ฅผ ๋Œ€๊ธฐ๋ชฉ๋ก์˜ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰์— ๋„ฃ์Šต๋‹ˆ๋‹ค. 3. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด J๋ฅผ ์ธ์‡„ํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, 4๊ฐœ์˜ ๋ฌธ์„œ(A, B, C, D)๊ฐ€ ์ˆœ์„œ๋Œ€๋กœ ์ธ์‡„ ๋Œ€๊ธฐ๋ชฉ๋ก์— ์žˆ๊ณ  ์ค‘์š”๋„๊ฐ€ 2 1 3 2 ๋ผ๋ฉด C D A B ์ˆœ์œผ๋กœ ์ธ์‡„ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

๋‚ด๊ฐ€ ์ธ์‡„๋ฅผ ์š”์ฒญํ•œ ๋ฌธ์„œ๊ฐ€ ๋ช‡ ๋ฒˆ์งธ๋กœ ์ธ์‡„๋˜๋Š”์ง€ ์•Œ๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ์˜ˆ์—์„œ C๋Š” 1๋ฒˆ์งธ๋กœ, A๋Š” 3๋ฒˆ์งธ๋กœ ์ธ์‡„๋ฉ๋‹ˆ๋‹ค.

ํ˜„์žฌ ๋Œ€๊ธฐ๋ชฉ๋ก์— ์žˆ๋Š” ๋ฌธ์„œ์˜ ์ค‘์š”๋„๊ฐ€ ์ˆœ์„œ๋Œ€๋กœ ๋‹ด๊ธด ๋ฐฐ์—ด priorities์™€ ๋‚ด๊ฐ€ ์ธ์‡„๋ฅผ ์š”์ฒญํ•œ ๋ฌธ์„œ๊ฐ€ ํ˜„์žฌ ๋Œ€๊ธฐ๋ชฉ๋ก์˜ ์–ด๋–ค ์œ„์น˜์— ์žˆ๋Š”์ง€๋ฅผ ์•Œ๋ ค์ฃผ๋Š” location์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ๋‚ด๊ฐ€ ์ธ์‡„๋ฅผ ์š”์ฒญํ•œ ๋ฌธ์„œ๊ฐ€ ๋ช‡ ๋ฒˆ์งธ๋กœ ์ธ์‡„๋˜๋Š”์ง€ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ

  • ํ˜„์žฌ ๋Œ€๊ธฐ๋ชฉ๋ก์—๋Š” 1๊ฐœ ์ด์ƒ 100๊ฐœ ์ดํ•˜์˜ ๋ฌธ์„œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์ธ์‡„ ์ž‘์—…์˜ ์ค‘์š”๋„๋Š” 1~9๋กœ ํ‘œํ˜„ํ•˜๋ฉฐ ์ˆซ์ž๊ฐ€ ํด์ˆ˜๋ก ์ค‘์š”ํ•˜๋‹ค๋Š” ๋œป์ž…๋‹ˆ๋‹ค.
  • location์€ 0 ์ด์ƒ (ํ˜„์žฌ ๋Œ€๊ธฐ๋ชฉ๋ก์— ์žˆ๋Š” ์ž‘์—… ์ˆ˜ - 1) ์ดํ•˜์˜ ๊ฐ’์„ ๊ฐ€์ง€๋ฉฐ ๋Œ€๊ธฐ๋ชฉ๋ก์˜ ๊ฐ€์žฅ ์•ž์— ์žˆ์œผ๋ฉด 0, ๋‘ ๋ฒˆ์งธ์— ์žˆ์œผ๋ฉด 1๋กœ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

prioritie location return
[2, 1, 3, 2] 2 1
[1, 1, 9, 1, 1, 1] 0 5

ํ’€์ด:์šฐ์„  answervalue์™€ answerkey๋ฅผ ๋งŒ๋“ค์–ด์„œ priorities์—์„œ location์— ํ•ด๋‹น๋˜๋Š” ๊ฐ’๊ณผ ๋ช‡ ๋ฒˆ์งธ์ธ์ง€ ์•Œ์•„๋‚ธ๋‹ค.

๊ทธ๋ฆฌ๊ณ  sort๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค๊ณ  priorites๋ฅผ sortํ•ด์„œ ์—ญ์ˆœ์œผ๋กœ ์ •๋ ฌ์„ ํ•ด์ค€๋‹ค.

๊ทธ๋ฆฌ๊ณ  while๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ ค 

๋งŒ์•ฝ answerkey๊ฐ€ 0์ด ๋˜์—ˆ๋‹ค๋ฉด ๋งจ ๋’ค๋กœ ๋ณด๋‚ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— answervalue๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ๊ณ  0๋ฒˆ์งธ๋ฅผ ์‚ญ์ œ ํ•ด์ค€ ๋’ค answerkey์— priorities์˜ ๋งจ ๋’ท๊ฐ’์„ ๋”ํ•ด์ค€๋‹ค. ๊ทธ๋ฆฌ๊ณ  continue๋ฅผ ์ด์šฉํ•ด ์•„๋ž˜ ์กฐ๊ฑด์„ ์•ˆํ•˜๊ณ  ๋‹ค์Œ์œผ๋กœ ๋„˜์–ด๊ฐ€๊ฒŒ ํ•œ๋‹ค.

๋จ„์•ฝ sort[0] ์ฆ‰, ๊ฐ€์žฅ ํฐ ๊ฐ’์ด priorities์˜ ๊ฐ€์žฅ ์ฒซ ๋ฒˆ์งธ ๊ฐ’์ด๋ผ๋ฉด ์‚ญ์ œํ•˜๊ณ  count๋ฅผ 1์„ ๋”ํ•ด์ค€๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๋ฐฐ์—ด์—์„œ ํ•œ ๊ฐœ๊ฐ€ ์‚ฌ๋ผ์กŒ์œผ๋ฏ€๋กœ answerkey๋Š” ๋‹น์—ฐํžˆ -1๋งŒํผ ์•ž์œผ๋กœ ์˜ค๊ฒŒ ๋œ๋‹ค.

๊ฐ€์žฅ ํฐ ๊ฐ’์ด ์•„๋‹ˆ๋ผ๋ฉด ๊ทธ ๊ฐ’์€ ๋งจ ๋’ค๋กœ ์ด๋™ํ•˜๋ฏ€๋กœ answerkey๋Š” -1๋งŒํผ ์˜ค๊ฒŒ ํ•œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๋งŒ์•ฝ sort์—์„œ ๊ฐ€์žฅ ํฐ ๊ฐ’์ด answervalue๋ผ๋ฉด ๋ฐ˜๋ณต๋ฌธ์„ ๋‚˜์™€์„œ

answervalue์™€ ๊ฐ™์€ ๊ฐ’์ด ์žˆ๋‹ค๋ฉด count๋ฅผ 1์”ฉ ๋”ํ•ด์ฃผ๊ณ  answervalue์™€ answerkey๊ฐ€ ๋ชจ๋‘ ๊ฐ™์€ ๊ฐ’์ด ๋ฐ”๋กœ ์ฒ˜์Œ์— ์›ํ–ˆ๋˜ ๊ฐ’์ด๋ฏ€๋กœ ๊ทธ ๊ฐ’์ด ๋‚˜์˜ค๋ฉด count์— ๋งˆ์ง€๋ง‰์œผ๋กœ 1์„ ๋”ํ•ด์ฃผ๊ณ  count๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด ๋ช‡ ๋ฒˆ์งธ์ธ์ง€ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.

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
import Foundation
 
func solution(_ priorities:[Int], _ location:Int) -> Int {
    let answervalue = priorities[location]
    var answerkey = location
    var priorities1 = priorities
    var sort = priorities.sorted(by:>)
    var count = 0
    while true {
        print(sort[0],answervalue)
        if sort[0== answervalue {
                    break
                              }
        if answerkey == 0 {
                           priorities1.append(answervalue)
                           priorities1.remove(at: 0)
                           answerkey += priorities1.count - 1
            continue
                                  }
        if sort[0== priorities1[0]{
               priorities1.remove(at: 0)
               count += 1
            answerkey -= 1
               sort.remove(at: 0)
           }else{
               priorities1.append(priorities1[0])
                priorities1.remove(at: 0)
               answerkey -= 1
           }
    }
    for i in 0...priorities1.count-1{
        if i == answerkey && priorities1[i] == answervalue{
            count += 1
            break
        }
        if priorities1[i] == answervalue{
            count += 1
        }
    }
    return count
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€