-
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.
Merge branch 'main' of https://github.com/kdongsu5509/Haedal_Algorith…
- Loading branch information
Showing
76 changed files
with
1,847 additions
and
24 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
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(s): | ||
stack = [ ] | ||
for c in s: | ||
if stack and stack[-1] == c: | ||
stack.pop( ) | ||
else: | ||
stack.append(c) | ||
return int(not stack) | ||
|
||
# print(solution('baabaa')) | ||
# print(solution('cdcd')) |
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(prices): | ||
n = len(prices) | ||
answer = [0] * n | ||
|
||
stack = [0] | ||
for i in range(1, n): | ||
while stack and prices[i] < prices[stack[-1]]: | ||
j = stack.pop( ) | ||
answer[j] = i - j | ||
stack.append(i) | ||
while stack: | ||
j = stack.pop( ) | ||
answer[j] = n - 1 - j | ||
return answer | ||
|
||
# print(solution([1,2,4,2,3])) |
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(board, moves): | ||
lanes = [[ ] for _ in range(len(board[0]))] | ||
|
||
for i in range(len(board) - 1, -1, -1): | ||
for j in range(len(board[0])): | ||
if board[i][j]: | ||
lanes[j].append(board[i][j]) | ||
|
||
bucket = [ ] | ||
|
||
answer = 0 | ||
|
||
for m in moves: | ||
if lanes[m - 1]: | ||
doll = lanes[m - 1].pop( ) | ||
|
||
if bucket and bucket[-1] == doll: | ||
bucket.pop( ) | ||
answer += 2 | ||
else: | ||
bucket.append(doll) | ||
|
||
return answer | ||
|
||
# board = [[0,0,0,0,0],[0,0,1,0,3],[0,2,5,0,1],[4,2,4,4,2],[3,5,1,3,1]] | ||
# moves = [1,5,3,5,1,2,1,4] | ||
# print(solution(board, moves)) |
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,39 @@ | ||
def solution(n, k, cmd): | ||
deleted = [ ] | ||
|
||
up = [i - 1 for i in range(n + 2)] | ||
|
||
down = [i + 1 for i in range(n + 1)] | ||
|
||
k += 1 | ||
|
||
for cmd_i in cmd: | ||
if cmd_i.startswith("C"): | ||
deleted.append(k) | ||
up[down[k]] = up[k] | ||
down[up[k]] = down[k] | ||
k = up[k] if n < down[k] else down[k] | ||
|
||
elif cmd_i.startswith("Z"): | ||
restore = deleted.pop( ) | ||
down[up[restore]] = restore | ||
up[down[restore]] = restore | ||
|
||
else: | ||
action, num = cmd_i.split( ) | ||
if action == "U": | ||
for _ in range(int(num)): | ||
k = up[k] | ||
else: | ||
for _ in range(int(num)): | ||
k = down[k] | ||
|
||
answer = ["O"] * n | ||
for i in deleted: | ||
answer[i - 1] = "X" | ||
return "".join(answer) | ||
|
||
# n = 8 | ||
# k = 2 | ||
# cmd = ['D 2', 'C', 'U 3', 'C', 'D 4', 'C', 'U 2', 'Z', 'Z'] | ||
# print(solution(n,k,cmd)) |
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 deque | ||
|
||
def solution(N, K): | ||
queue = deque(range(1, N+1)) | ||
|
||
while len(queue) > 1: | ||
for _ in range(K-1): | ||
queue.append(queue.popleft()) | ||
queue.popleft() | ||
return queue[0] | ||
|
||
# print(solution(5,2)) |
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,24 @@ | ||
import math | ||
|
||
def solution(progresses, speeds): | ||
answer = [ ] | ||
n = len(progresses) | ||
days_left = [math.ceil((100 - progresses[i]) / speeds[i]) for i in range(n)] | ||
|
||
count = 0 | ||
max_day = days_left[0] | ||
|
||
for i in range(n): | ||
if days_left[i] <= max_day: | ||
count += 1 | ||
else: | ||
answer.append(count) | ||
count = 1 | ||
max_day = days_left[i] | ||
|
||
answer.append(count) | ||
return answer | ||
|
||
# progresses = [93,30,55] | ||
# speeds = [1,30,5] | ||
# print(solution(progresses, speeds)) |
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 @@ | ||
from collections import deque | ||
|
||
def solution(cards1, cards2, goal): | ||
cards1 = deque(cards1) | ||
cards2 = deque(cards2) | ||
goal = deque(goal) | ||
|
||
while goal: | ||
if cards1 and cards1[0] == goal[0]: | ||
cards1.popleft() | ||
goal.popleft() | ||
elif cards2 and cards2[0] == goal[0]: | ||
cards2.popleft() | ||
goal.popleft() | ||
else: | ||
break | ||
|
||
return "Yes" if not goal else "No" | ||
|
||
# cards1 = ['i', 'drink', 'water'] | ||
# cards2 = ['want', 'to'] | ||
# goal = ['i', 'want', 'to', 'drink', 'water'] | ||
# print(solution(cards1, cards2, goal)) |
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,24 @@ | ||
def count_sort(arr, k): | ||
hashtable = [0] * (k + 1) | ||
|
||
for num in arr: | ||
if num <= k: | ||
hashtable[num] = 1 | ||
return hashtable | ||
|
||
def solution(arr, target): | ||
hashtable = count_sort(arr, target) | ||
|
||
for num in arr: | ||
complement = target - num | ||
if ( | ||
complement != num | ||
and complement >= 0 | ||
and complement <= target | ||
and hashtable[complement] == 1 | ||
): | ||
return True | ||
return False | ||
|
||
# print(solution([1, 2, 3, 4, 8], 6)) | ||
# print(solution([2, 3, 5, 9], 10)) |
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,21 @@ | ||
def polynomial_hash(str): | ||
p = 31 | ||
m = 1_000_000_007 | ||
hash_value = 0 | ||
for char in str: | ||
hash_value = (hash_value * p + ord(char)) % m | ||
return hash_value | ||
|
||
def solution(string_list, query_list): | ||
hash_list = [polynomial_hash(str) for str in string_list] | ||
|
||
result = [ ] | ||
for query in query_list: | ||
query_hash = polynomial_hash(query) | ||
if query_hash in hash_list: | ||
result.append(True) | ||
else: | ||
result.append(False) | ||
return result | ||
|
||
# print(solution(["apple", "banana", "cherry"], ["banana", "kiwi", "melon", "apple"] )) |
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,19 @@ | ||
def solution(participant, completion): | ||
dic = { } | ||
|
||
for p in participant: | ||
if p in dic: | ||
dic[p] += 1 | ||
else: | ||
dic[p] = 1 | ||
|
||
for c in completion: | ||
dic[c] -= 1 | ||
|
||
for key in dic.keys( ) : | ||
if dic[key] > 0: | ||
return key | ||
|
||
# participant = ['leo', 'kiki', 'eden'] | ||
# completion = ['eden', 'kiki'] | ||
# print(solution(participant, completion)) |
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,31 @@ | ||
def solution(s): | ||
answer = 0 | ||
n = len(s) | ||
for i in range(n): | ||
stack = [ ] | ||
for j in range(n): | ||
c = s[(i + j) % n] | ||
if c == "(" or c == "[" or c == "{": | ||
stack.append(c) | ||
else: | ||
if not stack: | ||
break | ||
|
||
if c == ")" and stack[-1] == "(": | ||
stack.pop( ) | ||
elif c == "]" and stack[-1] == "[": | ||
stack.pop( ) | ||
elif c == "}" and stack[-1] == "{": | ||
stack.pop( ) | ||
else: | ||
break | ||
else: | ||
if not stack: | ||
answer += 1 | ||
return answer | ||
|
||
|
||
# print(solution('[](){}')) | ||
# print(solution('}]()[{')) | ||
# print(solution('[)(]')) | ||
# print(solution('}}}')) |
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
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(arr1, arr2): | ||
r1, c1 = len(arr1), len(arr1[0]) | ||
r2, c2 = len(arr2), len(arr2[0]) | ||
|
||
ret = [[0] * c2 for _ in range(r1)] | ||
|
||
for i in range(r1): | ||
for j in range(c2): | ||
for k in range(c1): | ||
ret[i][j] += arr1[i][k] * arr2[k][j] | ||
return ret | ||
|
||
# arr1 = [[1,4],[3,2],[4,1]] | ||
# arr2 = [[3,3],[3,3]] | ||
# print(solution(arr1, arr2)) |
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,22 @@ | ||
def solution(N, stages): | ||
challenger = [0] * (N + 2) | ||
for stage in stages: | ||
challenger[stage] += 1 | ||
|
||
fails = { } | ||
total = len(stages) | ||
|
||
for i in range(1, N + 1): | ||
if challenger[i] == 0 : | ||
fails[i] = 0 | ||
else: | ||
fails[i] = challenger[i] / total | ||
total -= challenger[i] | ||
|
||
result = sorted(fails, key=lambda x : fails[x], reverse=True) | ||
|
||
return result | ||
|
||
N = 5 | ||
stages = [2,1,2,6,2,4,3,3] | ||
print(solution(N, stages)) |
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,28 @@ | ||
def is_valid_move(nx, ny) : | ||
return 0 <= nx < 11 and 0 <= ny < 11 | ||
|
||
def update_location(x, y, dir) : | ||
if dir == 'U': | ||
nx, ny = x, y + 1 | ||
elif dir == 'D': | ||
nx, ny = x, y - 1 | ||
elif dir == 'L': | ||
nx, ny = x - 1, y | ||
elif dir == 'R': | ||
nx, ny = x + 1, y | ||
return nx, ny | ||
|
||
def solution(dirs): | ||
x, y = 5, 5 | ||
ans = set( ) | ||
for dir in dirs : | ||
nx, ny = update_location(x, y, dir) | ||
if not is_valid_move(nx, ny) : | ||
continue | ||
ans.add((x, y, nx, ny)) | ||
ans.add((nx, ny, x, y)) | ||
x, y = nx, ny | ||
return len(ans)/2 | ||
|
||
# dirs = 'ULURRDLLU' | ||
# print(solution(dirs)) |
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,18 @@ | ||
def solution(s): | ||
stack = [ ] | ||
for c in s: | ||
if c == "(": | ||
stack.append(c) | ||
elif c == ")": | ||
if not stack: | ||
return False | ||
else: | ||
stack.pop( ) | ||
if stack: | ||
return False | ||
else: | ||
return True | ||
|
||
|
||
# print(solution('(())()')) | ||
# print(solution('((())()')) |
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(decimal): | ||
stack = [] | ||
while decimal > 0: | ||
remainder = decimal % 2 | ||
stack.append(str(remainder)) | ||
decimal //= 2 | ||
stack.reverse() | ||
return ''.join(stack) | ||
|
||
|
||
# print(solution(10)) | ||
# print(solution(27)) | ||
# print(solution(12345)) |
Oops, something went wrong.