๐ฌย ๋ฌธ์
https://app.codility.com/programmers/trainings/4/first_unique/
๐ฌย Idea
- ์์ ํ์์ ํตํด Dictionary์ ํด๋น key๊ฐ์ด ์ฒ์ ๋ํ๋ฌ์ ๋์ index์ key๊ฐ์ด ๋ช๋ฒ ๋ํ๋ฌ๋์ง๋ฅผ ์ ์ฅํ๋ค.
- ์ดํ ๊ฐ์ด ๊ณ ์ ํ์ง ์์ ๊ฒ๋ค์ ๋ชจ๋ ํํฐ๋งํ ๋ค index๋ฅผ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌํ์ฌ ์ฒซ key๋ฅผ ๋ฆฌํดํ๋ค. ์ด ๋ ๋น ๋ฐฐ์ด์ด๋ผ๋ฉด -1์ ๋ฆฌํดํ๋ค.
๐ฌย ํ์ด
publicfunc solution(_ A :inout[Int])->Int{vardict:[Int:(Int,Int)]=[:]ifA.count ==1{returnA.first! }ifA.count ==0{return-1}for(index, i)inA.enumerated(){ifdict[i]==nil{dict[i]=(index,1)}else{dict[i]!.1+=1}}
dict = dict.filter({ $0.value.1==1})return dict.sorted(by:{ $0.value.0< $1.value.0}).first?.key ??-1}์์์๊ฐ : 8๋ถ
์๊ฐ ๋ณต์ก๋ : O(N * log(N))
ํ๊ฐํ : https://app.codility.com/demo/results/trainingV9V5FJ-6NK/
๐ฌย ๋ฌธ์ https://app.codility.com/programmers/trainings/4/first_unique/
๐ฌย Idea๐ฌย ํ์ด์์์๊ฐ: 8๋ถ์๊ฐ ๋ณต์ก๋: O(N * log(N))ํ๊ฐํ: https://app.codility.com/demo/results/trainingV9V5FJ-6NK/