๋ฐฑ์ค€ - ํ•œ์กฐ์„œ์—ด์ •๋ฆฌํ•˜๊ณ ์˜ดใ…‹ใ…‹(Swift)

2022. 11. 7. 14:22ใ†Algorithm

๋ฐฑ์ค€ - ํ•œ์กฐ์„œ์—ด์ •๋ฆฌํ•˜๊ณ ์˜ดใ…‹ใ…‹(Swift)

 

 

 ๋ฌธ์ œ ์„ค๋ช…

 

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

 

14659๋ฒˆ: ํ•œ์กฐ์„œ์—ด์ •๋ฆฌํ•˜๊ณ ์˜ดใ…‹ใ…‹

์ฒซ์งธ ์ค„์— ๋ด‰์šฐ๋ฆฌ์˜ ์ˆ˜ ๊ฒธ ํ™œ์žก์ด์˜ ์ˆ˜ N์ด ์ฃผ์–ด์ง„๋‹ค. (1 ≤ N ≤ 30,000) ๋‘˜์งธ ์ค„์— N๊ฐœ ๋ด‰์šฐ๋ฆฌ์˜ ๋†’์ด๊ฐ€ ์™ผ์ชฝ ๋ด‰์šฐ๋ฆฌ๋ถ€ํ„ฐ ์ˆœ์„œ๋Œ€๋กœ ์ฃผ์–ด์ง„๋‹ค. (1 ≤ ๋†’์ด ≤ 100,000) ๊ฐ๊ฐ ๋ด‰์šฐ๋ฆฌ์˜ ๋†’์ด๋Š” ์ค‘๋ณต ์—†์ด

www.acmicpc.net

 

 ๋‚˜์˜ ํ’€์ด

 

๋”ฑ O(N)๋งŒํผ๋งŒ ๋Œ๋ฉด ๋˜๋Š” ๊ฐ„๋‹จํ•œ ํ’€์ด๋‹ค.

 

import Foundation

let input = Int(readLine()!)!

let arr = readLine()!.components(separatedBy:" ").map{Int($0)!}

var maxCnt = 0
var cnt = 0
var cur = 0

for x in arr {
    if cur < x {
        cur = x
        cnt = 0
    } else {
        cnt += 1
    }
    maxCnt = max(maxCnt, cnt)
}

print(maxCnt)