๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Algorithm

[Programmers] 1์ฐจ ์ถ”์„ ํŠธ๋ž˜ํ”ฝ

๋ฌธ์ œ๋งํฌ  ๐Ÿ‘‰  https://programmers.co.kr/learn/courses/30/lessons/17676

 

๋ฌธ์ œ ์ดํ•ด 

์‘๋‹ต ์™„๋ฃŒ๋œ ์‹œ๊ฐ„ ์ •๋ณด์™€ ์ฒ˜๋ฆฌ ์‹œ๊ฐ„ ์ •๋ณด๊ฐ€ (๋‚ ์งœ ์ •๋ณด๋„ ์ฃผ์–ด์ง€์ง€๋งŒ ์˜๋ฏธ๊ฐ€ ์—†์Œ) ๋ฐฐ์—ด๋กœ ์ฃผ์–ด์ง€๊ณ  ์ดˆ๋‹น ์ตœ๋Œ€ ์ฒ˜๋ฆฌ๋Ÿ‰์„ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ๋ฌธ์ œ์˜ ๋งจ ์•„๋ž˜ ๋ถ€๋ถ„์˜ ๊ทธ๋ฆผ์„ ๋ณด๋ฉด, ์ดˆ๋‹น ๊ฒน์น˜๋Š” ์ฒ˜๋ฆฌ๋ถ€๋ถ„์ด ์ตœ๋Œ€ ๋ช‡๊ฐœ๋ƒ๋ฅผ ๊ตฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. 

๋ฌธ์ œ ํ’€์ด

์˜ˆ์ œ๋ฅผ ๋ณด๊ณ  ์ฒ˜์Œ์—” 0.001์ดˆ ์†Œ์ˆซ์  ์…‹์งธ ์งœ๋ฆฌ๊นŒ์ง€ ๊ณ„์‚ฐํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‘๊ฐ€์ง€ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค.

1. 1000์„ ๊ณฑํ•ด์„œ ์†Œ์ˆ˜์ ์„ ์ง€์šฐ๊ณ  ๊ณ„์‚ฐํ•œ๋‹ค.

2. swift์˜ DateFormatter ํด๋ž˜์Šค์™€ Date ํƒ€์ž…์˜ timeIntervalSince ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ์‹œ๊ฐ„ ๊ณ„์‚ฐ์„ ํ•œ๋‹ค. 

์ €๋Š” swift์˜ ์žฅ์ ์„ ์‚ด๋ฆด ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ๊ฒ ๊ตฌ๋‚˜ ํ•˜๋ฉฐ 2๋ฒˆ ๋ฐฉ๋ฒ•์„ ์„ ํƒํ•˜์˜€๊ณ  ๊ทธ ์ดํ›„๋กœ ์‚ฝ์งˆ์„ ์ •๋ง ๋งŽ์ด ํ–ˆ์Šต๋‹ˆ๋‹ค.

 

๋‚ ์งœ์™€ ์‹œ๊ฐ„(์‹œ,๋ถ„,์ดˆ)์ด ์ฃผ์–ด์ง€๊ณ  ์‹œ์ž‘์‹œ๊ฐ„๊ณผ ์™„๋ฃŒ์‹œ๊ฐ„์€ ๊ตฌํ–ˆ๋Š”๋ฐ, ์ดˆ๋‹น ์ตœ๋Œ€ ์ฒ˜๋ฆฌ๋Ÿ‰์„ ๊ตฌํ•˜๋‹ค๊ฐ€ ๋ง‰ํ˜€๋ฒ„๋ ธ์Šต๋‹ˆ๋‹ค. 

์ „ํ˜€ ๊ฐ์ด ์˜ค์ง€ ์•Š๋”๋ผ๊ตฌ์š”.. ๊ทธ๋ž˜์„œ ์นด์นด์˜ค ํ•ด์„ค์„ ๋ณด๊ณ  ํ’€์ด ๋ฐฉํ–ฅ์„ ์žก์„ ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

https://tech.kakao.com/2017/09/27/kakao-blind-recruitment-round-1/

 

์นด์นด์˜ค ์‹ ์ž… ๊ณต์ฑ„ 1์ฐจ ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ ๋ฌธ์ œ ํ•ด์„ค

‘๋ธ”๋ผ์ธ๋“œ’ ์ „ํ˜•์œผ๋กœ ์‹ค์‹œ๋˜์–ด ์‹œ์ž‘๋ถ€ํ„ฐ ์—„์ฒญ๋‚œ ํ™”์ œ๋ฅผ ๋ชฐ๊ณ  ์˜จ ์นด์นด์˜ค ๊ฐœ๋ฐœ ์‹ ์ž… ๊ณต์ฑ„. ๊ทธ ์ฒซ ๋ฒˆ์งธ ๊ด€๋ฌธ์ธ 1์ฐจ ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ๊ฐ€ ์ง€๋‚œ 9์›” 16์ผ(ํ† ) ์˜คํ›„ 2์‹œ๋ถ€ํ„ฐ 7์‹œ๊นŒ์ง€ ์žฅ์žฅ 5์‹œ๊ฐ„ ๋™์•ˆ ์˜จ๋ผ์ธ

