분류 전체보기 (204) 썸네일형 리스트형 코딜리티 lesson 17 (Dynamic Programming) NumberSolitaire NumberSolitaire [최초풀이] # you can write to stdout for debugging purposes, e.g. # print("this is a debug message") def solution(A): if len(A) == 2: return sum(A) sumA = A[0] + A[-1] for i in range(1, len(A)-1): if A[i] < 0: continue else: sumA += A[i] return sumA 코딜리티 lesson16 (greedy Algorithm), MaxNonoverlappingSegments, TieRopes MaxNonoverlappingSegments def solution(A, B): N = len(A) if N == 0: return 0 if N == 1: return 1 end_point = B[0] cnt = 1 for i in range(1, N): if end_point = K: line = 0 cnt += 1 return.. 코딜리티 lesson15 (Caterpillar method), AbsDistinct, CountDistinctSlices AbsDistinct Compute number of distinct absolute values of sorted array elements. [최초풀이] # you can write to stdout for debugging purposes, e.g. # print("this is a debug message") def solution(A): res = set() for a in A: res.add(abs(a)) return len(res) CountDistinctSlices Count the number of distinct slices (containing only unique numbers). 코딜리티 lesson14, 이진탐색, MinMaxDivision(?), NailingPlanks(?) MinMaxDivision Divide array A into K blocks and minimize the largest sum of any block. 코딜리티 lesson13 (피보나치), FibFrog, Ladder FibFrog [참고풀이] # you can write to stdout for debugging purposes, e.g. # print("this is a debug message") def solution(A): target = len(A) if target == 0: return 1 fibo = [0,1] preNum1 = fibo[-1] preNum0 = fibo[-2] while fibo[-1] 코딜리티 lesson12 유클리디안 알고리즘(GCD: 최대공약수), ChocolatesByNumbers(?), CommonPrimeDivisors(?) 유클리디안 알고리즘 A = Q*B + R gcd(A, B) = gcd(B, R) ChocolatesByNumbers [참고풀이] -> 규칙?답?을 알고 보면 인풋과 아웃풋 사이의 관계가 저렇구나 알수 있지만,, 답을 알기전엔 그 상관관계(수식)를 찾기가 어려웠다 # you can write to stdout for debugging purposes, e.g. # print("this is a debug message") from math import gcd def solution(N, M): return N // gcd(N, M) CommonPrimeDivisors [참고풀이] ->문제속에서 상관관계를 알아내는 것이 문제다.. # you can write to stdout for debugging pur.. 코딜리티 lesson11 에라토스테네스의 체(소수구하기) CountNonDivisible(?), CountSemiprimes x [최초코드] # you can write to stdout for debugging purposes, e.g. # print("this is a debug message") def solution(A): ans = [] for i in range(len(A)): cnt = 0 for j in range(len(A)): if i != j: if A[i] % A[j] != 0: cnt += 1 ans.append(cnt) return ans 코딜리티 lesson10 CountFactors, MinPerimeterRectangle, Flags(?), Peaks(?) CountFactors -> 약수 개수 찾기 [참고풀이] # you can write to stdout for debugging purposes, e.g. # print("this is a debug message") def solution(N): cnt = 0 i = 0 while 1: i += 1 n = N % i #나머지 m = N//i #몫 if i > m: return cnt if n == 0: if i == m: cnt +=1 return cnt else: cnt += 2 return cnt MinPerimeterRectangle # you can write to stdout for debugging purposes, e.g. # print("this is a debug message") de.. 코딜리티 lesson9 MaxProfit, MaxSliceSum, MaxDoubleSliceSum(?) MaxProfit [최초풀이] # you can write to stdout for debugging purposes, e.g. # print("this is a debug message") def solution(A): maxP = 0 for i in range(len(A)): for j in range(i+1, len(A)): if maxP < A[j]-A[i]: maxP = A[j]-A[i] return maxP [다른 풀이] # you can write to stdout for debugging purposes, e.g. # print("this is a debug message") def solution(A): if (len(A) == 1 ) or (len(A) == 0): return 0 mi.. 코딜리티 lesson8 Dominator, EquiLeader Dominator [최초풀이] # you can write to stdout for debugging purposes, e.g. # print("this is a debug message") def solution(A): if len(A) == 0: return -1 limitNum = len(A)/2 setA = set(A) ans = [] for x in setA: if A.count(x) > limitNum: ans.append(x) if len(ans) == 0: return -1 for y in ans: for i in range(len(A)): if y == A[i]: return i [다른풀이] # you can write to stdout for debugging purposes, e.g... 이전 1 ··· 12 13 14 15 16 17 18 ··· 21 다음