Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Weekly Pull Request - 안세아 #348

Merged
merged 17 commits into from
Jul 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions Seah-An/baekjoon/dfs&bfs/2606.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import sys
from collections import deque

c = int(sys.stdin.readline())
pair = int(sys.stdin.readline())

adj = [[] for _ in range(c)]
visited = [0 for _ in range(c)]

for i in range(pair):
x, y = map(int, sys.stdin.readline().split(" "))
adj[x-1].append(y-1)
adj[y-1].append(x-1)

queue = deque()
queue.append(0)
visited[0] = 1
count = 0


while queue:
v = queue.popleft()
count += 1

for each in adj[v]:
if visited[each] == 0:
queue.append(each)
visited[each] = 1
print(count-1)



42 changes: 42 additions & 0 deletions Seah-An/baekjoon/dfs&bfs/2644.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# import sys
# input = sys.stdin.readline()
from collections import deque

# 입력부

# 전체 사람 수 n
n = int(input())
# 촌수를 계산해야하는 서로 다른 두 사람의 번호
a, b = map(int,input().split())
# 부모 자식들간의 관계의 개수
m = int(input())

G = [[]for _ in range(n+1)]


#부모 자식간의 관계를 나타내는 두 번호 x, y
# x는 y의 부모 번호
for _ in range(m):
x, y= map(int,input().split())
G[x].append(y)
G[y].append(x)

def bfs(v):
q= deque()
visited = [0 for _ in range(n + 1)]
q.append(v)
visited[v] = 1
chon = 0
while q:
chon += 1
for _ in range(len(q)):
v = q.popleft()
if v == b:
return chon -1
for e in G[v]:
if not visited[e]:
visited[e] = 1
q.append(e)
return -1

print(bfs(a))
24 changes: 24 additions & 0 deletions Seah-An/book/binary_search/binary_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
def binary_search(element, some_list, start_index=0, end_index=None):
if end_index == None:
end_index = len(some_list) - 1

if start_index > end_index:
return None

medium = (start_index + end_index) // 2

if some_list[medium] == element:
return medium

if element < some_list[medium]:
return binary_search(element, some_list, start_index, medium - 1)
else:
return binary_search(element, some_list, medium + 1, end_index)



print(binary_search(2, [2, 3, 5, 7, 11]))
print(binary_search(0, [2, 3, 5, 7, 11]))
print(binary_search(5, [2, 3, 5, 7, 11]))
print(binary_search(3, [2, 3, 5, 7, 11]))
print(binary_search(11, [2, 3, 5, 7, 11]))
13 changes: 13 additions & 0 deletions Seah-An/book/implementation/AA.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import sys
# sys.stdin=open("input.txt","rt")
n, k = map(int, input().split())
arr = []
for i in range(1,n+1):
if n % i == 0:
arr.append(i)

if len(arr) < k:
print(-1)
else:
print(arr.pop(k-1))

37 changes: 37 additions & 0 deletions Seah-An/book/palindrome/palindrome-deque-is-better.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Definition for singly-linked list.
import collections
from typing import Deque


class ListNode(object):
def __init__(self, value, next=None):
self.value = value
self.next = next


# deque는 리스트와 달리 양쪽 방향에서 추출이 가능하기때문에 시간복잡도 O(1)
class Solution(object):
def isPalindrome(self, head: ListNode) -> bool:
l: Deque = collections.deque()
node = head

while node is not None:
l.append(node.value)
node = node.next

while len(l) > 1:
if l.pop(0) != l.pop():
return False

return True


if __name__ == "__main__":
rs = Solution()
# 1->2->1
ln = ListNode(1)
ln2 = ListNode(2)
ln3 = ListNode(1)
ln.next = ln2
ln2.next = ln3
print(rs.isPalindrome(ln))
32 changes: 32 additions & 0 deletions Seah-An/book/palindrome/palindrome-linked-list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Definition for singly-linked list.
class ListNode(object):
def __init__(self, value, next=None):
self.value = value
self.next = next


class Solution(object):
def isPalindrome(self, head: ListNode) -> bool:
list = []
node = head

while node is not None:
list.append(node.value)
node = node.next

while len(list) > 1:
if list.pop(0) != list.pop():
return False

return True


if __name__ == "__main__":
rs = Solution()
# 1->2->1
ln = ListNode(1)
ln2 = ListNode(2)
ln3 = ListNode(1)
ln.next = ln2
ln2.next = ln3
print(rs.isPalindrome(ln))
21 changes: 21 additions & 0 deletions Seah-An/book/palindrome/reverse-linked-list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Definition for singly-linked list.
class ListNode(object):
def __init__(self, val=0, next=None):
self.val = val
self.next = next



