공대생의 비망록

[프로그래머스][Lv. 1] 신고 결과 받기 Swift 풀이 본문

Programming Language/Swift

[프로그래머스][Lv. 1] 신고 결과 받기 Swift 풀이

myungsup1250 2022. 3. 14. 15:20

https://programmers.co.kr/learn/courses/30/lessons/92334

 

코딩테스트 연습 - 신고 결과 받기

문제 설명 신입사원 무지는 게시판 불량 이용자를 신고하고 처리 결과를 메일로 발송하는 시스템을 개발하려 합니다. 무지가 개발하려는 시스템은 다음과 같습니다. 각 유저는 한 번에 한 명의

programmers.co.kr

풀이는 추후에 차차 올리도록 하겠습니다...

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import Foundation
 
func solution(_ id_list:[String], _ report:[String], _ k:Int-> [Int] {
    var user_dict: [String : Int= [String : Int]()
    for id in id_list { 
        user_dict.updateValue(0, forKey: id)
    }
 
    let report_proc = Array(Set(report))    
    for rpt in report_proc {
        let slice: [String= rpt.components(separatedBy: " ")
        var count: Int = user_dict[slice[1]]!
        user_dict.updateValue(count + 1, forKey: slice[1])
    }
    
    var report_count: [Int= Array(repeating: 0, count: user_dict.count)
    for i in 0..<id_list.count {
        if !(k > user_dict[id_list[i]]!) {
            report_count[i] += 1
        }
    }
    
    var result_count: [Int= Array(repeating: 0, count: user_dict.count)
    for rpt in report_proc {
        let slice: [String= rpt.components(separatedBy: " ")
        if !(k > user_dict[slice[1]]!) { // slice[1]: count
            result_count[id_list.index(of: slice[0])!+= 1 // slice[0]: reporter 
        }
    }
 
    return result_count
}
cs
Comments