본문 바로가기

개발/알고리즘35

[codility]FrogRiverOne app.codility.com/programmers/lessons/4-counting_elements/frog_river_one/ FrogRiverOne coding task - Learn to Code - Codility Find the earliest time when a frog can jump to the other side of a river. app.codility.com 오오 갓갓 코딜리티 테스트 케이스를 미리 생각하고 푸니 덜 틀리게 되었다. A의 길이가 X보다 작으면 아무리해도 강을 못건너서 -1을 리턴하고 처음 나오는 수는 d에 저장하고 그 개수를 센다 개수가 X와 같아지면 A의 index를 리턴함 def solution(X, A): # write your code in Python 3.. 2021. 2. 17.
[codility] TapeEquilibrium app.codility.com/programmers/lessons/3-time_complexity/tape_equilibrium/ TapeEquilibrium coding task - Learn to Code - Codility Minimize the value |(A[0] + ... + A[P-1]) - (A[P] + ... + A[N-1])|. app.codility.com 오.. O(n*n)이라 실패 def solution(A): # write your code in Python 3.6 min_sum = max(A) for i in range(1,len(A)): sum_p_head = sum(A[:i]) sum_p_tail = sum(A[i:]) min_sum = min(min_sum, sum_p_.. 2021. 2. 16.
[codility]PermMissingElem app.codility.com/programmers/lessons/3-time_complexity/perm_missing_elem/ PermMissingElem coding task - Learn to Code - Codility Find the missing element in a given permutation. app.codility.com 정렬해서 맨 첫번째로 index+1과 value가 같지 않은 곳이 빠진 수 n=0일 때 생각 못해서 50%뜸ㅠ 아... 맨 마지막 것 체크 안했네 for문 돌릴 때 맨 마지막 원소를 체크했는지도 확인해야겠다 def solution(A): # write your code in Python 3.6 if len(A) == 0: return 1 A=sorted(A) #.. 2021. 2. 16.
[codility] FrogJmp app.codility.com/programmers/lessons/3-time_complexity/frog_jmp/ FrogJmp coding task - Learn to Code - Codility Count minimal number of jumps from position X to Y. app.codility.com 나머지가 0보다 크면 1을 더해줌 def solution(X, Y, D): # write your code in Python 3.6 q = (Y-X)//D r = (Y-X)%D return q if r == 0 else q+1 2021. 2. 16.
[codility] OddOccurrencesInArray app.codility.com/programmers/lessons/2-arrays/odd_occurrences_in_array/ OddOccurrencesInArray coding task - Learn to Code - Codility Find value that occurs in odd number of elements. app.codility.com dict로 홀수의 개수를 세어서 홀수면 그게 정답 def solution(A): # write your code in Python 3.6 d = {} for i in A: if i not in d.keys(): d[i] = 1 else: d[i] += 1 for i in d.keys(): if d[i] % 2 == 1 : return i 2021. 2. 16.
[codility] CyclicRotation app.codility.com/programmers/lessons/2-arrays/cyclic_rotation/ CyclicRotation coding task - Learn to Code - Codility Rotate an array to the right by a given number of steps. app.codility.com 배열 돌리기다 def solution(A, K): # write your code in Python 3.6 n = len(A) if n == 0: return A k = K % n if k == 0: return A return A[n-k:] + A[:n-k] 2021. 2. 16.