class Solution(object):
def reverseList(self, head: ListNode) -> ListNode:
node = head
prev = None

while node != None:
temp = node.next
node.next = prev
prev = node
node = temp
return prev


10 changes: 10 additions & 0 deletions Seah-An/book/selection_sort/6-1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
array = [7,5,9,0,3,1,6,2,4,8]

for i in range(len(array)):
min_index = i
for j in range(i+1, len(array)):
if array[min_index] > array[j]:
min_index = j
array[i], array[min_index] = array[min_index], array[i]

print(array)
17 changes: 17 additions & 0 deletions Seah-An/inflearn/implementation/K번째 큰 수.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import sys
sys.stdin=open("in1.txt","rt")

N, K = map(int, input().split())

arr = list(map(int,input().split()))

count = 0
res = set()
for i in range(N):
for j in range(i+1,N):
for k in range(j+1, N):
res.add(arr[i] + arr[j] + arr[k])

res = list(res)
res.sort(reverse=True)
print(res[K-1])
14 changes: 14 additions & 0 deletions Seah-An/inflearn/implementation/K번째수.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import sys

sys.stdin = open("input.txt", "rt")

# 테케
T = int(input())

for i in range(T):
N, s, e, k = map(int, input().split())

a = list(map(int,input().split()))
a = a[s-1:e]
a.sort()
print("#%d %d" %(i+1,a.pop(k-1)))
25 changes: 25 additions & 0 deletions Seah-An/programmers/graph/programmers_순위.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
def solution(n, results):
answer = 0
win = {}
lose = {}

for i in range(1, n + 1):
win[i] = set()
lose[i] = set()
for winner, loser in results:
win[winner].add(loser)
lose[loser].add(winner)

for i in range(1, n + 1):
for winner in lose[i]:
win[winner].update(win[i])
for loser in win[i]:
lose[loser].update(lose[i])

for i in range(1, n + 1):
if len(win[i]) + len(lose[i]) == n - 1:
answer += 1
return answer


solution(5, [[4, 3], [4, 2], [3, 2], [1, 2], [2, 5]])
29 changes: 29 additions & 0 deletions Seah-An/programmers/heap/programmers_disc_controller4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import heapq


def solution(jobs):
answer = 0

jobs_heap = []
start_time = -1
current_time = 0
completed_jobs = 0
num_of_jobs = len(jobs)

while num_of_jobs > completed_jobs:
for job in jobs:
# 시작시간이 start시간과 current_time사이에 걸쳐있다면
if start_time < job[0] <= current_time:
heapq.heappush(jobs_heap, [job[1], job[0]])
if jobs_heap:
min_job = heapq.heappop(jobs_heap)
start_time = current_time
current_time += min_job[0]
answer += current_time - min_job[1]
completed_jobs += 1
else:
current_time += 1
return answer // num_of_jobs


print(solution([[1, 3], [2, 2], [8, 11]]))
18 changes: 18 additions & 0 deletions Seah-An/programmers/heap/programmers_더맵게_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import heapq

def solution(scoville, K):
answer = 0
heap =[]
for i in scoville:
heapq.heappush(heap,i)

while heap[0] < K:
if heap[0] < K and len(heap) == 1:
return -1
heapq.heappush(heap,heapq.heappop(heap) + heapq.heappop(heap)*2)
answer += 1

return answer


print(solution([1, 2, 3, 9, 10, 12], 7))
39 changes: 39 additions & 0 deletions Seah-An/programmers/heap/programmers_이중우선순위큐.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# https://programmers.co.kr/learn/courses/30/lessons/42628

import heapq

def solution(operations):
answer = []

heap = []

for i in operations:
# 명령어
command = i[0]
num = int(i[2:])

if command == 'I':
heapq.heappush(heap,num)
heap.sort()
elif command == 'D':
if len(heap) == 0:
pass
# 최대값 삭제
elif num == 1:
heap.sort()
del heap[-1]
# 최소값 삭제
elif num == -1:
heapq.heappop(heap)

if len(heap) == 0:
answer = [0,0]
else:
answer.append(heap[-1])
answer.append(heap[0])
return answer

print(solution(["I 16","D 1"]))
print(solution(["I 7","I 5","I -5","D -1"]))
print(solution(["I 16","I -5643", "D -1","D 1","I 123","D -1"]))
print(solution(["I 4", "I -1", "I 6", "I 3"]))
Loading