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

[Add] Algo16 #36

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
26 changes: 26 additions & 0 deletions YJ/1018_체스판.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
N, M = map(int, input().split())
chessboard = []
count = []

for _ in range(N):
chessboard.append(input())

for a in range(N-7):
for b in range(M-7):#8개보다 클 때, 큰 숫자마다 하나씩 더 돌려줌
Start_W = 0
Start_B = 0
for i in range(a, a+8):
for j in range(b, b+8):
if (i+j) % 2 == 0: #인덱스의 합이 짝수
if chessboard[i][j] != 'W': #W로 색칠되어야 함
Start_W += 1
if chessboard[i][j] != 'B': #B로 색칠되어야 함
Start_B += 1
else: #인덱스의 합이 홀수
if chessboard[i][j] != 'B':
Start_W += 1
if chessboard[i][j] != 'W':
Start_B += 1
count.append(min(Start_W, Start_B))

print(min(count))
3 changes: 3 additions & 0 deletions YJ/1085_직사각형에서 탈출.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
x, y, w, h = map(int, input().split())
print(min(x-0, y-0, w-x, h-y))
#사각형의 네 모서리 중 가장 가까운데로 가면 된다.
12 changes: 12 additions & 0 deletions YJ/10989.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#sort를 쓰면 메모리가 초과됨
#다른 문제 풀 때도 썼던 방법을 응용 (인덱스 번호를 기준으로 숫자를 1 증가하는것과 비슷하게)
#해당 숫자의 개수를 배열에 넣고, 숫자의 개수만큼 출력!
import sys
N = int(sys.stdin.readline())
A = [0] * 10001 #이 수는 10,000보다 작거나 같은 자연수이다.
for i in range(N):
A[int(sys.stdin.readline())] += 1
for i in range(10001):
if A[i] != 0:
for j in range(A[i]):
print(i)
20 changes: 20 additions & 0 deletions YJ/1181_중복제거.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#주어진 조건

#중복제거

#길이가 짧은 것부터
#길이가 같으면 사전 순으로

import sys
N=int(input())
wordList=[]

for i in range(N):
wordList.append(input())

wordList=list(set(wordList)) #set 함수는 중복 제거를 해줌
wordList.sort() #파이썬은 알파벳 정렬도 됨!!
wordList.sort(key=len) #길이가 짧은 것부터

for i in wordList:
print(i)
8 changes: 8 additions & 0 deletions YJ/15829_해싱.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
L = int(input())
string = input()
sum=0

for i in range(L):
sum += (ord(string[i])-96) * (31 ** i) #ord함수 : 아스키코드 값
#a 기준으로 아스키코드는 97이므로 96을 뺌
print(sum % 1234567891) #나누는 수를 1234567891라고 문제에서 나옴
25 changes: 25 additions & 0 deletions YJ/1920.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import sys

N=sys.stdin.readline()
A = sorted(map(int,sys.stdin.readline().split()))
#이진탐색은 정렬되어 있는 경우에만 가능!
M=sys.stdin.readline()
B = map(int,sys.stdin.readline().split())


def binary(l,A,start,end):
if start>end:
return 0
M=(start+end)//2

if l==A[M]:
return 1
elif l<A[M]:
return binary(1,A,start,M-1)
else:
return binary(1,A,M+1,end)

for l in B:
start=0
end=len(A)-1
print(binary(l,A,start,end))
35 changes: 35 additions & 0 deletions YJ/1966.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include<iostream>
#include<queue>
using namespace std;

int main(){
int testcase,N, M,star,count;
cin>>testcase;
for(int i=0;i<testcase;i++){
count=0;
cin>>N>>M;
queue<pair<int,int>>q;
priority_queue<int>pq; //우선순위 큐

for(int j=0;j<N;j++){
cin>>star;
q.push({j,star}); //인덱스, 중요도
pq.push(star); //중요도
}

while(!q.empty()){
int curIndex=q.front().first; //인덱스
int curStar=q.front().second; //중요도
q.pop();
if(pq.top()==curStar){ //우선순위 큐의 top이랑 현재 중요도가 같으면 출력하고 카운트 업
pq.pop();
count++;
if(curIndex==M){ //원하던 프린트가 출력되면 현재 카운트 출력
cout<<count<<endl;
break;
}
}
else q.push({curIndex,curStar});//중요도 낮으면 다시 넣기
}
}
}
22 changes: 22 additions & 0 deletions YJ/2164.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include<iostream>
#include<queue>
using namespace std;

int main(){
int N;
cin>>N;
queue<int> q;

for (int i=1;i<N+1;i++){
q.push(i);
}


while(q.size()>1){
q.pop();
q.push(q.front());
q.pop();
}

cout<<q.front();
}
17 changes: 17 additions & 0 deletions YJ/2609.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
a,b=map(int,input().split())


