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

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

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

๋ฌธ์ œ ์„ค๋ช…

์ˆ˜ํ‰ ์ง์„ ์— ํƒ‘ N๋Œ€๋ฅผ ์„ธ์› ์Šต๋‹ˆ๋‹ค. ๋ชจ๋“  ํƒ‘์˜ ๊ผญ๋Œ€๊ธฐ์—๋Š” ์‹ ํ˜ธ๋ฅผ ์†ก/์ˆ˜์‹ ํ•˜๋Š” ์žฅ์น˜๋ฅผ ์„ค์น˜ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋ฐœ์‚ฌํ•œ ์‹ ํ˜ธ๋Š” ์‹ ํ˜ธ๋ฅผ ๋ณด๋‚ธ ํƒ‘๋ณด๋‹ค ๋†’์€ ํƒ‘์—์„œ๋งŒ ์ˆ˜์‹ ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ, ํ•œ ๋ฒˆ ์ˆ˜์‹ ๋œ ์‹ ํ˜ธ๋Š” ๋‹ค๋ฅธ ํƒ‘์œผ๋กœ ์†ก์‹ ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด ๋†’์ด๊ฐ€ 6, 9, 5, 7, 4์ธ ๋‹ค์„ฏ ํƒ‘์ด ์™ผ์ชฝ์œผ๋กœ ๋™์‹œ์— ๋ ˆ์ด์ € ์‹ ํ˜ธ๋ฅผ ๋ฐœ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด, ํƒ‘์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‹ ํ˜ธ๋ฅผ ์ฃผ๊ณ ๋ฐ›์Šต๋‹ˆ๋‹ค. ๋†’์ด๊ฐ€ 4์ธ ๋‹ค์„ฏ ๋ฒˆ์งธ ํƒ‘์—์„œ ๋ฐœ์‚ฌํ•œ ์‹ ํ˜ธ๋Š” ๋†’์ด๊ฐ€ 7์ธ ๋„ค ๋ฒˆ์งธ ํƒ‘์ด ์ˆ˜์‹ ํ•˜๊ณ , ๋†’์ด๊ฐ€ 7์ธ ๋„ค ๋ฒˆ์งธ ํƒ‘์˜ ์‹ ํ˜ธ๋Š” ๋†’์ด๊ฐ€ 9์ธ ๋‘ ๋ฒˆ์งธ ํƒ‘์ด, ๋†’์ด๊ฐ€ 5์ธ ์„ธ ๋ฒˆ์งธ ํƒ‘์˜ ์‹ ํ˜ธ๋„ ๋†’์ด๊ฐ€ 9์ธ ๋‘ ๋ฒˆ์งธ ํƒ‘์ด ์ˆ˜์‹ ํ•ฉ๋‹ˆ๋‹ค. ๋†’์ด๊ฐ€ 9์ธ ๋‘ ๋ฒˆ์งธ ํƒ‘๊ณผ ๋†’์ด๊ฐ€ 6์ธ ์ฒซ ๋ฒˆ์งธ ํƒ‘์ด ๋ณด๋‚ธ ๋ ˆ์ด์ € ์‹ ํ˜ธ๋Š” ์–ด๋–ค ํƒ‘์—์„œ๋„ ์ˆ˜์‹ ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

์†ก์‹  ํƒ‘(๋†’์ด)์ˆ˜์‹  ํƒ‘(๋†’์ด)

5(4) 4(7)
4(7) 2(9)
3(5) 2(9)
2(9) -
1(6) -

