💬 문제
[코딩테스트 연습 - 개인정보 수집 유효기간](https://school.programmers.co.kr/learn/courses/30/lessons/150370)
💬 풀이
func solution(_ today:String, _ terms:[String], _ privacies:[String])->[Int]{vartermDict:[String:Int]=[:]lettoday= today.components(separatedBy:".")varresult:[Int]=[]fortermin terms {lett= term.components(separatedBy:.whitespaces)termDict[t[0]]=Int(t[1])}for(index, privacy)in privacies.enumerated(){letp= privacy.components(separatedBy:.whitespaces)[1]vard= privacy.components(separatedBy:.whitespaces)[0].components(separatedBy:".").compactMap{Int(String($0))}d[0]+=termDict[p]! /12d[1]+=termDict[p]! %12d[2]-=1ifd[1]>12{d[1]-=12d[0]+=1}ifd[2]==0{d[1]-=1d[1]=d[1]==0?12:d[1]d[0]-=d[1]==0?1:0d[2]=28}vars= d.map({String($0)})s[1]=String(format:"%02d", arguments:[d[1]])s[2]=String(format:"%02d", arguments:[d[2]])if today.joined()> s.joined(){
result.append(index +1)}}return result
}💬 더 나은 방법?
func solution(_ today:String, _ terms:[String], _ privacies:[String])->[Int]{varanswer=[Int]()vartermDict=[String: Int]()letdate=getDate(today)forsin terms {letterm= s.components(separatedBy:.whitespaces)termDict[term[0]]=Int(term[1])}foriin privacies.indices {letprivacy=privacies[i].components(separatedBy:.whitespaces)ifgetDate(privacy[0])+(termDict[privacy[1]]??0)*28<= date {
answer.append(i +1)}}return answer
}privatefunc getDate(_ today:String)->Int{letdate= today.split(separator:".")letyear=Int(date[0])??0letmonth=Int(date[1])??0letday=Int(date[2])??0return(year *12*28)+(month *28)+ day
}💬 알게된 문법
✅ indices
💬 문제[코딩테스트 연습 - 개인정보 수집 유효기간](https://school.programmers.co.kr/learn/courses/30/lessons/150370)
💬 풀이💬 더 나은 방법?💬 알게된 문법✅ indices