Skip to content

Conversation

@alstjr7437
Copy link
Member

@alstjr7437 alstjr7437 commented May 12, 2025

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

ํ›„๋ณด ์ถ”์ฒœํ•˜๊ธฐ

๋‹ค์‹œ ๊ทธ ๊ธฐ๊ฐ„์ด๋‹ˆ ๋ฌธ์ œ๋ฅผ ๋“ค๊ณ ์™”์Šต๋‹ˆ๋‹ค!(์ •์น˜ ํฌ์Šคํ„ฐ๋Š” ๋…ผ๋ž€์ด ์ƒ๊ธธ ๊ฒƒ ๊ฐ™์•„์„œ)

image

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

25๋ถ„

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

๋ฌธ์ œ๋Š” ๋‹จ์ˆœ ๊ตฌํ˜„์ด์˜€์Šต๋‹ˆ๋‹ค!!

  1. ์ด๋ฏธ ์žˆ๋Š” ํ›„๋ณด๋ฉด ์ถ”์ฒœ์ˆ˜ ์ถ”๊ฐ€
  2. ์ถ”์ฒœ์„ ๋ฐ›๋Š”๋ฐ ์ž๋ฆฌ๊ฐ€ ๋น„์–ด ์žˆ์œผ๋ฉด ๊ทธ๋ƒฅ ๋„ฃ๊ธฐ
  3. ์ž๋ฆฌ๊ฐ€ ์—†์œผ๋ฉด ์ถ”์ฒœ์ˆ˜๊ฐ€ ๋‚ฎ์€ ์‚ฌ๋žŒ ์ง€์šฐ๊ธฐ
  4. ๋งŒ์•ฝ ์ถ”์ฒœ์ˆ˜๊ฐ€ ๊ฐ™์œผ๋ฉด ์ œ์ผ ์˜ค๋ž˜๋œ ์‚ฌ๋žŒ ์ง€์šฐ๊ธฐ

์œ„ 4๊ฐœ์˜ ๋กœ์ง๋งŒ ์ƒ๊ฐํ•˜๋ฉด ๋์Šต๋‹ˆ๋‹ค.


๊ทธ๋ž˜์„œ ๋‹จ์ˆœํ•˜๊ฒŒ ์œ„ ๋กœ์ง ์ˆœ์„œ๋Œ€๋กœ ๋„ฃ์–ด์คฌ์Šต๋‹ˆ๋‹ค!

  1. recommend ๋ชจ๋‘ ๋ฐ˜๋ณต๋ฌธ ๋Œ๊ธฐ

  2. ์ด๋ฏธ result์— ์กด์žฌ?

  • ์ถ”์ฒœ์ˆ˜ ์ฆ๊ฐ€
  1. ์นธ ์ˆ˜ ๋ถ€์กฑ?
  • ํ›„๋ณด์— ๋„ฃ๊ธฐ
  1. ์นธ ์ˆ˜๊ฐ€ ์—†์œผ๋ฉด?
  • ์ถ”์ฒœ ์ˆ˜ ์ œ์ผ ๋‚ฎ์€ ๊ฐ’ ๋ฝ‘์•„๋‚ด๊ธฐ
  • ์ถ”์ฒœ ์ˆ˜๊ฐ€ ๊ฐ™์€๊ฒŒ ์žˆ์œผ๋ฉด? time์ด ์ œ์ผ ์˜ค๋ž˜๋œ๊ฑฐ ๋ฝ‘์•„๋‚ด๊ธฐ
  1. ๋งˆ์ง€๋ง‰์— sortํ•ด์„œ ๊ฒฐ๊ณผ ์ถœ๋ ฅํ•˜๊ธฐ

์œ„ ๋กœ์ง๋Œ€๋กœ ํ˜๋Ÿฌ๊ฐ”์Šต๋‹ˆ๋‹ค!!
์•„๋งˆ ์•„๋ž˜ ์ •๋‹ต ์ฝ”๋“œ๋ฅผ ๋ณด์‹œ๋ฉด ์ดํ•ด๊ฐ€ ํŽธํ•˜์‹ค๊บผ์—์š”!(์ฃผ์„๋„ ๋‹ฌ์•„๋†จ์Šต๋‹ˆ๋‹ค!)

์ •๋‹ต ์ฝ”๋“œ

let N = Int(readLine()!)!
let totalCount = Int(readLine()!)!
let recommend = readLine()!.split(separator: " ").map { Int(String($0))! }

var result: [Int: (recommendCount: Int, time: Int)] = [:]
var currentTime = 0

