ํฐ์คํ ๋ฆฌ ๋ทฐ
728x90
๋ฐ์ํ
๋ฌธ์
https://app.codility.com/programmers/lessons/8-leader/dominator/
Dominator coding task - Learn to Code - Codility
Find an index of an array such that its value occurs at more than half of indices in the array.
app.codility.com
๋ฌธ์ ํ์ด
๋ฆฌ์คํธ์ ๊ฐ ๋ค ์ค, ํ๋์ ์๊ฐ ๋ฆฌ์คํธ ๊ธธ์ด์ ์ ๋ฐ ์ด์์ ์ฐจ์งํ๋ฉด dominator๊ฐ ๋๊ณ , ํด๋นํ๋ ์์ ๋ฐฐ์ด๋ด ์ธ๋ฑ์ค ์ค ์๋ฌด ๊ฐ์ด๋ ๋ฐํํ๋ฉด ๋๋ ๋ฌธ์ ์ด๋ค.
์ฝ๋
def solution(A):
cnt = {}
length = len(A)
if not length:
return -1
for i, num in enumerate(A):
if num not in cnt:
cnt[num] = [i]
else:
cnt[num].append(i)
sort = sorted(cnt.values(), key=len)[-1]
return sort[0] if len(sort) > length // 2 else -1
-
๋์ ๋๋ฆฌ๋ฅผ ํตํด ๋ฆฌ์คํธ ๋ด์ ๊ฐ๊ฐ์ ๊ฐ์ ๋ํ ์ธ๋ฑ์ค๋ฅผ ๋ชจ๋ ๋ด๊ณ , ๊ธฐ์กด ๋ฆฌ์คํธ ๊ธธ์ด์ ์ ๋ฐ์ ๋๋์ง ํ์ธํ๋ค.
728x90
๋ฐ์ํ
'๐จโ๐ป ์ฝ๋ฉํ ์คํธ > Codility' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Lesson 9: Maximum Slice Problem โ Max Profit (0) | 2020.06.11 |
---|---|
Lesson 8: Leader โ Equil Leader (0) | 2020.06.10 |
Lesson 7: Stacks and Queues โ Stone Wall (0) | 2020.06.09 |
Lesson 7: Stacks and Queues โ Nesting (0) | 2020.06.09 |
Lesson 7: Stacks and Queues โ Fish (0) | 2020.06.09 |
๋๊ธ
๊ธ ๋ณด๊ดํจ
์ต๊ทผ์ ์ฌ๋ผ์จ ๊ธ
์ต๊ทผ์ ๋ฌ๋ฆฐ ๋๊ธ