algorithm_study's Introduction
algorithm_study's People
algorithm_study's Issues
7/29 Python **sample
ํ์ด์ฌ์์๋ C, C++๊ณผ ๋ฌ๋ฆฌ ํฌ์ธํฐ๋ผ๋ ๊ฐ๋ ์ด ์์ต๋๋ค.
-
์ฐ๊ฒฐ๋ฆฌ์คํธ๋ ์์ ์ ๋ ธ๋์์ ๋ค์์ ๋ ธ๋๋ง์ ๊ฐ๋ฆฌํฌ ์ ์๋ ํํ๋ฅผ ๊ฐ์ง๊ณ ์์ต๋๋ค.
-
์ฐ๊ฒฐ๋ฆฌ์คํธ์ ํน์ง์ ํ๋ก๊ทธ๋จ์ด ์คํ๋๋ ์ค๊ฐ์ ํฌ๊ธฐ๋ฅผ ๋ฐ๊ฟ ์ ์๊ณ ์ฐ์๋ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ฌ์ฉํ์ง ์๋ ๋ค๋ ์ ์ด๋ค.
์ฐ๊ฒฐ๋ฆฌ์คํธ์ ๋ ธ๋
class Node:
def __init__(self,data,next=None):
self.data=data
self.next=next
- ์ฐ๊ฒฐ๋ฆฌ์คํธ ๋ ธ๋๋ ์ ๋ณด๋ฅผ ์ ์ฅํ๋ data์ ๋ค์ ๋ ธ๋๋ฅผ ์ฐ๊ฒฐํ๋ ๋งํฌ๋ฅผ ์ ์ฅํ๋ next๋ก ๊ตฌ์ฑ ๋์ด ์์ต๋๋ค.
์ฐ๊ฒฐ๋ฆฌ์คํธ ์ด๊ธฐํ
def init_list():
global node_A
node_A=Node("A")
node_B=Node("B")
node_A.next=node_B
์ฐ๊ฒฐ๋ฆฌ์คํธ ์ถ๋ ฅ
class Node:
def __init__(self,data,next=None):
self.data=data
self.next=next
def init_list():
global node_A
node_A=Node("A")
node_B=Node("B")
node_A.next=node_B
def print_list():
global node_A
node=node_A
while node:
print(node.data)
node=node.next
print
if __name__=='__main__':
init_list()
print_list()
[๋ฐฑ์ค 2751] ์ ๋ ฌ_๋ฒ๋ธ์ ๋ ฌ,๋ณํฉ์ ๋ ฌ(์ฌ์๋)
๋ฒ๋ธ์ ๋ ฌ๋ก ๋์
=> ๋ฒ๋ธ ์ ๋ ฌ์ ์ต์ ํ๊ท ์ต์
์ ์๊ฐ ๋ณต์ก๋๊ฐ ๋ชจ๋ O(N^2)์ธ ์ ๋ ฌ ๋ฐฉ๋ฒ์ด๊ธฐ ๋๋ฌธ์ ์ค์ฉ์ ์ด์ง ์์
==> ๋ฐํ์์๋ฌ
O(N lg N)์ธ ์ ๋ ฌ์๋ ๋ณํฉ์ ๋ ฌ, ํต์ ๋ ฌ ๋ฑ์ด ์๋๋ฐ, ๋ณํฉ์ ๋ ฌ๋ก ๋์
=> ๋ฐํ์ ์๋ฌ (๋ญ๊ฐ ์๋ฌ์ธ์ง ๋ค์ ํด๋ด์ผํจ)
[20190811]์ฐ์ ์์ ํ
์ฐ์ ์์ ํ๋?
์ฐ์ ์์๋ก ์ ๋ ฌ๋ ์๋ฃ๊ตฌ์กฐ ํํ
๊ตฌํ ๋ฐฉ์
๋ฐฐ์ด, ์ฐ๊ฒฐ๋ฆฌ์คํธ, ํ ์ผ๋ก ๊ตฌํํ๋ฉฐ, ์ฃผ๋ก ํ์ ์ด์ฉํ๋ค
ํ
์์ ์ด์งํธ๋ฆฌ์ ์ผ์ข
์ผ๋ก ์ต๋ํ, ์ต์ํ ๋ ์ข
๋ฅ๊ฐ ์๋ค
๋ฐ์ ๋ ฌ ์ํ ๋ฅผ ์ ์งํ๋ ๊ฒ์ด ํน์ง์ด๋ฉฐ ์ฃผ๋ก ๋ฐฐ์ด๋ก ๊ตฌํํ๋ค
[๋ฐฑ์ค 10866] ๋ฑ
๋ฑ(Deque) ์ด๋?
- ํ 2๊ฐ๋ฅผ ๋ฐ๋๋ก ๋ถ์ฌ์ ๋ง๋ ์๋ฃ๊ตฌ์กฐ
- ์คํ๊ณผ ํ์ ํน์ฑ์ ๋ชจ๋ ๊ฐ์ง๊ณ ์๋ ์๋ฃ๊ตฌ์กฐ
- ์คํ์ผ๋ก ์ฌ์ฉํ ์๋ ์๊ณ , ํ๋ก๋ ์ฌ์ฉํ ์ ์์
๋ฑ์ ๊ตฌํ
- ์์ชฝ ๋์์ ์ฝ์ /์ญ์ ์ฐ์ฐ์ ์ํํ๋ฉด์ ํฌ๊ธฐ ๋ณํ์ ์ ์ฅ๋ ์์์ ์์ ๋ณํ๊ฐ ๋ง์ผ๋ฏ๋ก ์์ฐจ ์๋ฃ๊ตฌ์กฐ๋ ๋นํจ์จ์
- ์๋ฐฉํฅ์ผ๋ก ์ฐ์ฐ์ด ๊ฐ๋ฅํ ์ด์ค ์ฐ๊ฒฐ ๋ฆฌ์คํธ๋ฅผ ์ฌ์ฉํ๋ค.
C ์ฝ๋
`
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#pragma warning(disable:4996)
typedef struct node {
int elem;
struct node* rlink;
struct node* llink;
}node;//์ด์ค ์ฐ๊ฒฐ ๋ฆฌ์คํธ์ ๋
ธ๋ ๊ตฌ์กฐ
typedef struct deque {
struct node* front;
struct node* back;
}deque;//๋ฑ์ ๊ตฌ์กฐ
void push_front(deque *x, int n);
void push_back(deque *x, int n);
int pop_front(deque *x);
int pop_back(deque *x);
void size(deque *x);
void main() {
int n, i;
int m, ans;
char name[20] = { '\0' };
char *str;
deque x;
x = (deque)malloc(sizeof(deque));
x->front = NULL;
x->back = NULL;//์ด๊ธฐํ
scanf("%d", &n);
for (i = 0; i < n; i++) {
getchar();
scanf("%s", name);
str = (char*)malloc(sizeof(char)*strlen(name) + 1);
strcpy(str, name);
if (strcmp(str, "push_front") == 0) {
scanf("%d", &m);
push_front(x, m);
}
if (strcmp(str, "push_back") == 0) {
scanf("%d", &m);
push_back(x, m);
}
if (strcmp(str, "pop_front") == 0) {
ans = pop_front(x);
if (ans == 0) {
printf("-1\n");
}
else printf("%d\n", ans);
}
if (strcmp(str, "pop_back") == 0) {
ans = pop_back(x);
if (ans == 0) {
printf("-1\n");
}
else printf("%d\n", ans);
}
if (strcmp(str, "size") == 0) {
size(x);
}
if (strcmp(str, "empty") == 0) {
if (x->front == NULL && x->back == NULL) { printf("1\n"); }
else { printf("0\n"); }
}
if (strcmp(str, "front") == 0) {
if (x->front == NULL && x->back == NULL) { printf("-1\n"); }
else { printf("%d\n", x->front->elem); }
}
if (strcmp(str, "back") == 0) {
if (x->front == NULL && x->back == NULL) { printf("-1\n"); }
else { printf("%d\n", x->back->elem); }
}
}//for๋ฌธ
}
void push_front(deque *x, int n) {
node newnode;
newnode = (node)malloc(sizeof(node));
newnode->elem = n;
newnode->rlink = NULL;
newnode->llink = NULL;
if (x->front == NULL && x->back == NULL) {
x->front = newnode;
x->back = newnode;
}//์ฒซ ๋ฒ์งธ ์์์ ์ฝ์
else {
x->front->llink = newnode;
newnode->rlink = x->front;
x->front = newnode;
}
}
void push_back(deque *x, int n) {
node newnode;
newnode = (node)malloc(sizeof(node));
newnode->elem = n;
newnode->rlink = NULL;
newnode->llink = NULL;
if (x->front == NULL&& x->back == NULL) {
x->front = newnode;
x->back = newnode;
}//์ฒซ ๋ฒ์งธ ์์์ ์ฝ์
else {
x->back->rlink = newnode;
newnode->llink = x->back;
x->back = newnode;
}
}
int pop_front(deque *x) {//์ญ์ ํ์ ๊ณต๋ฐฑ์ด ๋๋ ๊ฒฝ์ฐ๋ ์๊ฐ
int ans;
if (x->front == NULL&& x->back == NULL) {
ans = 0;
}//์ ์ด์ ๊ณต๋ฐฑ
else {
ans = x->front->elem;
if (x->front == x->back) {
x->front = NULL;
x->back = NULL;
}//์์๊ฐ ํ ๊ฐ ๋ฐ์ ์๋จ์์ ๋ ์ญ์ ํ ์ด๊ธฐํ
else {
x->front = x->front->rlink;
x->front->llink = NULL;
}
}
return ans;
}
int pop_back(deque *x) {
int ans;
if (x->front == NULL&& x->back == NULL) {
ans = 0;
}//๊ณต๋ฐฑ
else {
ans = x->back->elem;
if (x->front == x->back) {
x->front = NULL;
x->back = NULL;
}//์์๊ฐ ํ ๊ฐ ๋ฐ์ ์๋จ์์ ๋ ์ญ์ ํ ์ด๊ธฐํ
else {
x->back = x->back->llink;
x->back->rlink = NULL;
}
}
return ans;
}
void size(deque *x) {
node *p;
int cnt = 0;
p = x->front;
while (p != NULL) {
cnt++;
p = p->rlink;
}
printf("%d\n", cnt);
}
`
๋ฐํ์์๋ฌ๊ฐ ๋จ๋๋ฐ ์ด์ ๋ฅผ ์ ์๊ฐ ์๋ค ,,,,,,,,
[๋ฐฑ์ค 2750] ์ ๋ ฌ_๋ฒ๋ธ์ ๋ ฌ
[๋ฐฑ์ค 11279] ์ต๋ํ
ํ๊ณผ ์ด์งํ์ํธ๋ฆฌ
- ๊ณตํต์ : ์ด์งํธ๋ฆฌ
- ์ฐจ์ด์ :
- ํ: ์์(์ผ์ชฝ,์ค๋ฅธ์ชฝ)๋ ธ๋ < ๋ ธ๋(๋ถ๋ชจ) => ์ฐ์ ์์ ์ ๋ ฌ์ ์ข์.
- ์ด์งํ์ํธ๋ฆฌ: ์ผ์ชฝ ์์๋ ธ๋<๋ถ๋ชจ๋ ธ๋<์ค๋ฅธ์ชฝ ์์๋ ธ๋ => ํ์์ ์ข์.
heapify=ํ ์ฑ์ง์ ๋ง์กฑํ๋ ์ฐ์ฐ
- ์ผ๋ฐ์ ์ผ๋ก ์ด์งํธ๋ฆฌ ๋์ด์ ์๊ฐ๋ณต์ก๋๋ log
2n -> ์์ธ์ง๋ชจ๋ฆ ๋ชป์ฐพ์ ใ ใ ใ ใ ใ
๊ฐ์ ๋ฐ๊พธ๊ฑฐ๋ ๋น๊ตํ๋ ์ฐ์ฐ์ O(1)์ด๋ฏ๋ก heapify์ ๊ณ์ฐ๋ณต์ก๋๋ ํธ๋ฆฌ ๋์ด์ ์์กด์ ์ด๋ค. ๋ฐ๋ผ์ O(log n) ์ด๋ค.
Python์ฝ๋
def heapify(unsorted,index,heap_size):
largest=index #๋ฐฐ์ด์ ์ธ๋ฑ์ค!! (๋ฐฐ์ด๊ฐ์ด ์๋)
left_index=2*index+1
right_index=2*index+2
if left_index<heap_size and unsorted[left_index]>unsorted[largest]: # ์๋ก ๋ค์ด์จ index๊ฐ left index๋ณด๋ค ์์ผ๋ฉด,
largest=left_index # left index๋ฅผ ์ต๋๊ฐ ์ธ๋ฑ์ค๋ก ์ค์ ํด์ค๋ค!
if right_index<heap_size and unsorted[right_index]>unsorted[largest]:
largest=right_index
if largest!=index:
unsorted[largest],unsorted[index]=unsorted[index],unsorted[largest] # largest๊ฐ ์์ ๋ if๋ฌธ์ ์ํด index์ ๋ค๋ฅธ ๊ฒฝ์ฐ unsorted[index]์ unsorted[largest]๋ฅผ(->๋ฐฐ์ด๊ฐ) ๋ฐ๊ฟ์ค๋ค.
heapify(unsorted,largest,heap_size) # ๊ทธ๋ฆฌ๊ณ ์ฌ๊ท๋ก ๋ฐ๋ณต~~!!
def heap_sort(unsorted):
n=len(unsorted)
for i in range(n//2-1,-1,-1): # ํ์ด์ฌ์์ // ์ฐ์ฐ์๋ ๋๋๊ณ ๋ชซ! ๋ง ์๋ ค์ค๋๋ค(์์ฐ์)
# ์๊ณ ๋ n=13์ผ๋ 6(13//2)๋ถํฐ 0(-1)๊น์ง ๊ฑฐ๊พธ๋ก(-1) ๋์๊ฐ๋๋ค.
heapify(unsorted,i,n)
##์ฌ๊ธฐ ๋ถ๋ถ์ ๋๋ฆฌ๊ฒ ๋๋ฉด heap sort๊ฐ ๋ฉ๋๋ค ! ํ์ง๋ง ์ด ๋ฌธ์ ๋ heap "sort"๊ฐ ์๋๋ฏ๋ก ์ง์ ์!
#for i in range(n-1,0,-1):
#unsorted[0],unsorted[i]=unsorted[i],unsorted[0]
#heapify(unsorted,0,i)
return unsorted
unsorted=[]
n=int(input())
for i in range(n):
a=int(input())
if a==0: # 0์ ์
๋ ฅ๋ฐ์์๋ unsorted๋ฐฐ์ด์ด ๋น์ด์์ผ๋ฉด 0์ ์ถ๋ ฅํ๊ณ ๊ทธ๋ ์ง ์์ผ๋ฉด ๋ฃจํธ๋
ธ๋(์ต๋๊ฐ)๋ฅผ ์ถ๋ ฅํ๊ณ ์ง์์ค๋๋ค.
if len(unsorted)==0:
print(0)
else:
heap_sort(unsorted)
print(unsorted[0])
del unsorted[0] ## ์ต๋ํ ์ด๋ฏ๋ก ๋ฃจํธ๋
ธ๋๋ฅผ ์ง์์ค๋๋ค!
else:
unsorted.append(a) # 0์ด ์๋๋ฉด unsorted๋ฐฐ์ด์ ์ฐจ๊ณก์ฐจ๊ณก ๋ฃ์ด์ค๋๋ค.
[ํฉ์ ์ง]
[๋ฐฑ์ค 2750] ์ ๋ ฌ _ ์ฝ์ ์ ๋ ฌ
- ์๊ฐ ๋ณต์ก๋๊ฐ n ์ ๊ณฑ์ธ ์ ๋ ฌ(ex. ๋ฒ๋ธ์ ๋ ฌ, ์ฝ์ ์ ๋ ฌ)์ ์ด์ฉํ์ฌ ํธ๋ ๋ฌธ์
์ฝ์ ์ ๋ ฌ์ด๋?
- ์ ๋ ฌ๋ ๋ถ๋ถ(S)๊ณผ ์์ง ์ ๋ ฌ๋์ง ์์ ๋ถ๋ถ(U)์์ ๋ฐฐ์ด์ด ๋ค์ ๊ตฌ์ฑ๋๋ค๊ณ ์๊ฐํ๋ฉฐ ๋น๊ต, ์ฝ์ ์ ๋ฐ๋ณตํ๋ค
- ์ค๋ช ์ ๋ณด๋ฉด ์ฝ์ ์ ๋ ฌ์ด ๋ฌด์์ ์๋ฏธํ๋์ง๋ ์๊ฒ ๋๋ฐ ์๊ฐ ๋ณต์ก๋๋ฅผ ์์ง๋ ์ ๋ชจ๋ฅด๊ฒ ๋ค ใ
[๋ฐฑ์ค2750] ์ค๋ฆ์ฐจ์ ์ ๋ ฌ PYTHON
N=int(input())
order=[]
for i in range(N):
order.append(int(input()))
tmp=0
for i in range(N):
for j in range(N):
if order[i]<order[j]:
tmp=order[i]
order[i]=order[j]
order[j]=tmp
for i in range(N):
print(order[i])
์ ๊ฐ ์ด์ฉํ ๋ฐฉ๋ฒ์ bubble sort์
๋๋ค.
์ฐ์๋ ๋ ์ซ์๋ฅผ ๋น๊ตํ๊ณ ํด๋น ์กฐ๊ฑด๋ฌธ์ ๊ฑฐ์ณ ์์๋ฅผ ๋ฐ๊พธ๋ ๋ฐฉ๋ฒ์
๋๋ค.
์ ์ฒด ๋น๊ต๋ฅผ ์งํํ๊ธฐ ๋๋ฌธ์, ์๊ฐ๋ณต์ก๋๋ n์ ๊ณฑ์
๋๋ค.
[๊ณต์ง]Milestones ์ด์ฉํด ๋ณด๊ธฐ
์ด์๋ฅผ ๋ณด๊ธฐ์ข๊ฒ ํ๋ ค๊ณ milestones์ ์จ๋ณผ๊น ํ๋๋ฐ์ฉ milestones์ด๋ issue๋ฅผ ์ฃผ์ ๋ณ๋ก ๋ฌถ๊ธฐ์ํด ์ฌ์ฉํฉ๋๋ค!
๋ฐฉ๋ฒ
1. ์ด์์ฐฝ์์ milestones ๋ฒํผ์ ๋๋ฅธ๋ค
2. ์ ์ ํ milestones ์ ์ฐพ๋๋ค
3. ํด๋น milestone์ issue๋ฅผ ์ ๋ฆฌํ๋ค
ํน์ milestones ์ ์ถ๊ฐ๋ก ๋ ์๊ณ ์ถ๋ค๋ฉด ๋งํฌ ์ฐธ๊ณ ํด ์ฃผ์๊ณ
๋ ๊นํ๋ธ ์ด์๋ ๋งํฌ๋ค์ด ๋ฌธ๋ฒ์ ์ฌ์ฉํฉ๋๋ค! ์ค๋ช ๋งํฌ๋ค์ด ๋ฌธ๋ฒ ์ค๋ช ๋งํฌ ์ฌ๋ ค๋๋ฆด๊ป์
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.