#최대공약수
for i in range(min(a,b),0,-1):
#a와 b 중 작은 숫자부터 1까지, 1씩 감소하며, a와 b를 i로 나눔
#두 수를 모두 나눌 수 있는 약수 중 가장 큰 수
if a%i==0 and b%i==0:
print(i)
break

#최소공배수
for i in range(max(a,b),(a*b)+1):
#a와 b 중 큰 숫자부터 둘의 곱까지, 1씩 증가하며, i를 a와 b를 나눔
if i%a==0 and i%b==0:
print(i)
break
35 changes: 35 additions & 0 deletions YJ/2775.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <iostream>
using namespace std;

int main()
{
int T, k, n;
cin >> T;

for (int i = 0; i < T; i++)
{ ///
cin >> k;
cin >> n;
int person[k][n] = {
0,
};
for (int a = 0; a <= k; a++)
{
for (int b = 0; b <= n; b++)
{
person[0][b] = b;
person[a][1] = 1;
}
} //초기화

for (int a = 1; a <= k; a++)
{
for (int b = 2; b <= n; b++)
{
//반복문
person[a][b] = person[a - 1][b] + person[a][b - 1];
}
}
cout << person[k][n];
}
}
149 changes: 149 additions & 0 deletions YJ/BinarySearchTree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
class Node:
def __init__(self, data):
self.left = None
self.data = data
self.right = None

class BinarySearchTree:

def __init__(self):
self.rootNode = None


def insert(self, data):
if self.rootNode is None:
self.rootNode = Node(data)
else:
self.base = self.rootNode
while True:
if data == self.base.data:
print("중복된 데이터 입니다.")
break
elif data > self.base.data:
if self.base.right is None:
self.base.right = Node(data)
break
else:
self.base = self.base.right
else:
if self.base.left is None:
self.base.left = Node(data)
break
else:
self.base = self.base.left

def remove(self, data):
self.searched = False
self.cur_node = self.rootNode
self.parent = self.rootNode
while self.cur_node:
if self.cur_node.data == data:
self.searched = True
break
elif self.cur_node.data > data:
self.parent = self.cur_node
self.cur_node = self.cur_node.left
else:
self.parent = self.cur_node
self.cur_node = self.cur_node.right
if self.searched:#지울거 찾음
# rootNode를 지우는 경우
if self.cur_node.data == self.parent.data:
self.rootNode = None #None 처리하면 데이터는 삭제됨!
else: #rootNode가 아닌 그외의 경우
# 1) 삭제하는 node가 leaf node인 경우
if self.cur_node.left is None and self.cur_node.right is None:
if self.parent.data > self.cur_node.data:
self.parent.left = None
else:
self.parent.right = None

# 2) 삭제하는 node의 자식이 하나인 경우
elif self.cur_node.left is not None and self.cur_node.right is None:
if self.parent.data > data:
self.parent.left = self.cur_node.left
else:
self.parent.right = self.cur_node.left
elif self.cur_node.left is None and self.cur_node.right is not None:
if self.parent.data > data:
self.parent.left = self.cur_node.right
else:
self.parent.right = self.cur_node.right

# 3) 삭제하는 node의 자식이 둘인 경우
elif self.cur_node.left is not None and self.cur_node.right is not None:
self.tmp_parent = self.cur_node.right
self.tmp_cur = self.cur_node.right
while self.tmp_cur.left:
self.tmp_parent = self.tmp_cur
self.tmp_cur = self.tmp_cur.left
if self.tmp_cur.right is not None:
self.tmp_parent.left = self.tmp_cur.right
else:
self.tmp_parent.left = None
if self.parent.data > data:
self.parent.left = self.tmp_cur
else:
self.parent.right = self.tmp_cur
self.tmp_cur.left = self.cur_node.left
self.tmp_cur.right = self.cur_node.right
else: #지울거 못 찾음
print("해당 데이터가 존재하지 않습니다.")

def search(self, data):
self.base = self.rootNode
while self.base:
if self.base.data == data:
return True
elif self.base.data > data:
self.base = self.base.left
else:
self.base = self.base.right
return False


#재귀함수를 쓰면 구현할 수 있음
#전위 순회
def preOrder(self, node):
if not node:
return
print(node.data, end=' ')
self.preOrder(node.left)
self.preOrder(node.right)

#중위 순회
def inOrder(self, node):
if not node:
return
self.inOrder(node.left)
print(node.data, end=' ')
self.inOrder(node.right)

#후위 순회
def PostOrder(self, node):
if not node:
return
self.PostOrder(node.left)
self.PostOrder(node.right)
print(node.data, end=' ')

b = BinarySearchTree()
b.insert(31)
b.insert(15)
b.insert(41)
b.insert(12)
b.insert(18)
b.insert(40)
b.insert(51)
b.insert(11)
b.insert(13)
b.insert(16)
b.insert(19)
b.insert(17)
b.insert(20)

b.preOrder(b.rootNode)
print()
b.inOrder(b.rootNode)
print()
b.PostOrder(b.rootNode)