본문 바로가기

Codility

(9)
[Lesson 9][Maximum slice problem] MaxDoubleSliceSum app.codility.com/programmers/lessons/9-maximum_slice_problem/max_double_slice_sum/ MaxDoubleSliceSum coding task - Learn to Code - Codility Find the maximal sum of any double slice. app.codility.com # (i, mid, j) => 구간합의 최대값 찾기 def solution(A): N = len(A) l_best = [0] * N r_best = [0] * N for i in range(1, N - 1): l_best[i] = max(l_best[i - 1] + A[i], 0) for j in range(N - 2, 0, -1): r_best[j] =..
[Lesson 8][Leader] Dominator 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 # 최대 카운트 숫자의 인덱스 찾기 # 주의사항: A = [] 경우 import collections def solution(A): N = len(A) if N == 0: return -1 dic = collections.Counter(A) dominator = dic.most_common(1)[0] rst = [] i..
[Lesson 7][Stacks and Queues] Brackets app.codility.com/programmers/lessons/7-stacks_and_queues/brackets/ Brackets coding task - Learn to Code - Codility Determine whether a given string of parentheses (multiple types) is properly nested. app.codility.com # 괄호 짝이 맞는지 def solution(S): if not S: return 1 st = [] for ch in S: if ch in '{[(': st.append(ch) elif ch == ')': if st and st[-1] == '(': st.pop() else: return 0 elif ch == ']': i..
[Lesson 6][Sorting] Distinct app.codility.com/programmers/lessons/6-sorting/distinct/ Distinct coding task - Learn to Code - Codility Compute number of distinct values in an array. app.codility.com # 중복 제거 def solution(A): return len(set(A)) print(solution([2, 1, 1, 2, 3, 1]))
[Lesson 5][Prefix Sums] CountDiv app.codility.com/programmers/lessons/5-prefix_sums/count_div/ CountDiv coding task - Learn to Code - Codility Compute number of integers divisible by k in range [a..b]. app.codility.com # A~B 사이에 K 배수가 몇개인지 def solution(A, B, K): end_cnt = B // K start_cnt = (A - 1) // K return end_cnt - start_cnt print(solution(7, 11, 2)) # 2
[Lesson 4][Counting Elements] 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 # 1~X 위치가 모두 나타나는 최초 시간 def solution(X, A): S = set(range(1, X + 1)) N = len(A) for i in range(N): if A[i] in S: S.remove(A[i]) if len(S) == 0: return i return -1 print(solution(5, [..
[Lesson 3][Time Complexity] 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 # X -> Y 도달하기 위해 필요한 점프 수 def solution(X, Y, D): if (Y - X) % D == 0: return (Y - X) // D else: return (Y - X) // D + 1 print(solution(10, 85, 30)) # 3
[Lesson 2][Arrays] 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 # K번 배열 회전하기 def solution(A, K): if not A: return A for _ in range(K): A.insert(0, A.pop()) return A print(solution([3, 8, 9, 7, 6], 3)) # [9, 7, 6, 3, 8] print(solution([0, 0, 0], 1)) # [0, 0, 0] print(solu..