DESCRIPTION:
In this Kata, you will sort elements in an array by decreasing frequency of elements. If two elements have the same frequency, sort them by increasing value.
solve([2,3,5,3,7,9,5,3,7]) = [3,3,3,5,5,7,7,2,9]
--we sort by highest frequency to lowest frequency. If two elements have same frequency, we sort by increasing value
More examples in test cases.
Good luck!
def solve(arr):
ans=[]
adict={}
for i in range(len(arr)):
if arr[i] in adict:
adict[arr[i]]+=1
else:
adict[arr[i]]=1
max=len(arr)
alist=sorted(adict)
while len(alist)>0:
maxn=alist[0]
maxc=adict[alist[0]]
for i in range(len(alist)):
if adict[alist[i]]>maxc:
maxn=alist[i]
maxc=adict[alist[i]]
for i in range(maxc):
ans.append(maxn)
alist.remove(maxn)
return ans
'알고리즘 문제 > CodeWar' 카테고리의 다른 글
Sequences and Series (0) | 2022.11.14 |
---|---|
Remove the Parentheses (0) | 2022.11.10 |
Binary to Text (ASCII) Conversion (0) | 2022.11.09 |
Square Matrix Multiplication (0) | 2022.11.08 |
Twisted Sum (0) | 2022.11.08 |