for student in recommend {
    // ์ด๋ฏธ ํ›„๋ณด์— ์กด์žฌ
    if result[student] != nil {
        result[student]!.recommendCount += 1
    } else {
        // ํ›„๋ณด ์นธ์ด ๋น„์–ด ์žˆ์œผ๋ฉด
        if result.count < N {
            result[student] = (1, currentTime)
        } else {
            // ๋น„์–ด ์žˆ์ง€ ์•Š์œผ๋ฉด
            let removeValue = result.min {
                // ์ถ”์ฒœ ์ˆ˜๊ฐ€ ๊ฐ™์œผ๋ฉด ์‹œ๊ฐ„์ด ์ œ์ผ ์˜ค๋ž˜๋œ๊ฑฐ ๋ฆฌํ„ด
                if $0.value.recommendCount == $1.value.recommendCount {
                    return $0.value.time < $1.value.time
                }
                // ์ถ”์ฒœ ์ˆ˜๊ฐ€ ์ œ์ผ ์ž‘์€๊ฑฐ ๋ฆฌํ„ด
                return $0.value.recommendCount < $1.value.recommendCount
            }!.key
            
            // ์ด๋ฏธ ์กด์žฌํ•˜๋Š” ํ›„๋ณด๋ฅผ ์—†์• ๊ณ  ์ƒˆ ํ›„๋ณด๋ฅผ ๋„ฃ๊ธฐ
            result.removeValue(forKey: removeValue)
            result[student] = (1, currentTime)
        }
    }
    currentTime += 1
}

print(result.keys.sorted().map { String($0) }.joined(separator: " "))

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

๋”•์…”๋„ˆ๋ฆฌ ์•ˆ์— set์„ ๋„ฃ์–ด์„œ ๋ณ€์ˆ˜๋ช…์„ ์ง€์–ด์„œ ์ž‘์„ฑํ•˜๋‹ˆ ๋ฌธ์ œ๋ฅผ ํ’€๋•Œ ํ™•์‹คํžˆ ๊ฐ€๋…์„ฑ๋„ ์ข‹๊ณ  ์ดํ•ด๋„ ํŽธํ•˜๋”๋ผ๊ตฌ์š”!!

์ €๋„ ์ฒ˜์Œ์—๋Š” ๊ทธ๋ƒฅ var result: [Int: (Int, Int)] = [:]์œผ๋กœ ํ’€์—ˆ๋‹ค๊ฐ€ ๋ฆฌํŒฉํ† ๋ง์„ ํ•ด์คฌ์Šต๋‹ˆ๋‹ค!

์ด์ „

var result: [Int: (Int, Int)] = [:]

if $0.value.0== $1.value.0 {
    return $0.value.1 < $1.value.1
}

์ดํ›„

var result: [Int: (recommendCount: Int, time: Int)] = [:]

if $0.value.recommendCount == $1.value.recommendCount {
    return $0.value.time < $1.value.time
}

@sep037
Copy link
Collaborator

sep037 commented May 14, 2025

์€ ๋จธ

์€๊ทผ ๋จธ๋ฆฌ ์•„ํ”ˆ ๋ฌธ์ œ์˜€์Œ

IMG_2198
์‚ฌ์ง„ ์ฒ˜๋Ÿผ ๋งค๋ฒˆ ์ดˆ๊ธฐํ™” ํ•ด์ฃผ๋ ค๊ณ  ํ•˜๋‹ค๊ฐ€ ์–ด์ฐจํ”ผ ๋ฐฐ์—ด์—์„œ ๋‚˜๊ฐ€๋ฉด ๋‹ค ์ƒ๊ด€ ์—†๋‹ค๋Š” ๊ฑธ ๋ฌธ์ œ ํ’€๋‹ค๊ฐ€ ๊นจ๋‹ฌ์•˜์Šต๋‹ˆ๋‹ค ..~

//
//  main.swift
//  1713
//
//  Created by Seungeun Park on 5/13/25.
//

import Foundation

let picturelimit = Int(readLine()!)!
let recommendCount = Int(readLine()!)!

struct Candidate {
    var num : Int
    var votes : Int
    var time : Int = 0
}

var time = 0

var candidateArray : [Candidate] = []
let recommendations = readLine()!.split(separator: " ").map { Int($0)! }

for candidateNum in recommendations {
    time += 1 // ์ผ๋‹จ ์‹œ๊ฐ„์€ ํ˜๋Ÿฌ๊ฐ€๋‹ˆ๊นŒ ..
    
    /*
     if ํ›„๋ณด์— ์˜ฌ๋ผ๊ฐ€์žˆ์ง€ ์•Š์œผ๋ฉด {
         if ์‚ฌ์ง„ํ‹€์ด ๋น„์–ด์žˆ์œผ๋ฉด or ์ž๋ฆฌ๊ฐ€ ์žˆ์œผ๋ฉด {
             // ๊ทธ๋ƒฅ ์˜ฌ๋ฆฌ๊ธฐ
         } else {
             // ๊ฐ€์žฅ ์ ๊ฒŒ ์ถ”์ฒœ๋ฐ›์€ ์‚ฌ๋žŒ ์ œ๊ฑฐํ•˜๊ณ  ์ƒˆ ํ›„๋ณด ์˜ฌ๋ฆฌ๊ธฐ
         }
     } else {
         // ์ด๋ฏธ ์˜ฌ๋ผ๊ฐ€ ์žˆ์œผ๋ฉด ํˆฌํ‘œ ์ˆ˜๋งŒ ์˜ฌ๋ฆฌ๊ธฐ
     }
     */
    
    if let index = candidateArray.firstIndex(where: {$0.num == candidateNum}) { // ํ•ด๋‹น ํ›„๋ณด ๋ฒˆํ˜ธ๊ฐ€ ์žˆ๋Š” ์ธ๋ฑ์Šค ํ™•์ธ
        candidateArray[index].votes += 1 // ์ด๋ฏธ ์˜ฌ๋ผ๊ฐ€ ์žˆ์œผ๋ฉด ์ˆ˜๋งŒ ์˜ฌ๋ฆฌ๊ธฐ
    } else { // ์˜ฌ๋ผ๊ฐ€์žˆ์ง€ ์•Š์œผ๋ฉด
        if candidateArray.count < picturelimit { // ์ž๋ฆฌ ์žˆ์œผ๋ฉด ๊ทธ๋ƒฅ ์ถ”๊ฐ€
            candidateArray.append(Candidate(num: candidateNum, votes: 1, time: time))
        } else { // ์ž๋ฆฌ๊ฐ€ ์—†์œผ๋ฉด ๋น„๊ตํ•ด์ฃผ๊ธฐ
            candidateArray.sort { // ์ถ”์ฒœ ์ˆ˜ ๊ฐ™์œผ๋ฉด
                if $0.votes == $1.votes {
                    return $0.time < $1.time // ์‹œ๊ฐ„์œผ๋กœ ๋น„๊ต
                }
                return $0.votes < $1.votes // ์•„๋‹ˆ๋ฉด ์ถ”์ฒœ ์ˆ˜๋กœ ๋น„๊ต
                }
            candidateArray.removeFirst()
            candidateArray.append(Candidate(num: candidateNum, votes: 1, time: time))
            }
            
        }
        
    }
    
