1from collections import defaultdict 2 3def solution(s): 4 count = defaultdict(int) 5 for i in s[1:-1].replace('},{', '} {').split(' '): 6 for j in i[1:-1].split(','): 7 count[j] += 1 8 return [int(i[0]) for i in sorted(count.items(), key=lambda x: x[1], reverse=True)] ๋ฌธ์ ํน์ ํํ์ ํฌํํ๋ ์งํฉ์ด ๋ด๊ธด ๋ฌธ์์ด s๊ฐ ๋งค๊ฐ๋ณ์๋ก ์ฃผ์ด์ง๋ค s๊ฐ ํํํ๋ ํํ์ ๋ฐฐ์ด์ ๋ด์ ๋ฐํํ๋ผ TC input “{{2},{2,1},{2,1,3},{2,1,3,4}}”
ouput [2, 1, 3, 4]
ํด๊ฒฐ๋ฐฉ๋ฒ ๋ฌธ์ ์์ ์ํ๋ ํํ์ ์์๋ ์์์ ๊ฐ์๊ฐ ์์ฃผ ๋ฑ์ฅํ๋ ์์์ด๋ค ๊ฐ ์์์ ๊ฐ์๋ฅผ ์ธ์ด count๋ผ๋ defaultdict์ ๋ฃ๋๋ค count๋ฅผ value ๊ธฐ์ค์ผ๋ก ์ ๋ ฌํ์ฌ key๊ฐ์ list์ ํํ๋ก ๋ฐํํ๋ผ
1answer = [0, 0] 2 3def solution(arr): 4 def recursion(sx, sy, k): 5 global answer 6 origin = arr[sy][sx] 7 cnt = 0 8 for i in range(sx, sx+k): 9 for j in range(sy, sy+k): 10 if origin != arr[j][i]: 11 recursion(sx, sy, k//2) 12 recursion(sx+k//2, sy, k//2) 13 recursion(sx, sy+k//2, k//2) 14 recursion(sx+k//2, sy+k//2, k//2) 15 return 16 answer[origin] += 1 17 18 recursion(0, 0, len(arr)) 19 return answer ๋ฌธ์ 0๊ณผ 1๋ก ์ด๋ฃจ์ด์ง 2^n x 2^n ํฌ๊ธฐ์ 2์ฐจ์ ์ ์ ๋ฐฐ์ด arr์ ์์ถํ๋ ค ํ๋ค ์์ถํ๋ ๋ฐฉ๋ฒ์ ๋น์ ์ด ์์ถํ๊ณ ์ ํ๋ ํน์ ์์ญ์ S๋ผ๊ณ ์ ์ํ๋ค ๋ง์ฝ S ๋ด๋ถ์ ์๋ ๋ชจ๋ ์๊ฐ ๊ฐ์ ๊ฐ์ด๋ผ๋ฉด, S๋ฅผ ํด๋น ์ ํ๋๋ก ์์ถ์ํจ๋ค ๊ทธ๋ ์ง ์๋ค๋ฉด, S๋ฅผ ์ ํํ 4๊ฐ์ ๊ท ์ผํ ์ ์ฌ๊ฐํ ์์ญ์ผ๋ก ์ชผ๊ฐ ๋ค, ๊ฐ ์ ์ฌ๊ฐํ ์์ญ์ ๋ํด ๊ฐ์ ๋ฐฉ์์ ์์ถ์ ์๋ํ๋ค TC input [[1,1,0,0],[1,0,0,0],[1,0,0,1],[1,1,1,1]]
1dx = [1, 0, -1, 0] 2dy = [0, 1, 0, -1] 3 4def solution(rows, columns, queries): 5 table = [[(j*columns)+i+1 for i in range(columns)] for j in range(rows)] 6 answer = [] 7 8 for y1, x1, y2, x2 in queries: 9 min_num = 10000 10 x1 -= 1 11 y1 -= 1 12 x2 -= 1 13 y2 -= 1 14 direction = 0 15 curX, curY = x1, y1 16 postNum = table[y1][x1] 17 while True: 18 curX = curX+dx[direction] 19 curY = curY+dy[direction] 20 21 temp = table[curY][curX] 22 table[curY][curX] = postNum 23 postNum = temp 24 25 min_num = min(min_num ,postNum) 26 27 if ((curX == x2 and curY == y1) or 28 (curX == x2 and curY == y2) or 29 (curX == x1 and curY == y2)): 30 direction += 1 31 32 if curX == x1 and curY == y1: 33 break 34 35 answer.
1def solution(word): 2 answer = 0 3 char = ['A', 'E', 'I', 'O', 'U'] 4 cnt = 0 5 6 def traverse(cur): 7 nonlocal char, cnt, word 8 if cur == word: 9 return cnt 10 11 if len(cur) < 5: 12 for ch in char: 13 cnt += 1 14 if traverse(cur+ch) != None: 15 return cnt 16 return traverse('') ๋ฌธ์ ์ฌ์ ์ A,E,I,O,U๋ง ์ฌ์ฉํ์ฌ ๋ง๋ค ์ ์๋ ๊ธธ์ด 5์ดํ์ ๋ชจ๋ ๋จ์ด๊ฐ ์๋ก๋์ด์๋ค ๋จ์ด ํ๋ word๊ฐ ์ฃผ์ด์ง๋ ์ฌ์ ์์ ๋ช๋ฒ์งธ ๋จ์ด์ธ์ง ๊ตฌํ๋ผ TC input ‘I’
1def calc(a1, b1, c1, a2, b2, c2): 2 if a1*b2-a2*b1 == 0: 3 return (0.1, 0.1) 4 return ((b1*c2-b2*c1)/(a1*b2-a2*b1), (c1*a2-a1*c2)/(a1*b2-a2*b1)) 5 6def solution(line): 7 answer = [] 8 length = len(line) 9 points = [] 10 size = [1e15, -1e15, 1e15,- 1e15] 11 12 for i in range(length): 13 for j in range(i): 14 point = calc(line[i][0],line[i][1], line[i][2], line[j][0], line[j][1], line[j][2]) 15 if point[0]%1 != 0 or point[1]%1 != 0: 16 continue 17 x = int(point[0]) 18 y = int(point[1]) 19 points.
1def solution(n, left, right): 2 answer = [] 3 start = (left//n, left%n) 4 end = (right//n, right%n) 5 6 for i in range(start[0], end[0]+1): 7 line = [i+1]*(i+1) + [i for i in range(i+2, n+1)] 8 answer += line 9 10 return answer[start[1]:right-(start[0]*n)+1] ๋ฌธ์ ์ ์ n, left, right๊ฐ ์ฃผ์ด์ง๋ค n X n ํฌ๊ธฐ์ 2์ฐจ์ ๋ฐฐ์ด์ ๋ง๋ ๋ค i=1,2,3..n์ ๋ํด์, 1ํ 1์ด๋ถํฐ iํ iํ๊น์ง ์ซ์ i๋ก ์ฑ์ด๋ค 1ํ, 2ํ.. nํ์ ๋ชจ๋ ์ด์ด๋ถ์ธ ์๋ก์ด 1์ฐจ์ ๋ฐฐ์ด์ ๋ง๋ ๋ค ์๋ก์ด 1์ฐจ์ ๋ฐฐ์ด์์ left๋ฒ์งธ ์ซ์๋ถํฐ right๋ฒ์งธ ์ซ์๊น์ง๋ฅผ ๋ฐฐ์ด๋ก ๋ฐํํ๋ผ TC input n : 3, left : 2, right : 5
1def solution(k, dungeons): 2 result = 0 3 length = len(dungeons) 4 def dfs(cur, visited, cnt): 5 nonlocal result 6 visit = False 7 for i in range(length): 8 if not visited[i] and cur >= dungeons[i][0]: 9 visit = True 10 visited[i] = True 11 dfs(cur-dungeons[i][1], visited, cnt+1) 12 visited[i] = False 13 14 if not visit: 15 result = max(result, cnt) 16 17 dfs(k, [False]*length, 0) 18 19 return result ๋ฌธ์ ์ ์ ์ ํ์ฌ ํผ๋ก๋ k, ๋์ ๋ณ [“์ต์ ํ์ ํผ๋ก๋”, “์๋ชจ ํผ๋ก๋”]๋ฅผ ๋ด์ 2์ฐจ์ ๋ฐฐ์ด dungeons๊ฐ ์ฃผ์ด์ง๋ค ๋์ ์ ํํํ๊ธฐ ์ํด์๋ ์ ์ ์ ํ์ฌ ๋จ์ ํผ๋ก๋๊ฐ ์ต์ ํ์ ํผ๋ก๋ ์ด์์ด์ด์ผ ํ๋ค ๋์ ์ ํด๋ฆฌ์ดํ๋ฉด “์๋ชจ ํผ๋ก๋"๋งํผ ํผ๋ก๋๊ฐ ์๋ชจ๋๋ค ๋์ ์ ํํํ ์ ์๋ ์ต๋ ๋์ ์๋ฅผ ๊ตฌํ๋ผ TC input k:80, dungeons:[[80,20],[50,40],[30,10]]
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] !