๋งจ ์™ผ์ชฝ๋ถ€ํ„ฐ ์ˆœ์„œ๋Œ€๋กœ ํƒ‘์˜ ๋†’์ด๋ฅผ ๋‹ด์€ ๋ฐฐ์—ด heights๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ ๊ฐ ํƒ‘์ด ์œ ์‹ ํ˜ธ๋ฅผ ์–ด๋Š ํƒ‘์—์„œ ๋ฐ›์•˜๋Š”์ง€ ๊ธฐ๋กํ•œ ๋ฐฐ์—ด์„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ ์‚ฌํ•ญ

  • heights๋Š” ๊ธธ์ด 2 ์ด์ƒ 100 ์ดํ•˜์ธ ์ •์ˆ˜ ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค.
  • ๋ชจ๋“  ํƒ‘์˜ ๋†’์ด๋Š” 1 ์ด์ƒ 100 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • ์‹ ํ˜ธ๋ฅผ ์ˆ˜์‹ ํ•˜๋Š” ํƒ‘์ด ์—†์œผ๋ฉด 0์œผ๋กœ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

 

heights return
[3,9,9,3,5,7,2] [0,0,0,3,3,3,6]
[1,5,3,6,7,6,5] [0,0,2,0,0,5,6]
[6,9,5,7,4] [0,0,2,2,4]

ํ’€์ด:height์˜ ์˜ค๋ฅธ์ชฝ๋ถ€ํ„ฐ ์™ผ์ชฝ์œผ๋กœ ์ฐจ๋ก€๋Œ€๋กœ ๊ฐ€์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— index๋ฅผ ์—ญ์ˆœ์œผ๋กœ ๋Œ๋ ค์•ผํ–ˆ๋‹ค. 

for in stirde๋ฌธ์œผ๋กœ heights์˜ ๊ธธ์ด์˜ -1๋ถ€ํ„ฐ -1๊นŒ์ง€ ๋Œ๋ ธ๋Š”๋ฐ 0์ด ๋“ค์–ด๊ฐ€์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— -1๊นŒ์ง€ ๋Œ๋ ธ๋‹ค.

๊ทธ๋ฆฌ๊ณ  for๋ฌธ์„ ํ•œ ๊ฐœ ๋”์จ์„œ j๋ฅผ i๋ถ€ํ„ฐ -1๊นŒ์ง€ ๋Œ๋ฆฌ๋Š”๋ฐ heights[j]๊ฐ€ heights[i]๋ณด๋‹ค ํฌ๋‹ค๋ฉด ๋ฐ”๋กœ answer๋ฐฐ์—ด์— ์ถ”๊ฐ€ํ•˜๊ณ 

๋ฐ”๊นฅ ํฌ๋ฌธ์„ ๋ฐ”๋กœ ๋‹ค์Œ๊ฑฐ๋ฅผ ํ•˜๋„๋ก continue outer๋ฅผ ์จ์ฃผ์—ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๋งˆ์ง€๋ง‰์œผ๋กœ ๋ฐฐ์—ด์ด ์ฐจ๋ก€๋Œ€๋กœ ์ถ”๊ฐ€๋˜๋ฏ€๋กœ answer๋ฅผ ์—ญ์ˆœ์œผ๋กœ ๋‚˜ํƒ€๋‚ด๊ฒŒ reversed๋ฅผ ์จ์ฃผ์—ˆ๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import Foundation
 
func solution(_ heights:[Int]) -> [Int] {
    var answer = [Int]()
    outer:for i in stride(from: heights.count-1, to: -1, by: -1){
        for j in stride(from: i, to: -1, by: -1){
            if heights[i] < heights[j]{
                answer.append(j+1)
                continue outer
            }
        }
            answer.append(0)
    }
    return answer.reversed()
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

๋‹ค๋ฅธ์‚ฌ๋žŒ ํ’€์ด ์ค‘ ๊ฐ€์žฅ ์ข‹๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋Š” ๊ฒƒ

for๋ฌธ์„ ์—ญ์ˆœ์œผ๋กœ ๋Œ๋ฆฌ๋ ค๋ฉด reversed๋ฅผ ์ €๋ ‡๊ฒŒ ์“ฐ๋ฉด ๋จ.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import Foundation
 
func solution(_ heights:[Int]) -> [Int] {
    var answer = [Int](repeating: 0, count: heights.count)
    var size = heights.count
 
    for i in (1..<size).reversed() {
        for j in (0..<i).reversed() {
            if heights[i] < heights[j] {
                answer[i] = j + 1
                break
            }
        }
    }
    return answer
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€