-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
361 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
def solution(s): | ||
counts = [0] * 26 | ||
for c in s: | ||
counts[ord(c) - ord("a")] += 1 | ||
sorted_str = "" | ||
for i in range(26): | ||
sorted_str += chr(i + ord("a")) * counts[i] | ||
|
||
return sorted_str | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
def solution(arr1, arr2): | ||
merged = [] | ||
i, j = 0, 0 | ||
while i < len(arr1) and j < len(arr2): | ||
if arr1[i] <= arr2[j]: | ||
merged.append(arr1[i]) | ||
i += 1 | ||
else: | ||
merged.append(arr2[j]) | ||
j += 1 | ||
|
||
while i < len(arr1): | ||
merged.append(arr1[i]) | ||
i += 1 | ||
while j < len(arr2): | ||
merged.append(arr2[j]) | ||
j += 1 | ||
|
||
return merged | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
def solution(strings, n): | ||
return sorted(strings, key=lambda x: (x[n], x)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
def solution(n): | ||
digits = list(str(n)) | ||
digits.sort(reverse=True) | ||
answer = int("".join(digits)) | ||
return answer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
def solution(array, commands): | ||
answer = [] | ||
for cmd in commands: | ||
i, j, k = cmd | ||
sliced_arr = array[i - 1 : j] | ||
sorted_arr = sorted(sliced_arr) | ||
answer.append(sorted_arr[k - 1]) | ||
return answer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import functools | ||
def compare(a,b): | ||
tl = str(a) + str(b) | ||
t2 = str(b) + str(a) | ||
return (int(tl) > int(t2)) - (int(tl) < int(t2)) | ||
def solution(numbers): | ||
sorted_nums = sorted( | ||
numbers, key=functools.cmp_to_key(lambda a, b: compare(a, b)), reverse=True | ||
) | ||
answer = "".join(str(x) for x in sorted_nums) | ||
return "0" if int(answer) == 0 else answer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
def solution(s): | ||
s = s[2:-2].split("}, {") | ||
s = sorted(s, key=len) | ||
answer = [] | ||
for element in s: | ||
numbers = element.split(",") | ||
for number in numbers: | ||
if int(number) not in answer: | ||
answer.append(int(number)) | ||
return answer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from heapq import heappush, heappop | ||
def solution(land, height): | ||
answer = 0 | ||
n = len(land) | ||
di = [-1, 0, 1, 0] | ||
dj = [0, 1, 0, -1] | ||
visited = [[False] * n for _ in range(n)] | ||
q = [] | ||
heappush(q,[0,0,0]) | ||
while q: | ||
cost,i,j=heappop(q) | ||
if not visited[i][j]: | ||
visited[i][j]=True | ||
answer+=cost | ||
for d in range(4): | ||
ni,nj=i+di[d],j+dj[d] | ||
if 0<=ni<n and 0<= nj <n: | ||
temp_cost=abs(land[i][j]-land[ni][nj]) | ||
if temp_cost>height: | ||
new_cost=temp_cost | ||
else: | ||
new_cost=0 | ||
heappush(q,[new_cost,ni,nj]) | ||
return answer | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
def solution(arr, n): | ||
def rotate_90(arr): | ||
n = len(arr) | ||
rotated_arr = [[0] * n for _ in range(n)] | ||
for i in range(n): | ||
for j in range(n): | ||
rotated_arr[j][n - i - 1] = arr[i][j] | ||
return rotated_arr | ||
rotated_arr = arr.copy() | ||
|
||
|
||
for _ in range(n): | ||
rotated_arr = rotate_90(rotated_arr) | ||
|
||
return rotated_arr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
def multiply_matrices(matrix1, matrix2): | ||
result = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] | ||
for i in range(3): | ||
for j in range(3): | ||
for k in range(3): | ||
result[i][j] += matrix1[i][k] * matrix2[k][j] | ||
return result | ||
def transpose_matrix(matrix): | ||
result = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] | ||
for i in range(3): | ||
for j in range(3): | ||
result[j][i] = matrix[i][j] | ||
return result | ||
def solution(matrix1, matrix2): | ||
multiplied = multiply_matrices(matrix1, matrix2) | ||
transposed = transpose_matrix(multiplied) | ||
return transposed |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
def solution(n): | ||
snail_array = [[0] * n for _ in range(n)] | ||
|
||
num = 1 | ||
start_row, end_row = 0, n - 1 | ||
start_col, end_col = 0, n - 1 | ||
|
||
while start_row <= end_row and start_col <= end_col: | ||
for i in range(start_col, end_col + 1): | ||
snail_array[start_row][i] = num | ||
num += 1 | ||
start_row += 1 | ||
for i in range(start_row, end_row + 1): | ||
snail_array[i][end_col] = num | ||
num += 1 | ||
end_col -= 1 | ||
if start_row <= end_row: | ||
for i in range(end_col, start_col - 1, -1): | ||
snail_array[end_row][i] = num | ||
num += 1 | ||
end_row -= 1 | ||
if start_col <= end_col: | ||
for i in range(end_row, start_row - 1, -1): | ||
snail_array[i][start_col] = num | ||
num += 1 | ||
start_col += 1 | ||
return snail_array |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
def solution(s): | ||
count_transform = 0 | ||
count_zero = 0 | ||
while s != "1": | ||
count_transform += 1 | ||
count_zero += s.count("0") | ||
s = bin(s.count("l"))[2:] | ||
return [count_transform, count_zero] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from collections import Counter | ||
def solution(topping): | ||
cnt = 0 | ||
topping_counter = Counter(topping) | ||
half_topping_set = set() | ||
for t in topping: | ||
half_topping_set.add(t) | ||
topping_counter[t] -= 1 | ||
if topping_counter[t] == 0: | ||
topping_counter.pop(t) | ||
if len(half_topping_set)==len(topping_counter): | ||
cnt+=1 | ||
return cnt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
def solution(blue,white): | ||
total=blue+white | ||
for vertical in range(3,int(total**0.5)+1): | ||
if total% vertical==0: | ||
horizontal=(int)(total/vertical) | ||
if blue==(horizontal+vertical-2)*2: | ||
return [horizontal,vertical] | ||
return[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
def solution(N): | ||
return bin(N).count('1') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
def solution(keyinput, board): | ||
x , y = 0, 0 | ||
moves = {"up": [0, 1], "down": [0, -1], "left": [-1, 0], "right": [1, 0]} | ||
width, height = board[0] // 2, board[1] // 2 | ||
def is_in_bounds(x, y, dx, dy): | ||
return -width <= x + dx <= width and -height <= y + dy <= height | ||
for key in keyinput: | ||
dx, dy = moves[key] | ||
if is_in_bounds(x, y, dx, dy): | ||
x+=dx | ||
y+=dy | ||
return [x,y] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
def solution(str1, str2): | ||
m = len(str1) | ||
n = len(str2) | ||
dp = [[0] * (n + 1) for _ in range(m + 1)] | ||
for i in range(1, m + 1): | ||
for j in range(1, n + 1): | ||
if str1[i - 1] == str2[j - 1]: | ||
dp[i][j] = dp[i - 1][j - 1] + 1 | ||
else: | ||
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) | ||
return dp[m][n] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
def solution(nums): | ||
n = len(nums) | ||
dp = [1] * n | ||
|
||
for i in range(1, n): | ||
for j in range(i): | ||
if nums[i] > nums[j]: | ||
dp[i] = max(dp[i], dp[j] + 1) | ||
return max(dp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
def solution(arr): | ||
n = len(arr[0]) | ||
dp = [[0] * n for _ in range(4)] | ||
|
||
dp[0][0] = arr[0][0] | ||
dp[1][0] = arr[1][0] | ||
dp[2][0] = arr[2][0] | ||
dp[3][0] = arr[0][0] + arr[2][0] | ||
|
||
for i in range(1, n): | ||
dp[0][i] = arr[0][i] + max(dp[1][i - 1], dp[2][i - 1]) | ||
dp[1][i] = arr[1][i] + max(dp[0][i - 1], dp[2][i - 1], dp[3][i - 1]) | ||
dp[2][i] = arr[2][i] + max(dp[0][i - 1], dp[1][i - 1]) | ||
dp[3][i] = arr[0][i] + arr[2][i] + dp[1][i - 1] | ||
|
||
return max(dp[0][-1], dp[1][-1], dp[2][-1], dp[3][-1]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
def solution(n): | ||
fib=[0,1] | ||
for i in range(2, n + 1): | ||
fib.append((fib[i - 1] + fib[i - 2])) | ||
return fib[n] % 1234567 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
def solution(n): | ||
if n == 1: | ||
return 1 | ||
if n==2: | ||
return 2 | ||
dp=[0]*(n+1) | ||
dp[1]=1 | ||
dp[2]=2 | ||
for i in range(3,n+1): | ||
dp[i]=(dp[i-1]+dp[i-2])% 1000000007 | ||
return dp[n] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
def solution(triangle): | ||
n = len(triangle) | ||
dp = [[0] * n for _ in range(n)] | ||
for i in range(n): | ||
dp[n - 1][i] = triangle[n - 1][i] | ||
for i in range(n - 2, -1, -1): | ||
for j in range(i + 1): | ||
dp[i][j] = max(dp[i + 1][j], dp[i + 1][j + 1]) + triangle[i][j] | ||
return dp[0][0] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
def solution(land): | ||
for i in range(1, len(land)): | ||
for j in range(4): | ||
land[i][j] += max(land[i - 1][:j] + land[i - 1][j + 1 :]) | ||
return max(land[-1]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
def solution(money): | ||
n=len(money) | ||
dp1 = [0] * n | ||
dp2 = [0] * n | ||
dp1[0] = money[0] | ||
dp1[1] = money[0] | ||
for i in range(2, n - 1): | ||
dp1[i] = max(dp1[i - 1], dp1[i - 2] + money[i]) | ||
dp2[1] = money[1] | ||
for i in range(2, n): | ||
dp2[i] = max(dp2[i - 1], dp2[i - 2] + money[i]) | ||
answer = max(dp1[-2], dp2[-1]) | ||
return answer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
def solution(board): | ||
row,col=len(board),len(board[0]) | ||
for i in range(1,row): | ||
for j in range(1,col): | ||
if board[i][j]==1: | ||
up,left,ul=( | ||
board[i-1][j], | ||
board[i][j-1]. | ||
board[i-1][j-1] | ||
) | ||
board[i][j]=min(up,left,ul)+1 | ||
a=max(max(x) for x in board) | ||
return a**2 |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
def solution(amount): | ||
denominations = [1, 10, 50, 100] | ||
denominations.sort(reverse=True) | ||
change = [] | ||
for coin in denominations: | ||
while amount >= coin: | ||
change.append(coin) | ||
amount -= coin | ||
return change |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
def calculate_unit_value(items): | ||
for item in items: | ||
item.append(item[1] / item[0]) | ||
return items | ||
def sort_by_unit_value(items): | ||
items.sort(key=lambda x: x[2], reverse=True) | ||
return items | ||
def sack(items, lim): | ||
total_value = 0 | ||
remaining = lim | ||
for item in items: | ||
if item[0] <= remaining: | ||
total_value += item[1] | ||
remaining -= item[0] | ||
else: | ||
fraction = remaining / item[0] | ||
total_value += item[1] * fraction | ||
break | ||
return total_value | ||
def solution(items,lim): | ||
items = calculate_unit_value(items) | ||
items = sort_by_unit_value(items) | ||
return sack(items, lim) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
def solution(d, budget): | ||
d.sort() | ||
count = 0 | ||
for amount in d: | ||
if budget < amount: | ||
break | ||
budget -= amount | ||
count += 1 | ||
return count if budget >= 0 else count - 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
def solution(people, limit): | ||
people.sort() | ||
count = 0 | ||
i = 0 | ||
j = len(people) - 1 | ||
while i <= j: | ||
if people[j] + people[i] <= limit: | ||
i += 1 | ||
j -= 1 | ||
count+=1 | ||
return count |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from collections import Counter | ||
def solution(k, tangerine): | ||
counter = Counter(tangerine) | ||
sorted_counts = sorted(counter.values(), reverse=True) | ||
num = 0 | ||
sum = 0 | ||
for count in sorted_counts: | ||
sum += count | ||
num_types += 1 | ||
if sum >= k: | ||
break | ||
return num |
Oops, something went wrong.