1import math 2 3def diff(start, end): 4 sh, sm = map(int, start.split(':')) 5 eh, em = map(int, end.split(':')) 6 7 return (eh*60+em) - (sh*60+sm) 8 9def calc(time, baseTime, baseFee, unitTime, unitFee): 10 if time < baseTime: 11 return baseFee 12 time -= baseTime 13 return baseFee + math.ceil(time/unitTime)*unitFee 14 15def solution(fees, records): 16 cars = {} 17 for i in records: 18 time, car_num, _ = i.split(' ') 19 # μλ‘ μ
μ°¨ ν κ²½μ° 20 if not car_num in cars: 21 cars[car_num] = [0, time] 22 else: 23 # μΆμ°¨ ν λ€μ μ
μ°¨ν κ²½μ° 24 if cars[car_num][1] == '': 25 cars[car_num][1] = time 26 # μΆμ°¨ν κ²½μ° 27 else: 28 cars[car_num][0] += diff(cars[car_num][1], time) 29 cars[car_num][1] = '' 30 31 # μμ§ μΆμ°¨νμ§ μμ μ°¨λλ€μ λν΄ 23:59μ μΆμ°¨ν κ²μΌλ‘ κ°μ£Ό 32 for num in cars: 33 if cars[num][1] !
1from collections import deque 2 3def solution(queue1, queue2): 4 sum1, sum2 = sum(queue1), sum(queue2) 5 dq1 = deque(queue1) 6 dq2 = deque(queue2) 7 cnt = 0 8 9 while cnt <= len(queue1)*2+1 and sum1 != sum2: 10 if sum1 > sum2: 11 tmp = dq1.popleft() 12 dq2.append(tmp) 13 sum1 -= tmp 14 sum2 += tmp 15 elif sum1 < sum2: 16 tmp = dq2.popleft() 17 dq1.append(tmp) 18 sum1 += tmp 19 sum2 -= tmp 20 cnt += 1 21 22 return cnt if sum1 == sum2 else -1 λ¬Έμ κΈΈμ΄κ° κ°μ λ νκ° μ£Όμ΄μ§λ€ λ νμ ν©μ΄ κ°μμ§λλ‘ νμ μμλ₯Ό κ΅νν μ μλ μ΅μ νμλ₯Ό ꡬνλΌ νμ popμ μΌμͺ½μμ, pushλ μ€λ₯Έμͺ½μμ μ΄λ£¨μ΄μ§λ€ TC input queue1 : [3, 2, 7, 2], queue2 : [4 ,6, 5, 1]
1from collections import deque 2 3def solution(want, number, discount): 4 want_dict = dict() 5 answer = 0 6 7 for i in range(len(want)): 8 want_dict[want[i]] = number[i] 9 10 for i in discount[:10]: 11 if i in want_dict: 12 want_dict[i] -= 1 13 14 for i in range(0, len(discount)-9): 15 if all(map(lambda x: x <= 0, want_dict.values())): 16 answer += 1 17 18 if discount[i] in want_dict: 19 want_dict[discount[i]] += 1 20 if i+10 < len(discount) and discount[i+10] in want_dict: 21 want_dict[discount[i+10]] -= 1 22 23 return answer λ¬Έμ XYZλ§νΈμμλ νμμ κ°μ
νλ©΄ 10μΌλμ ν μΈννμ λ°λλ€ ν μΈνλ μ νμ ν루μ νλμ©λ§ ꡬ맀ν μ μλ€ μ νμ΄κ° μνλ μ ν 리μ€νΈ, μνλ μ νμ μλ 리μ€νΈ, λ§νΈμμ ν μΈνλ μ ν 리μ€νΈκ° μ£Όμ΄μ§λ€ μ νμ΄κ° μνλ μ νμ λͺ¨λ ν μΈ λ°μ μ μλ νμ λ±λ‘ λ μ§μ μλ₯Ό ꡬνλΌ TC input want: [“banana”, “apple”, “rice”, “pork”, “pot”]
1def solution(k, tangerine): 2 D = {} 3 for i in tangerine: 4 if i in D: 5 D[i] += 1 6 else: 7 D[i] = 1 8 D = sorted(D.items(), key=lambda x: -x[1]) 9 answer = 0 10 for _, num in D: 11 k -= num 12 answer += 1 13 if k <= 0: 14 break 15 16 return answer λ¬Έμ κ·€μ κ°μ kμ κ·€μ κ°μλ₯Ό λ΄μ λ°°μ΄ tangerineμ΄ μ£Όμ΄μ§λ€ κ·€ kκ°λ₯Ό κ³ λ₯Ό λ, ν¬κΈ°κ° μλ‘ λ€λ₯Έ μ’
λ₯μ μμ μ΅μκ°μ ꡬνλΌ TC input k: 6, tangerine: [1, 3, 2, 5, 4, 5, 2, 3]
1import math 2 3def solution(k, d): 4 answer = 0 5 6 for x in range(0, d+1, k): 7 a = math.floor((d**2-x**2)**0.5) // k + 1 8 answer += a 9 10 return answer λ¬Έμ 2μ°¨μ μ’ν νλ©΄μμ (xk (x=0,1,2,3…), yk (y=0,1,2,3…)) μ§μ μ μ μ μ°λλ€ μμ κ³Όμ κ±°λ¦¬κ° dκ° λμΌλ©΄ μ μ μ°μ§ μλλ€ kμ dκ° μ£Όμ΄μ§ λ, μ μ΄ μ΄ λͺ κ° μ°νλμ§ κ΅¬νλΌ TC input k:2, d:4
ouput 6
1import heapq 2 3def solution(n, k, enemy): 4 heap = [] 5 cnt = 0 6 for i in enemy: 7 n -= i 8 heapq.heappush(heap, -i) 9 while n < 0: 10 k -= 1 11 if not heap or k < 0: 12 return cnt 13 tmp = -heapq.heappop(heap) 14 n += tmp 15 cnt += 1 16 return cnt λ¬Έμ n: κ°μ§κ³ μλ λ³μ¬ μ k: μ¬μ©ν μ μλ 무μ κΆ μ€ν¬ μ enemy: λΌμ΄λλ§λ€ μ‘΄μ¬νλ μ μμ λ°°μ΄ λΌμ΄λλ§λ€ enemy[i]λͺ
λ§νΌ μλͺ¨νμ¬ enemy[i]λ§λ¦¬μ μ μ λ§μ μ μλ€ λ¬΄μ κΆμ μ μ ν μ¬μ©νμ¬ λ²νΈ μ μλ μ΅λ λΌμ΄λ μλ₯Ό ꡬνλΌ TC input n: 7, k: 3, enemy: [4, 2, 4, 5, 3, 3, 1]
1def solution(data, col, row_begin, row_end): 2 answer = 0 3 data.sort(key=lambda x: (x[col-1], -x[0])) 4 s = [] 5 for i in range(row_begin-1, row_end): 6 s.append(sum(map(lambda x: x%(i+1), data[i]))) 7 8 for i in s: 9 answer ^= i 10 return answer λ¬Έμ ν΄μ ν¨μλ col, row_begin, row_endμ μ
λ ₯μΌλ‘ λ°λλ€ ν
μ΄λΈμ ννμ colλ²μ§Έ 컬λΌμ κ°μ κΈ°μ€μΌλ‘ μ€λ¦μ°¨μ μ λ ¬μ νλ, λ§μ½ κ·Έ κ°μ΄ λμΌνλ©΄ κΈ°λ³Έν€μΈ 첫 λ²μ§Έ 컬λΌμ κ°μ κΈ°μ€μΌλ‘ λ΄λ¦Όμ°¨μ μ λ ¬νλ€ μ λ ¬λ λ°μ΄ν°μμ S_iλ₯Ό i λ²μ§Έ νμ ννμ λν΄ κ° μ»¬λΌμ κ°μ i λ‘ λλ λλ¨Έμ§λ€μ ν©μΌλ‘ μ μνλ€ row_begin β€ i β€ row_end μΈ λͺ¨λ S_iλ₯Ό λμ νμ¬ bitwise XOR ν κ°μ ν΄μ κ°μΌλ‘μ λ°ννλΌ TC input data: [[2,2,6],[1,5,10],[4,2,9],[3,8,3]]
1def solution(cap, n, deliveries, pickups): 2 answer = 0 3 tempD = 0 4 tempP = 0 5 for i in range(n-1, -1, -1): 6 tempD += deliveries[i] 7 tempP += pickups[i] 8 9 while tempD > 0 or tempP > 0: 10 tempD -= cap 11 tempP -= cap 12 answer += (i+1)*2 13 return answer λ¬Έμ νΈλμ μ€μ μ μλ μ¬νμ© νλ°° μμμ μ΅λκ°μ cap, λ°°λ¬ν μ§μ κ°μ n νλ°° μμμ κ°μλ₯Ό λ΄μ deliveries, μ¬νμ© νλ°° μμμ κ°μλ₯Ό λ΄μ pickupsκ° μ£Όμ΄μ§λ€ νΈλνλλ‘ λͺ¨λ λ°°λ¬κ³Ό μκ±°λ₯Ό λ§μΉκ³ λμμ¬ μ μλ μ΅μ μ΄λ 거리λ₯Ό ꡬνλΌ TC input (cap, n, deliveries, pickups) 4, 5, [1, 0, 3, 1, 2], [0, 3, 0, 4, 0]