let result = candidateArray.map { $0.num }.sorted() // ํ›„๋ณด ๋ฒˆํ˜ธ ์˜ค๋ฆ„์ฐจ์ˆœ !
print(result.map { String($0) }.joined(separator: " "))
    

Copy link
Member

@giljihun giljihun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ „์ฒด ๋กœ์ง์€ ๋™์ผํ•œ ๊ฒƒ ๊ฐ™๋„ค์š”~

/* MARK: - SOLUTION
 
 ํ˜„์žฌ๊นŒ์ง€ ์ถ”์ฒœ ๋ฐ›์€ ํšŸ์ˆ˜๊ฐ€ ๊ฐ€์žฅ ์ ์€ ํ•™์ƒ์˜ ์‚ฌ์ง„์„ ์‚ญ์ œํ•˜๊ณ ,
 ๊ทธ ์ž๋ฆฌ์— ์ƒˆ๋กญ๊ฒŒ ์ถ”์ฒœ๋ฐ›์€ ํ•™์ƒ์˜ ์‚ฌ์ง„์„ ๊ฒŒ์‹œ.
 (2๋ช… ์ด์ƒ์ด๋ฉด, ๊ฐ€์žฅ ๊ฒŒ์‹œ๋œ ์ง€ ์˜ค๋ž˜๋œ ์‚ฌ์ง„ ์‚ญ์ œ)
 (์ด ๊ฒฝ์šฐ, ์ถ”์ฒœ๋ฐ›์€ ํšŸ์ˆ˜๋Š” 0์œผ๋กœ ์ดˆ๊ธฐํ™”๋จ.)

 ์ข… ํ›„๋ณด์˜ ํ•™์ƒ ๋ฒˆํ˜ธ๋ฅผ ์ฆ๊ฐ€ํ•˜๋Š” ์ˆœ์„œ๋Œ€๋กœ ์ถœ๋ ฅ
 
 1. ์ž…๋ ฅ
 2.
    - ๋”•์…”๋„ˆ๋ฆฌ๋กœ ์ถ”์ฒœํšŸ์ˆ˜, ์ˆœ์„œ๋ฅผ ๊ด€๋ฆฌ.
    - ์ œ๊ฑฐ๋Š” ์ถ”์ฒœ ํšŸ์ˆ˜๊ฐ€ ๊ฐ€์žฅ ๋‚ฎ์€๋†ˆ OUT (๋™ํ‘œ ์‹œ, ์ˆœ์„œ๊ฐ€ ์ ค ๋น ๋ฅธ๋†ˆ)
 */

import Foundation


// MARK: - ์ž…๋ ฅ
guard let input = readLine(),
      let N = Int(input),
      (1...20).contains(N) else { exit(0) }

guard let votingCnt = Int(readLine() ?? "") else { exit(0) }

if votingCnt == 0 {
    exit(0)
}

guard let votingPapers = readLine()?.split(separator: " ").compactMap({ Int($0) }) else { exit(0) }

// MARK: - ํ›„๋ณด ๋“ฑ๋ก ๋กœ์ง
var candidates: [Int: (votedCnt: Int, order: Int)] = [:]
var order = 1

votingPapers.forEach {
    addCandidate($0)
}

// MARK: - ์ถœ๋ ฅ
print(candidates.keys.sorted().map { String($0) }.joined(separator: " "))

// MARK: - ํ•จ์ˆ˜

// ํ›„๋ณด ๋“ฑ๋ก ํ•จ์ˆ˜
func addCandidate(_ candidate: Int) {
    if let _ = candidates[candidate] {
        votingCandidate(candidate)
        return
    }

    if candidates.count == N {
        delCandidate()
    }

    candidates[candidate] = (1, order)
    order += 1
}

// ํˆฌํ‘œ ๋ฐ›์€ ํšŸ์ˆ˜ ์ฆ๊ฐ€
func votingCandidate(_ candidate: Int) {
    candidates[candidate]!.votedCnt += 1
}