tech.kakao.com

 

0.001 ๋‹จ์œ„๋กœ ๋ชจ๋“  ์‹œ๊ฐ„๋งˆ๋‹ค ์ฒ˜๋ฆฌ๋Ÿ‰์„ ์ „๋ถ€ ๊ณ„์‚ฐ์„ ํ•ด๋ฒ„๋ฆฌ๋ฉด,

1์ดˆ๋ฅผ ๊ณ„์‚ฐํ•˜๋Š”๋ฐ 1000๋ฒˆ์„ ๋Œ์•„ ๊ณ„์‚ฐํ•ด์•ผํ•˜๊ณ  ์ด๊ฒƒ์„ n๋ฒˆ ํ•˜๋ฃจ๋™์•ˆ ๊ณ„์‚ฐ์„ ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. (ํ•˜๋ฃจ๋ผ๋Š” ๊ฒƒ์€ ๋ฌธ์ œ ์กฐ๊ฑด์— ๋‚˜์™€์žˆ์Œ)

ํ•˜๋ฃจ * N * 1000

= 24 * 60 * 60 * 1000 * N 

= 86400000 * N (1<=N<=2000)

๋ณด๋ฉด ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋‚˜์˜ฌ ๊ฒƒ์ด๋ผ๊ณ  ์˜ˆ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๊ทธ๋Ÿผ ๋‹ค์‹œ ๋ฌธ์ œ์˜ ๋งจ ์•„๋ž˜ ๋ถ€๋ถ„์˜ ๊ทธ๋ฆผ์„ ๋ณด๋ฉด,

์ฒ˜๋ฆฌ๋Ÿ‰์ด ๋ณ€ํ•˜๋Š” ๋ถ€๋ถ„์€ ์‘๋‹ต์„ ์‹œ์ž‘ํ•˜๋Š” ๋ถ€๋ถ„๊ณผ ์™„๋ฃŒํ•˜๋Š” ๋ถ€๋ถ„ ๋‘ ๊ตฐ๋ฐ ๋ฟ์ด๋ผ๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

๊ทธ ๋‘ ๋ถ€๋ถ„์€ ํ•˜๋‚˜์˜ ์‘๋‹ต์ด๋ฏ€๋กœ ํ•œ ๊ตฐ๋ฐ๋งŒ ๊ณ„์‚ฐ์„ ํ•ด๋„ ๋˜๋Š”๋ฐ

์™„๋ฃŒ ์‹œ๊ฐ„์ด ์ด๋ฏธ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋˜์–ด ์žˆ๋Š” ๊ฒƒ์„ ๋ณด์•„ ์™„๋ฃŒ ์‹œ๊ฐ„๋งŒ ๊ณ ๋ คํ•˜๋ฉด ๋œ๋‹ค๋ฅผ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๊ฐ ์‘๋‹ต ์™„๋ฃŒ ์‹œ๊ฐ„๋งˆ๋‹ค ๋ชจ๋“  ์‘๋‹ต ์‹œ๊ฐ„์˜ ์‹œ์ž‘์‹œ๊ฐ„์—์„œ 1s๋งŒํผ์˜ ์‹œ๊ฐ„์„ ๋บ€ ์‹œ๊ฐ์„ ๋น„๊ตํ•˜์—ฌ์„œ ์‘๋‹ต ์™„๋ฃŒ ์‹œ๊ฐ„์ด ํฌ๋‹ค๋ฉด ํฌํ•จํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

์‘๋‹ต๋“ค์˜ ๊ฐœ์ˆ˜๋ฅผ N์ด๋ผ ํ•˜๋ฉด

๊ฐ ์‘๋‹ต๋งˆ๋‹ค ์‘๋‹ต๋“ค์„ ํ•œ๋ฒˆ์”ฉ ๋น„๊ตํ•ด๊ฐ€๋ฉด ๋˜๋‹ˆ 

N * N , ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(N^2)๋กœ ์œ„์˜ ๊ฒฝ์šฐ๋ณด๋‹ค ํ›จ์”ฌ ์ค„๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

But,,

