๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
3๏ธโƒฃ Swift/Problem Solving

[Swift Algorithm] ์˜คํฐ์ˆ˜ BOJ #17298

by seolhee2750 2021. 8. 8.
๋ฌธ์ œ ์„ค๋ช…

ํฌ๊ธฐ๊ฐ€ N์ธ ์ˆ˜์—ด A = A1, A2, ..., AN์ด ์žˆ๋‹ค. ์ˆ˜์—ด์˜ ๊ฐ ์›์†Œ Ai์— ๋Œ€ํ•ด์„œ ์˜คํฐ์ˆ˜ NGE(i)๋ฅผ ๊ตฌํ•˜๋ ค๊ณ  ํ•œ๋‹ค. Ai์˜ ์˜คํฐ์ˆ˜๋Š” ์˜ค๋ฅธ์ชฝ์— ์žˆ์œผ๋ฉด์„œ Ai๋ณด๋‹ค ํฐ ์ˆ˜ ์ค‘์—์„œ ๊ฐ€์žฅ ์™ผ์ชฝ์— ์žˆ๋Š” ์ˆ˜๋ฅผ ์˜๋ฏธํ•œ๋‹ค. ๊ทธ๋Ÿฌํ•œ ์ˆ˜๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ์— ์˜คํฐ์ˆ˜๋Š” -1์ด๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, A = [3, 5, 2, 7]์ธ ๊ฒฝ์šฐ NGE(1) = 5, NGE(2) = 7, NGE(3) = 7, NGE(4) = -1์ด๋‹ค. A = [9, 5, 4, 8]์ธ ๊ฒฝ์šฐ์—๋Š” NGE(1) = -1, NGE(2) = 8, NGE(3) = 8, NGE(4) = -1์ด๋‹ค.

 

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ์ˆ˜์—ด A์˜ ํฌ๊ธฐ N (1 ≤ N ≤ 1,000,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„์— ์ˆ˜์—ด A์˜ ์›์†Œ A1, A2, ..., AN (1 ≤ Ai ≤ 1,000,000)์ด ์ฃผ์–ด์ง„๋‹ค.

 

์ถœ๋ ฅ

์ด N๊ฐœ์˜ ์ˆ˜ NGE(1), NGE(2), ..., NGE(N)์„ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„ํ•ด ์ถœ๋ ฅํ•œ๋‹ค.

 

์ž…์ถœ๋ ฅ ์˜ˆ์ œ

์ž…๋ ฅ1

4
3 5 2 7

์ถœ๋ ฅ1

5 7 7 -1

 

์ž…๋ ฅ2

4
9 5 4 8

์ถœ๋ ฅ2

-1 8 8 -1

 

๋‚ด ๋ฌธ์ œ ํ’€์ด
import Foundation

var count = Int(readLine()!)!
var nums = readLine()!.split(separator: " ").map({Int(String($0))!})
var stack = [[0, nums[0]]]
var answer = Array(repeating: "-1", count: count)

for i in 1..<nums.count {
    while !stack.isEmpty && nums[i] > stack.last![1] {
        answer[stack.removeLast()[0]] = "\(nums[i])"
    }
    stack.append([i, nums[i]])
}

print(answer.joined(separator: " "))
  • ์Šคํƒ, LIFO๋กœ ํ’€์ดํ–ˆ๋‹ค.
  • nums ๋ฐฐ์—ด์— ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„ํ•œ ์ž…๋ ฅ ์ˆซ์ž๋“ค์„ ๋„ฃ์–ด์ฃผ์—ˆ๊ณ ,
    ์ธ๋ฑ์Šค์™€ nums๊ฐ’์„ ์Œ์œผ๋กœ ๊ฐ–๋Š” stack ๋ฐฐ์—ด์„ ์ƒ์„ฑํ–ˆ๋‹ค.
  • ์˜คํฐ์ˆ˜๋ฅผ ์ฐพ์ง€ ๋ชปํ•œ ์ˆ˜๋Š” -1๋กœ ํ‘œ์‹œํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ฒ˜์Œ๋ถ€ํ„ฐ -1๋กœ ์ฑ„์šด ๋ฐฐ์—ด์„ ๋งŒ๋“ค์—ˆ๋‹ค.
  • for๋ฌธ ์‹œ์ž‘ ์ „, ๋ฏธ๋ฆฌ nums ์ฒซ ๋ฒˆ์งธ ์ˆ˜๋ฅผ stack์— ๋„ฃ์—ˆ๊ณ , for๋ฌธ์€ 1๋ถ€ํ„ฐ ์‹œ์ž‘ํ–ˆ๋‹ค.
  • ์Šคํƒ์ด ๋น„์–ด์žˆ์ง€ ์•Š์œผ๋ฉฐ, ์Šคํƒ์˜ ๋งˆ์ง€๋ง‰ ์š”์†Œ๊ฐ€ ํ˜„์žฌ ์ž…๋ ฅ๋œ nums ๊ฐ’๋ณด๋‹ค ์ž‘์„ ๋•Œ ๋ฐ˜๋ณต์„ ์‹œํ–‰ํ–ˆ๋‹ค.
  • stack์˜ ๋งˆ์ง€๋ง‰ ์š”์†Œ๋ฅผ ์‚ญ์ œํ•จ๊ณผ ๋™์‹œ์— ์‚ญ์ œ๋œ ์ˆ˜์˜ ์ธ๋ฑ์Šค๋ฅผ answer๋ฐฐ์—ด์˜ ์œ„์น˜๋กœ ์‚ฌ์šฉํ–ˆ๋‹ค.

์•„๋ž˜ ์ž์„ธํ•œ ํ’€์ด ๊ณผ์ •์„ ์ฒจ๋ถ€ํ•œ๋‹ค. 

 

โž•

๊ทธ๋ฆฌ๊ณ  ๋‚ด๊ฐ€ ์ด ๋ฌธ์ œ๋ฅผ ๋นจ๋ฆฌ๋Š” ํ’€์—ˆ๋Š”๋ฐ,, ์ž๊พธ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚˜์„œ ์• ๋จน์—ˆ๋‹ค.

๊ทผ๋ฐ ๊ทธ ์ด์œ ๊ฐ€ ๋‹จ์ง€ spilt ๋Œ€์‹  components๋ฅผ ์จ์„œ,, ๊ทธ๋Ÿฐ๊ฑฐ์˜€๋‹ค.

split์ด components๋ณด๋‹ค ์‹œ๊ฐ„์„ ๋œ์žก์•„๋จน๊ณ , ์„ฑ๋Šฅ์ด ์ข‹๋‹ค๊ณ  ํ•œ๋‹ค. 

์ง€๊ธˆ๊นŒ์ง€๋Š” ์ด๊ฑธ ๋ชฐ๋ž๊ณ , components๊ฐ€ [String]์œผ๋กœ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋Š”๊ฒŒ

์‚ฌ์šฉํ•˜๊ธฐ ํŽธํ•ด์„œ ๋งจ๋‚  components๋กœ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ, ์•ž์œผ๋กœ๋Š” ์›ฌ๋งŒํ•˜๋ฉด split์œผ๋กœ ์‚ฌ์šฉํ•ด์•ผ๊ฒ ๋‹ค.

(split๊ณผ components ๋น„๊ต! ์š”๊ธฐ๐Ÿ‘‡)

2021.08.08 - [๐Ÿ“ Swift Note/๐Ÿ“‹ ๊ฐœ๋… ์ •๋ฆฌ] - [Swift] split๊ณผ components์˜ ์ฐจ์ด์ 

 

[Swift] split๊ณผ components์˜ ์ฐจ์ด์ 

split์ด components๋ณด๋‹ค ์„ฑ๋Šฅ์ด ์ข‹๋‹ค๋Š” ์‚ฌ์‹ค์„ ๊นจ๋‹ซ๊ณ ,, ๋‘˜์˜ ์ฐจ์ด์ ์„ ๊ณต๋ถ€ํ•ด๋ณด๋ คํ•œ๋‹ค. ๐Ÿถ ๐Ÿ“Ž components ๋จผ์ € components์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์•Œ์•„๋ณด์ž. ๊ณต์‹ ๊ฐœ๋ฐœ๋ฌธ์„œ์—์„œ components์˜ ์„ค๋ช…์„ ๊ฐ€์ ธ์™”๋‹ค. โœ”๏ธ

seolhee2750.tistory.com

 

๐Ÿ’ก ํ”ผ๋“œ๋ฐฑ
  • ์•„์ง๋„ swift ์–ธ์–ด์— ๋Œ€ํ•œ ์ดํ•ด๋„๊ฐ€ ๋ถ€์กฑํ•œ๊ฐ€๋ณด๋‹น ใ…œ ์ฑ…์ข€ ๋” ์ฝ์ž!~

 


 

๋ฌธ์ œ

https://www.acmicpc.net/problem/17298

๋Œ“๊ธ€