// ํ›„๋ณด์ž ์ œ๊ฑฐ
func delCandidate() {
    let minVote = candidates.values.map { $0.votedCnt }.min()
    let leastVoted = candidates.filter { $0.value.votedCnt == minVote }
    if let target = leastVoted.min(by: { $0.value.order < $1.value.order }) {
        candidates.removeValue(forKey: target.key)
    }
}

์กฐ๊ฑด์ด ๋งŽ์€ ๊ตฌํ˜„๋ฌธ์ œ๋Š”
๊ฐ ์ ์œผ๋กœ ๋” ์ฝ”๋“œ๋ฅผ ๊น”๋”ํ•˜๊ฒŒ ์งœ๋ ค๊ณ  ํ•˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค๊ณ  ๋А๊ผˆ์Šต๋‹ˆ๋‹ค.
๊ทธ๋ž˜์•ผ ์˜ˆ์™ธ์ฒ˜๋ฆฌํ•  ๋ถ€๋ถ„๋„ ๋” ๋ณด์ด๊ณ  ํ•˜๋Š” ๊ฒƒ ๊ฐ™์•„์š”.
๊น”๋”ํ•œ์ง„ ๋ชจ๋ฅด๊ฒ ์ง€๋งŒ์š”..

์ด๋ฒˆ ๋ฌธ์ œ์—์„œ ๊ณ„์† 1%์—์„œ ์˜ค๋ฅ˜๊ฐ€ ๋– ์„œ... ์—๋Ÿฌ์žก๋А๋ผ ์กฐ๊ธˆ ์‹œ๊ฐ„์ด ๊ฑธ๋ ธ๋Š”๋ฐ
์ •๋ง ๋ณ„๊ฑฐ ์•„๋‹Œ๋ฐ, ์น˜๋ช…์ ์ธ ๋ฌธ์ œ์˜€์–ด์š”!

var candidates: [Int: (votedCnt: Int, order: Int)]

์ด๋ ‡๊ฒŒ ์œ„ ์ฝ”๋“œ์ฒ˜๋Ÿผ ํ›„๋ณด์ž ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๊ตฌ์„ฑํ•ด์•ผํ•˜๋Š”๋ฐ

var candidates: [String: (votedCnt: Int, order: Int)]

์œ„ ์ฝ”๋“œ์ฒ˜๋Ÿผ ํ›„๋ณด์ž๋ฅผ String์œผ๋กœ ์„ ์–ธํ•ด๋†“๊ณ ... ๋””๋ฒ„๊น…ํ•˜๊ณ  ์žˆ์—ˆ์–ด์š”..

์ด๋ ‡๊ฒŒํ•˜๋ฉด ์ถœ๋ ฅ๋ถ€์—์„œ ์˜ˆ๋ฅผ๋“ค์–ด ํ›„๋ณด์ž๊ฐ€ 10, 2 ์˜€๋‹ค๋ฉด
๋ฌธ์ œ ์กฐ๊ฑด์— ๋”ฐ๋ผ 2 10 ์œผ๋กœ ์ถœ๋ ฅ์ด ๋˜์–ด์•ผํ•˜๋Š”๋ฐ..
String์„ ์ •๋ ฌํ•ด๋ดค์ž.. 10 2 ๋กœ ๋‚˜์˜ค๋Š” ๊ฒƒ์ด์ฃ ..

๋ฐ˜๋ก€๋ฅผ ์ฐพ์•„์„œ ๋‹คํ–‰์ด๊ณ .. ์•ž์œผ๋กœ๋Š” ํƒ€์ž…์„ ์ข€ ๋” ์‹ ๊ฒฝ์จ์•ผ๊ฒ ๋‹ค๊ณ  ๋А๊ผˆ์Šต๋‹ˆ๋‹ค.

@@ -0,0 +1,29 @@
let N = Int(readLine()!)!
let totalCount = Int(readLine()!)!
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

guard let votingCnt = Int(readLine() ?? "") else { exit(0) }

if votingCnt == 0 {
    exit(0)
}

์ „ ์ž๊พธ ์—๋Ÿฌ๋– ์„œ
"์„ค๋งˆ ๋ฌด์Šจ ํˆฌํ‘œ์ž 0๋ช…! ์ด๋Ÿฐ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์•ผํ•˜๋‚˜? ใ… ใ… " ํ•˜๋ฉด์„œ
์—ฌ๊ธฐ์— ์ด๋Ÿฐ ์กฐ๊ฑด๋„ ์ถ”๊ฐ€ํ•˜๊ณ  ๊ทธ๋žฌ๋„ค์š” ใ…‹ใ…‹ใ…‹

let recommend = readLine()!.split(separator: " ").map { Int(String($0))! }

var result: [Int: (recommendCount: Int, time: Int)] = [:]
var currentTime = 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var candidates: [Int: (votedCnt: Int, order: Int)] = [:]
var order = 1

currentTime ๋ณ€์ˆ˜๋ช… ์ข‹๋„ค์š”, ์ €๋Š” order(์ˆœ์„œ)๋กœ ํ–ˆ์Šด๋‹ค.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