์œ„ 2๋ฒˆ ๋ฐฉ๋ฒ•์œผ๋กœ ์ฒ˜์Œ์—๋Š” ํ’€๊ธฐ ์‹œ์ž‘ํ•˜์˜€์ง€๋งŒ ํ…Œ์ผ€ 3๋ฒˆ๊ณผ 18๋ฒˆ์—์„œ ๊ณ„์† ์‹คํŒจ๊ฐ€ ๋‚ฌ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋‹ค๋ฅธ ์–ธ์–ด๋กœ ํ‘ผ ์ฝ”๋“œ๋ฅผ ์ฐธ๊ณ ํ•˜์˜€๋Š”๋ฐ 1๋ฒˆ๋ฐฉ๋ฒ•์„ ์„ ํƒํ–ˆ๋”๋ผ๊ตฌ์š”. ๊ทธ๋ž˜์„œ ์ €๋„ 1๋ฒˆ ๋ฐฉ๋ฒ•์œผ๋กœ ๋ฐ”๊ฟ”๋ณด์•˜๊ณ  ํ†ต๊ณผํ–ˆ์Šต๋‹ˆ๋‹ค.

2๋ฒˆ ๋ฐฉ๋ฒ•์ด ์‹คํŒจํ•œ ์ด์œ ... ์•„์ง๋„ ์ •ํ™•ํžˆ๋Š” ๋ชจ๋ฅด๊ฒ ์ง€๋งŒ ์ œ๊ฐ€ ๋‚ด๋ฆฐ ๊ฒฐ๋ก ์€ Dateํƒ€์ž…์„ ์ด์šฉํ•˜์—ฌ ๊ณ„์‚ฐํ•˜๋ฉด ์†Œ์ˆซ์  ์…‹์งธ์งœ๋ฆฌ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์†Œ์ˆซ์  ๋„ท์งธ์ž๋ฆฌ, ๋‹ค์„ฏ์งธ์ž๋ฆฌ....  10๋ฒˆ์งธ ์ž๋ฆฌ๊นŒ์ง€ ๋” ์ •๋ฐ€ํ•˜๊ฒŒ ๊ณ„์‚ฐ์ด ๋˜์„œ 3๋ฒˆ๊ณผ 18๋ฒˆ์—์„œ ์˜ค๋ฅ˜ ๋‚˜๋Š” ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค. ์•„๋‹ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ฝ”๋“œ 

๋ฌธ์ž ํŒŒ์‹ฑํ•ด์„œ ์‘๋‹ต ์‹œ์ž‘ ์‹œ๊ฐ„๊ณผ ์‘๋‹ต ์™„๋ฃŒ ์‹œ๊ฐ„ ๊ตฌํ•˜๊ธฐ

์‹œ๊ฐ„ ์ •๋ณด๊ฐ€ ๋‚ ์งœ ์ •๋ณด์™€ ์„ž์—ฌ ์žˆ์œผ๋ฏ€๋กœ ์ ์ ˆํ•œ ์ •๋ณด๋ฅผ ์–ป๊ธฐ ์œ„ํ•ด ํŒŒ์‹ฑํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. 

๋ฌธ์ž์—ด ์ชผ๊ฐœ๋Š” ๋ฐฉ๋ฒ•! ๐Ÿ‘‰ 2022.01.15 - [iOS] - [Swift] String Index 

ํŒŒ์‹ฑํ•ด์ฃผ๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค๊ณ  lines๋ฐฐ์—ด์—์„œ ์›ํ•˜๋Š” ์œ„์น˜์˜ ์ •๋ณด๋ฅผ ๋นผ๋‚ด ์‘๋‹ต ์‹œ์ž‘ ์‹œ๊ฐ„ startIntTime๊ณผ ์‘๋‹ต ์™„๋ฃŒ ์‹œ๊ฐ„ endIntTime์„ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. ๋ฐ€๋ฆฌ์„ธ์ปจ๋“œ๋Š” ์‰ฝ๊ฒŒ ๊ณ„์‚ฐํ•ด์ฃผ๊ธฐ์œ„ํ•ด ์‹œ๋ถ„์ดˆ๋ฅผ 1000์„ ๊ณฑํ•˜๊ณ  ๋ฐ€๋ฆฌ์„ธ์ปจ๋“œ๋Š” ์ผ์˜์ž๋ฆฌ๋ถ€ํ„ฐ ๋ฐฑ์˜์ž๋ฆฌ๋กœ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. 

 

์ฒ˜๋ฆฌ์‹œ๊ฐ„์€ ์‹œ์ž‘์‹œ๊ฐ„๊ณผ ๋์‹œ๊ฐ„์„ ํฌํ•จํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์‘๋‹ต ์‹œ์ž‘ ์‹œ๊ฐ„์„ ๊ณ„์‚ฐํ•  ๋•Œ 1์„ ๋”ํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. (์›๋ž˜๋Š” 0.001์„ ๋”ํ•ด์•ผํ•˜๋Š”๋ฐ ์†Œ์ˆซ์ ์„ ์—†์• ์คฌ๊ธฐ ๋–„๋ฌธ์— 1์ž…๋‹ˆ๋‹ค.)

