Problem Solving/Codility27 [Python] 4.3. MissingInteger https://app.codility.com/programmers/lessons/4-counting_elements/ 4. Counting Elements lesson - Learn to Code - Codility Calculate the values of counters after applying all alternating operations: increase counter by 1; set value of all counters to current maximum. app.codility.com 정답률: 100% (Task Score 100%, Correctness 100%, Performance 100%) 시간복잡도: O(N) or O(N * log(N)) def solution(A): _A = .. 2021. 3. 15. [Python] 4.2. MaxCounters https://app.codility.com/programmers/lessons/4-counting_elements/ 4. Counting Elements lesson - Learn to Code - Codility Calculate the values of counters after applying all alternating operations: increase counter by 1; set value of all counters to current maximum. app.codility.com 정답률: 88% 시간복잡도:O(N + M) def solution(N, A): cnt = [0] * N maxcnt = 0 for K in range(len(A)): if A[K]-1 < N: cnt[A[K.. 2021. 3. 14. [Python] 4.1. FrogRiverOne https://app.codility.com/programmers/lessons/4-counting_elements/ 4. Counting Elements lesson - Learn to Code - Codility Calculate the values of counters after applying all alternating operations: increase counter by 1; set value of all counters to current maximum. app.codility.com 정답률: 100% 시간복잡도: O(N) def solution(X, A): check = [1] + [0] * X cnt = 0 for K in range(len(A)): if check[A[K]] == 0.. 2021. 3. 13. [Python] 3.3. TapeEquilibrium https://app.codility.com/programmers/lessons/3-time_complexity/ 3. Time Complexity lesson - Learn to Code - Codility Count minimal number of jumps from position X to Y. app.codility.com 정답률: 100% 시간복잡도: O(N) def solution(A): lst = [] total = sum(A) left = 0 for P in range(len(A)-1): left += A[P] right = total - left diff = abs(right-left) lst.append(diff) return min(lst) 2021. 3. 11. [Python] 3.2. PermMissingElem https://app.codility.com/programmers/lessons/3-time_complexity/ 3. Time Complexity lesson - Learn to Code - Codility Count minimal number of jumps from position X to Y. app.codility.com 정답률: 100% 시간 복잡도: O(N) or O(N * log(N)) def solution(A): N = len(A) total = int((N+1)*(N+2)/2) answer = total - sum(A) return answer 2021. 3. 11. [Python] 3.1. FrogJmp https://app.codility.com/programmers/lessons/3-time_complexity/ 3. Time Complexity lesson - Learn to Code - Codility Count minimal number of jumps from position X to Y. app.codility.com 나의 답안 - 정답률 100%, 시간복잡도: O(1) def solution(X, Y, D): if X == Y: return 0 answer = qoutient = (Y-X)//D remainder = (Y-X)%D if remainder != 0: answer = qoutient+1 return answer 2021. 3. 7. [Python] 2.2. OddOccurrencesInArray https://app.codility.com/programmers/lessons/2-arrays/ 2. Arrays lesson - Learn to Code - Codility Rotate an array to the right by a given number of steps. app.codility.com 나의 답안 - [66%] 정답 def solution(A): dic = dict() for v in A: if v not in dic.keys(): dic[v] = 1 else: dic[v] += 1 answer = min(dic.items(), key=lambda k:k[1]) return answer[0] 타인의 모범답안 - [100%] 정답 비트연산을 통해서 element가 짝수인지 홀수인지 판.. 2021. 3. 7. [Python] 2.1. CyclicRotation https://app.codility.com/programmers/lessons/2-arrays/ 2. Arrays lesson - 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 len(A) == 0: return A rotate = K % len(A) answer = [] for idx in range(len(A)): nex.. 2021. 3. 6. [Python] 1.1. BinaryGap https://app.codility.com/programmers/lessons/1-iterations/ 1. Iterations lesson - Learn to Code - Codility Find longest sequence of zeros in binary representation of an integer. app.codility.com 자연수를 이진수로 바꾼 후 1의 인덱스를 저장한다. 이후 인덱스간 차이를 계산해서 가장 큰 값을 최대값으로 업데이트해서 답을 구한다. def solution(N): bi = str(bin(N)) bi = bi[2:] max = 0 lst = [] for idx in range(len(bi)): if bi[idx] == "1": lst.append(idx) for.. 2021. 3. 6. 이전 1 2 3 다음 728x90 반응형