order ์ข‹๋„ค์š” ์ €๋Š” time? ํ•˜๋ ค๋‹ค๊ฐ€ ์™€๋‹ฟ์ง€์•Š์•„์„œ numberํ•ด์คฌ๋Š”๋ฐ, (์ €๋Š” ๊ธฐํ˜ธ~๋ฒˆ์„ symbol๋กœ ํ•ด์คฌ์Šต๋‹ˆ๋‹ค.) ํ—ท๊ฐˆ๋ฆฌ๋”๋ผ๊ตฌ์š”

@sep037
Copy link
Collaborator

sep037 commented May 15, 2025

๋˜ approveํ•ด์„œ ์ฃ„์†ก ใ…  ..

Copy link
Collaborator

@YooGyeongMo YooGyeongMo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let n = Int(readLine()!)!
let m = Int(readLine()!)!
let recommend = readLine()!.split(separator:" ").map { Int(String($0))! }

func photoFrames(frameCount n: Int, recommendationList nums: [Int]) -> [Int] {
    var stack: [Int] = []
    var count = Array(repeating: 0, count: 101)
    
    for student in nums {
        if stack.contains(student) {
            count[student] += 1
            continue
        }

        if stack.count == n {
            // ์ถ”์ฒœ ์ˆ˜๊ฐ€ ๊ฐ€์žฅ ์ ์€ ์‚ฌ๋žŒ ์ฐพ๊ธฐ
            let minVote = stack.map { count[$0] }.min()!
            
            // ๊ฐ€์žฅ ๋จผ์ € ๊ฑธ๋ฆฐ ํ•™์ƒ ์ค‘ ์ถ”์ฒœ ์ˆ˜ == min ์ธ ์‚ฌ๋žŒ ์ œ๊ฑฐ
            for old in stack {
                if count[old] == minVote {
                    count[old] = 0
                    if let idx = stack.firstIndex(of: old) {
                        stack.remove(at: idx)
                    }
                    break
                }
            }
        }

        // ์Šคํƒ์ฒ˜๋Ÿผ ๋’ค์— ์ถ”๊ฐ€
        stack.append(student)
        count[student] = 1
    }

    return stack.sorted()
}

let result = photoFrames(frameCount: n, recommendationList: recommend)
print(result.map { String($0) }.joined(separator: " "))

stack ์œผ๋กœ ์งœ๋ณด์•˜์Šต๋‹ˆ๋‹ค.

Comment on lines +9 to +29
if result[student] != nil {
result[student]!.recommendCount += 1
} else {
if result.count < N {
result[student] = (1, currentTime)
} else {
let removeValue = result.min {
if $0.value.recommendCount == $1.value.recommendCount {
return $0.value.time < $1.value.time
}
return $0.value.recommendCount < $1.value.recommendCount
}!.key

result.removeValue(forKey: removeValue)
result[student] = (1, currentTime)
}
}
currentTime += 1
}

print(result.keys.sorted().map { String($0) }.joined(separator: " "))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋”•์…”๋„ˆ๋ฆฌ์˜ ํ‚ค์™€ ๋ฒจ๋ฅ˜ ์‚ฌ์šฉํ•˜์…”์„œ ํ‘ธ์…ง๋„ค์š”.
์ฒ˜์Œ์—๋Š”

struct Candidate {
    let number: Int
    var votes: Int
    var time: Int
}

struct๋กœ ์ƒํƒœ๊ด€๋ฆฌํ•˜๋ ค๊ณ  ํ–ˆ์—ˆ๋Š”๋ฐ ์—ญ์‹œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์—์„œ๋Š” ๋”•์…”๋„ˆ๋ฆฌ๊ฐ€ ์งœ์„ธ๊ตฐ์š”.

์‚ฌ์‹ค ์ €๋Š” ๋ฌธ์ œ ๋ณด์ž๋งˆ์ž stack๊ตฌ์กฐ๊ฐ€ ๋– ์˜ฌ๋ผ์„œ ๋ฐ”๋กœ stack๊ตฌ์กฐ๋ฅผ ์งœ๋ณด์•˜์Šต๋‹ˆ๋‹ค.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ €๋„ ๋ณด๊ณ  struct, class ์จ๋ณด๊ณ  ์‹ถ์–ด์„œ ์–ต์ง€๋กœ ์จ๋ณด๋‹ค๊ฐ€ ์ข€ ์˜ค๋ž˜๊ฑธ๋ฆฌ๊ณ  ๋ณต์žกํ•ด์ง„ ๊ฒƒ ๊ฐ™์•„์š”. ์ •๋ง ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ dictionary๊ฐ€ ๋งŽ์€๊ฑธ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋„ค์š” ใ„ทใ„ท..
dictionary ๊ตฌ์กฐ๋ฅผ key value ์“ฐ๋ฉด์„œ ์—„์ฒญ ๊น”๋”ํ•˜๊ฒŒ ์ž˜ ํ‘ธ์‹ ๊ฒƒ๊ฐ™์Šต๋‹ˆ๋‹ค... ๋ฐฐ์›Œ๊ฐ€์š”

Copy link
Collaborator

@MuchanKim MuchanKim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ „ ๋ญ”๊ฐ€ ๊ตฌํ˜„ ๋ฌธ์ œ๊ฐ€ ์žฌ๋ฐŒ๊ธฐ๋„ํ•˜๊ณ  ๋„์›€๋„ ๋งŽ์ด๋˜๋Š” ๊ฒƒ ๊ฐ™๋„ค์š”..ใ…Žใ…Ž