func getString(original: String, from: Int, to: Int) -> String {
    let startIndex = original.startIndex
    let start = original.index(startIndex, offsetBy: from)
    let end = original.index(startIndex, offsetBy: to)
    return String(original[start...end])
}
    var endIntTime = [Int]()
    var startIntTime = [Int]()

    lines.forEach {
        let strings = $0.split(separator: " ")
        let strings1 = String(strings[1])
        let hour = Int(getString(original: strings1, from: 0, to: 1))!
        let minute = Int(getString(original: strings1, from: 3, to: 4))!
        let second = Int(getString(original: strings1, from: 6, to: 7))!
        let millisecond =  Int(getString(original: strings1, from: 9, to: 11))!
        let timeInt: Int = (hour * 3600 + minute * 60 + second) * 1000 + millisecond
        endIntTime.append(timeInt)
        
        var strings2 = String(strings[2])
        strings2.removeLast()
        startIntTime.append(timeInt - Int(Double(strings2)! * 1000) + 1)
        
    }

 

์‘๋‹ต ์™„๋ฃŒ ์‹œ์ ๋งˆ๋‹ค ์ฒ˜๋ฆฌ๋Ÿ‰ ๊ณ„์‚ฐํ•˜๊ธฐ

์™„๋ฃŒ ์‹œ์ ๋งˆ๋‹ค ๋ชจ๋“  ์‘๋‹ต๋“ค์˜ ์‹œ์ž‘ ์‹œ๊ฐ„ - 1000(1์ดˆ)๋ฅผ ๋น„๊ตํ•˜์—ฌ ํฌ๋‹ค๋ฉด ์‘๋‹ต์ด ์ฒ˜๋ฆฌ๋œ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๊ณ  ๋”ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ํฌ๊ฑฐ๋‚˜ ๊ฐ™๋‹ค๊ฐ€ ์•„๋‹ˆ๋ผ ํฌ๋‹ค์ธ ์ด์œ ๋Š” ์ฒ˜๋ฆฌ ์‹œ๊ฐ„์— ์‹œ์ž‘ ์‹œ๊ฐ„๋„ ํฌํ•จ๋˜๊ธฐ ๋–„๋ฌธ์ž…๋‹ˆ๋‹ค. ๊ฐ™๋‹ค๋ฉด ์‹œ์ž‘ ์‹œ๊ฐ„ + 1 ๋ถ€ํ„ฐ ์‘๋‹ต์ด ์‹œ์ž‘ ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ฒ˜๋ฆฌํ•˜๋ฉด ์•ˆ๋ฉ๋‹ˆ๋‹ค.

    var maxResult: Int = 0
    for i in 0...lines.count-1 {
        var count = 0
        let currentTime = endIntTime[i]
        for j in i...lines.count-1 {
            if currentTime > startIntTime[j] - 1000 {
                count += 1
            }
        }
        maxResult = max(maxResult, count)
    }

 

์ „์ฒด ์ฝ”๋“œ 

 

import Foundation

func getString(original: String, from: Int, to: Int) -> String {
    let startIndex = original.startIndex
    let start = original.index(startIndex, offsetBy: from)
    let end = original.index(startIndex, offsetBy: to)
    return String(original[start...end])
}
func solution(_ lines:[String]) -> Int {
    var endIntTime = [Int]()
    var startIntTime = [Int]()

    lines.forEach {
        let strings = $0.split(separator: " ")
        let strings1 = String(strings[1])
        let hour = Int(getString(original: strings1, from: 0, to: 1))!
        let minute = Int(getString(original: strings1, from: 3, to: 4))!
        let second = Int(getString(original: strings1, from: 6, to: 7))!
        let millisecond =  Int(getString(original: strings1, from: 9, to: 11))!
        let timeInt: Int = (hour * 3600 + minute * 60 + second) * 1000 + millisecond
        endIntTime.append(timeInt)
        
        var strings2 = String(strings[2])
        strings2.removeLast()
        startIntTime.append(timeInt - Int(Double(strings2)! * 1000) + 1)
        
    }

    var maxResult: Int = 0
    for i in 0...lines.count-1 {
        var count = 0
        let currentTime = endIntTime[i]
        for j in i...lines.count-1 {
            if currentTime > startIntTime[j] - 1000 {
                count += 1
            }
        }
        maxResult = max(maxResult, count)
    }
    
    return maxResult
}