๋‹ค๋ฅธ๋ถ„๋“ค ์ฝ”๋“œ๋ž‘ ๋น„๊ตํ•ด์„œ ๋ณด๋Š”๋ฐ ํฌ๊ฒŒ ๋‹ค๋ฅธ์ ์€ ์ „ ์˜ค๋ž˜๋œ ์‚ฌ์ง„์„ ๊ตฌ๋ถ„ํ•˜๊ธฐ์œ„ํ•œ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์ธ๋ฑ์Šค๋กœ ํŒ๋ณ„ํ•ด์คฌ์–ด์š”. ๊ทผ๋ฐ ๋‹ค๋ฅธ๋ถ„๋“ค์ฒ˜๋Ÿผ ๋ช…์‹œ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•ด์ฃผ๋Š”๊ฒŒ ๊ฐ€๋…์„ฑ ๋ฉด์—์„œ๋‚˜ ์„ฑ๋Šฅ๋ฉด์—์„œ๋„ ์ข‹๋‹ค๊ณ  ๋А๊ปด์ง€๋„ค์š”. ๊ณ ์ƒํ•˜์…จ์Šด๋‹ค!

import Foundation

struct Candidate {
   let studentNumber: Int
   var recommendCount: Int
   
   mutating func incrementRecommend() {
       recommendCount += 1
   }
   
   mutating func resetRecommend() {
       recommendCount = 0
   }
}

var candidates: [Candidate] = []
var res: [Int] = []

// input
let N = Int(readLine()!)!
let recommandCount = Int(readLine()!)!
let recommandNumbers: [Int] = readLine()!.split(separator: " ").map { Int($0)! }

for studentNumber in recommandNumbers {
   // ์ด๋ฏธ ๊ฒŒ์‹œ๋œ ํ•™์ƒ์ธ์ง€ ํ™•์ธ
   if let index = candidates.firstIndex(where: { $0.studentNumber == studentNumber }) {
       // ์ด๋ฏธ ๊ฒŒ์‹œ๋œ ๊ฒฝ์šฐ ์ถ”์ฒœ ํšŸ์ˆ˜๋งŒ ์ฆ๊ฐ€
       candidates[index].incrementRecommend()
   } else {
       // ์ƒˆ๋กœ์šด ํ•™์ƒ์ธ ๊ฒฝ์šฐ
       if candidates.count < N {
           // ์‚ฌ์ง„ํ‹€์ด ๋น„์–ด์žˆ๋Š” ๊ฒฝ์šฐ
           candidates.append(Candidate(studentNumber: studentNumber, recommendCount: 1))
       } else {
           // ์‚ฌ์ง„ํ‹€์ด ๊ฐ€๋“ ์ฐฌ ๊ฒฝ์šฐ
           // ์ถ”์ฒœ ํšŸ์ˆ˜๊ฐ€ ๊ฐ€์žฅ ์ ๊ณ , ๊ฐ€์žฅ ์˜ค๋ž˜๋œ ํ›„๋ณด ์ฐพ๊ธฐ
           let minRecommend = candidates.min { $0.recommendCount < $1.recommendCount }!.recommendCount
           if let index = candidates.firstIndex(where: { $0.recommendCount == minRecommend }) {
               // ๊ฐ€์žฅ ์˜ค๋ž˜๋œ ํ›„๋ณด ์ œ๊ฑฐ
               candidates.remove(at: index)
               // ์ƒˆ๋กœ์šด ํ›„๋ณด ์ถ”๊ฐ€
               candidates.append(Candidate(studentNumber: studentNumber, recommendCount: 1))
           }
       }
   }
}

res = candidates.map { $0.studentNumber }.sorted()

print(res.map { String($0) }.joined(separator: " "))

Comment on lines +9 to +11
if result[student] != nil {
result[student]!.recommendCount += 1
} else {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ตœ๊ทผ์— swiftyํ•˜๊ฒŒ ์ฝ”๋“œ ์“ฐ๋Š” ๋ฒ•์—๋Œ€ํ•ด ๊ณ ๋ฏผํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ต์…”๋„ nil ๊ฐ’์„ ์ฒดํฌํ•ด์ฃผ๋Š” ์ฒด์ด๋‹์ด ์žˆ๋Š”๋ฐ ์ €๋„ ํ‚จ๋”์ฒ˜๋Ÿผ ์ต์ˆ™ํ•œ ๋ฐฉ๋ฒ•๋งŒ ์“ฐ๊ฒŒ ๋˜๋”๋ผ๊ณ ์š”. ์ฒด์ด๋‹์„ ์จ์„œ ์•„๋ž˜์ฒ˜๋Ÿผ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์“ธ ์ˆ˜๋„ ์žˆ๊ฒ ๋„ค์š”!

์˜ˆ์‹œ

// ๊ธฐ์กด
if result[student] != nil {
    result[student]!.recommendCount += 1
}

// ์ฒด์ด๋‹
result[student]?.recommendCount += 1

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ด๊ฑฐ ์งฑ์งฑ swifty ใ…‡ใ…ˆํ•ฉ๋‹ˆ๋‹ค.

let totalCount = Int(readLine()!)!
let recommend = readLine()!.split(separator: " ").map { Int(String($0))! }

var result: [Int: (recommendCount: Int, time: Int)] = [:]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋”•์…”๋„ˆ๋ฆฌ๋กœ ํ•˜์…จ๊ตฐ์š”.

Copy link
Collaborator

@bishoe01 bishoe01 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋ฌธ์ œ๋ณด๊ณ  ์ข€ swift์Šค๋Ÿฝ๊ฒŒ ํ’€๋ ค๊ณ  ์ด๋ ‡๊ฒŒ ์ข€ ํŠน์ดํ•˜๊ฒŒ ํ’€์–ด๋ดค์Šต๋‹ˆ๋‹ค.

Candidate๋Š” struct์œผ๋กœ ๊ตฌํ˜„ํ•ด์คฌ์–ด์š”. ์ข€ ๊ตฌ์กฐ๊ฐ€ ๋‹จ์ˆœํ•˜๊ธฐ๋Ÿ๊ณ  ์ข‹์•„์š” ์ถ”๊ฐ€์ •๋„๋ฐ–์— ์—†์–ด์„œ struct๋กœ ํ•ด์คฌ๊ณ ,
Gallery๋Š” class๋กœ ๊ตฌํ˜„ํ•ด์คฌ๋Š”๋ฐ, ๋‚ด๋ถ€ ์ƒํƒœ lines๋ฅผ ๊ณ„์† ๋ฐ”๊ฟ”์ค„ ๊ฒƒ ๊ฐ™์•„์„œ class๋กœ ๋’€์Šต๋‹ˆ๋‹ค.
์ด ๋‘๊ฐ€์ง€๋ฅผ ์ข€ ๋ช…ํ™•ํ•˜๊ฒŒ ๋ญ๊ฐ€ ์ข‹๋‹คํ•˜๊ณ  ๊ตฌ๋ถ„ํ•˜๋Š”๊ฒŒ ์žˆ๋Š”์ง€.. ๊ถ๊ธˆํ•˜๋„ค์š”.. ๋ง๊ณ ๋ž‘ ์‚ด์ง ์–˜๊ธฐํ–ˆ์„๋•Œ๋Š” struct๋กœ ํ•˜๋Š”๊ฑธ ์ข€ ์ง€ํ–ฅํ•˜๋ผ๊ณ  ํ•˜์‹œ๊ธด ํ•˜์…จ๋Š”๋ฐ, ์ข€ ๋” ์•Œ์•„๋ด์•ผํ•  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค...

import Foundation

struct Candidate: Equatable {
    var symbol: Int
    var like: Int = 1
    let idx: Int

    mutating func updateLike() {
        like += 1
    }

    // ๋ถˆ๋ฆฐ์ˆœ์„œ
//    mutating func updateIdx(_ idx: Int) {
//        self.idx = idx
//    }
}

class Gallery {
    var limit: Int
    var lines = [Candidate]()

    init(_ limit: Int) {
        self.limit = limit
        self.lines = []
    }

    // ํ‡ด์ถœ์ œ๋„
    func out() {
        let minLike = lines.min(by: {
            if $0.like == $1.like {
                return $0.idx < $1.idx
            }
            return $0.like < $1.like
        })!

        lines.remove(at: lines.firstIndex(where: { $0.symbol == minLike.symbol })!)
    }

    func append(_ candidate: Candidate) {
        if let alreadyIdx = lines.firstIndex(where: { $0.symbol == candidate.symbol }) {
            lines[alreadyIdx].updateLike()
        }
        else {
            // ๊ฝ‰ ์ฐจ๋ฉด ํ‡ด์ถœ 
            if lines.count == limit {
                out()
            }
            // ์•„์ง ๊ฝ‰ ์ฐจ์ง€ ์•Š์•˜๋‹ค๋ฉด ? ๊ฑ ์ถ”๊ฐ€
            lines.append(candidate)
        }
        
    }

    func answer() {
        lines.sort { $0.symbol < $1.symbol }
//        print(lines)
        lines.map { $0.symbol }.forEach {
            print($0, terminator: " ")
        }
    }
}

func solution(_ n: Int, _ arr: [Int]) {
    let gallery = Gallery(n)

    for (idx, item) in arr.enumerated() {
        gallery.append(Candidate(symbol: item, idx: idx))
    }
//    gallery.out()
    gallery.answer()
}

๊ณ„์† ์ •๋‹ต 20%์—์„œ ํ‹€๋ ธ์Šต๋‹ˆ๋‹ค๊ฐ€ ๋ฌดํ•œ์œผ๋กœ ๋“ฑ์žฅ

append ํ•จ์ˆ˜์—์„œ ๋ฌธ์ œ๊ฐ€ ์žˆ์—ˆ์Œ. ๊ทธ๋ƒฅ ๊ฝ‰ ์•ˆ์ฐผ์œผ๋ฉด ์ƒ๊ฐ์—†์ด append๋ฅผ ํ•ด์ฃผ๊ณ  ์žˆ๋‹ค๋ณด๋‹ˆ, ์ค‘๋ณต์ด ๋ง‰๋“ค์–ด๊ฐ !!!

๊ธฐ์กด append

    func append(_ candidate: candidate) {
        // ์•„์ง ๊ฝ‰ ์ฐจ์ง€ ์•Š์•˜๋‹ค๋ฉด ? ๊ฑ ์ถ”๊ฐ€
        if lines.count < limit {
            lines.append(candidate)
        } else {
            // ๋งŒ์•ฝ์— ์ด๋ฏธ ์žˆ๋Š” ์• ๋ผ๋ฉด !
            if let alreadyIdx = lines.firstIndex(where: { $0.symbol == candidate.symbol }) {
                lines[alreadyIdx].updateLike()
            } else {
                // ์ด๋ฏธ ์žˆ๋Š” ์• ๊ฐ€ ์—†์œผ๋ฉด ํ›„๋ณด ๊ต์ฒด
                out()
                lines.append(candidate)
            }
        }
    }

๋ณ€๊ฒฝํ›„ append

 func append(_ candidate: Candidate) {
        if let alreadyIdx = lines.firstIndex(where: { $0.symbol == candidate.symbol }) {
            lines[alreadyIdx].updateLike()
        }
        else {
            // ๊ฝ‰ ์ฐจ๋ฉด ํ‡ด์ถœ 
            if lines.count == limit {
                out()
            }
            // ์•„์ง ๊ฝ‰ ์ฐจ์ง€ ์•Š์•˜๋‹ค๋ฉด ? ๊ฑ ์ถ”๊ฐ€
            lines.append(candidate)
        }        
    }

ํ’€์ด

  • ์›๋ž˜ ์ตœ๊ทผ์— ์ถ”์ฒœ๋ชป๋ฐ›์€์• ๊ฐ€ ํƒˆ๋ฝํ•˜๋Š”์ง€์•Œ๊ณ , updateIdx๋ฅผ ์‚ฌ์šฉํ•˜๋‹ค๊ฐ€ ํŒŒ๊ธฐํ–ˆ์Šต๋‹ˆ๋‹ค.
  • ํ›„๋ณด๋“ค์ด ๋ชจ์—ฌ์žˆ๋Š” ๊ณต๊ฐ„์„ Gallery๋กœ class๋ฅผ ๋‘๊ณ , ์—ฌ๊ธฐ์—์„œ ํ›„๋ณด๋“ค์„ ์ถ”๊ฐ€ํ•˜๊ณ .. ํ‡ด์ถœํ•˜๋Š” ๋“ฑ ๊ด€๋ฆฌ๋ฅผ ํ•ด์คฌ์Šต๋‹ˆ๋‹ค.
  • ์ฝ”๋“œ ๊ตฌ์กฐ์ž์ฒด๋Š” class์™€ struct๋งŒ ๋ณด์‹œ๋ฉด ์‚ฌ์‹ค ๊ทธ๋ƒฅ append ํ•œ๊ฐœ๋ผ์„œ ์ดํ•ดํ•˜๊ธฐ ์‰ฌ์šฐ์‰ด ๊ฒƒ ๊ฐ™์•„์š”

append

  1. ์ด๋ฏธ Gallery์˜ lines์— ์žˆ๋Š” ํ›„๋ณด๋ฉด ์ข‹์•„์š” ์ถ”๊ฐ€
  2. ๊ฝ‰ ์ฐจ๋ฉด ํ‡ด์ถœ
  3. ๊ฝ‰ ์ฐจ์ง€ ์•Š์•˜๋‹ค๋ฉด ๊ฑ ์ถ”๊ฐ€

out

  1. ์ข‹์•„์š” ์ˆ˜ ๋น„๊ต
  2. ๊ทธ ๋‹ค์Œ์œผ๋กœ idx ๋น„๊ต (๊ฒŒ์‹œ๋œ ์‹œ๊ฐ„ ์ˆœ)

answer

  • ๋ฐฑ์ค€์—์„œ ์›ํ•˜๋Š” ํ˜•ํƒœ ์ •๋‹ต ์ถœ๋ ฅ

๋‹น์žฅ ์ƒ๊ฐ๋‚˜๋Š” ๊ฐœ์„ ์ 

์•„๋งˆ ์ง€๊ธˆ idx๋กœ ํ•ด์ฃผ๊ณ  ์žˆ๋Š”๊ฑฐ๋ฅผ idx ์ž…๋ ฅ๋ฐ›์ง€๋ง๊ณ , Gallery์•ˆ์—์„œ ํ™œ๋™ํ•˜๋Š” ๋ณ€์ˆ˜๋กœ appendํ•ด์ค„๋•Œ๋งˆ๋‹ค +=1 ํ•ด์ฃผ๋ฉด ๋” ๊ฐ€์‹œ์„ฑ์žˆ๊ฒŒ ๊ตฌ์„ฑ์ด ๋  ๊ฒƒ ๊ฐ™๋„ค์š”

@alstjr7437
Copy link
Member Author

์˜คํ˜ธ ํ•€ ๋ฆฌ๋ทฐ๋ฅผ ์—„์ฒญ ์ž์„ธํ•˜๊ฒŒ ๊ทธ๋ฆฌ๊ณ  Swift์Šค๋Ÿฝ๊ฒŒ ํ‘ธ์…จ๋„ค์š” ์ž˜๋ดค์Šต๋‹ˆ๋‹ค!!

@alstjr7437 alstjr7437 merged commit bbbfd9f into main May 20, 2025
9 checks passed
@alstjr7437 alstjr7437 deleted the 4-alstjr7437 branch May 23, 2025 06:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants