Skip to content

Commit 71eff01

Browse files
committed
Quartz sync: Aug 19, 2025, 5:11 PM
1 parent 377c8a1 commit 71eff01

File tree

30 files changed

+2199
-144
lines changed

30 files changed

+2199
-144
lines changed

content/Computer Science/1 Foundations & Theory/Algorithms/Greedy.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
1-
- 부분 최적
2-
- 현재 가장 최상인 경우 선택
3-
- 미래의 경우를 생각 안하고
4-
- 이 경우 항상 최적해 보장 못함
5-
- 단, 미래의 경우에 영향일 미치지 않는다면, 항상 최적해 보장
1+
---
2+
description:
3+
aliases:
4+
created: 2025-03-22
5+
modified: 2025-08-19
6+
---
7+
8+
- 최적 부분 구조
9+
- 탐욕적 선택 속성
10+
- 현재 가장 최상인 경우 선택
11+
- 미래의 경우를 생각 안하고
12+
- 이 경우 항상 최적해 보장 못함
13+
- 단, 미래의 경우에 영향일 미치지 않는다면, 항상 최적해 보장
14+
- 지금 당장의 최적이 전체의 최적해를 보장한다면
615
- 거스름돈 최적화
716
- 1, 5, 10, 50원 의 경우 '65원' 최적해 보장
817
- 50, 10, 5

content/Computer Science/1 Foundations & Theory/Algorithms/Quick Sort.md

Lines changed: 57 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -7,66 +7,71 @@ tags:
77
references:
88
- https://www.youtube.com/watch?v=PgBzjlCcFvc
99
---
10-
11-
- 가장 쉬운 버전
12-
- 피벗은 가장 왼쪽 값
13-
- 피벗 보다 작으면 왼쪽 임시 리스트에 추가
14-
- 크거나 같으면 오른쪽 임시 리스트에 추가
15-
- return `quicksort(left) + [pivot] + quicksort(right)`
16-
- ```python
17-
def qsort(data):
18-
if len(data) <= 1:
19-
return data
20-
21-
pivot = data[0]
10+
# Choice of Pivot
11+
- 처음, 마지막 요소 피벗
12+
- 랜덤 요소 피벗
13+
- 중앙값 피벗
14+
# Partition Algorithm
15+
## Naive Partition
16+
- 피벗은 가장 왼쪽 값(혹은 마지막)
17+
- 피벗 보다 작으면 왼쪽 임시 리스트에 추가
18+
- 크거나 같으면 오른쪽 임시 리스트에 추가
19+
- return `quicksort(left) + [pivot] + quicksort(right)`
20+
- ```python
21+
def qsort(data):
22+
if len(data) <= 1:
23+
return data
2224

23-
left, right = list(), list()
24-
for index in range(1, len(data)):
25-
if pivot > data[index]:
26-
left.append(data[index])
27-
else:
28-
right.append(data[index])
29-
30-
#left = [ item for item in data[1:] if pivot > item]
31-
#right = [ item for item in data[1:] if pivot <= item]
25+
pivot = data[0]
3226

33-
return qsort(left) + [pivot] + qsort(right)
34-
- 최적 버전
35-
- 최초 조건
36-
- left_index = -1 (pivot 보다 같거나 작은 값 )
37-
- right_index = 0 (looping 용 index)
38-
- left_index < right_index
39-
- 맨 오른쪽 요소를 pivot으로 삼는다
27+
left, right = list(), list()
28+
for index in range(1, len(data)):
29+
if pivot > data[index]:
30+
left.append(data[index])
31+
else:
32+
right.append(data[index])
33+
34+
#left = [ item for item in data[1:] if pivot > item]
35+
#right = [ item for item in data[1:] if pivot <= item]
36+
37+
return qsort(left) + [pivot] + qsort(right)
38+
39+
## Lomuto Partition
40+
- 최초 조건
41+
- left_index = -1 (pivot 보다 같거나 작은 값 )
42+
- right_index = 0 (looping 용 index)
43+
- left_index < right_index
44+
- 맨 오른쪽 요소를 pivot으로 삼는다
45+
- loop
4046
- right_index 요소와 pivot 비교
4147
- 같거나 작으면 `left_index++` + `swap(arr[left_index], arr[right_index])`
4248
- 크면 pass
4349
- `right_index++`
44-
- right_index가 pivot 위치 전까지 오면 `swap(arr[left_index + 1], arr[pivot])`
45-
- pivot은 제자리에 가게됨
46-
- left_side, right_side 재귀로 quick_sort 호출
47-
- ```cpp
48-
void quickSort(int arr[], int low, int high){
49-
if (low < high){
50-
int pivot_index = partition(arr, low, high);
51-
52-
quickSort(arr, low, pivot_index - 1);
53-
quickSort(arr, pivot_index + 1, high)
54-
}
50+
- right_index가 pivot 위치 전까지 오면 `swap(arr[left_index + 1], arr[pivot])`
51+
- pivot은 제자리에 가게됨
52+
- left_side, right_side 재귀로 quick_sort 호출
53+
- ```cpp
54+
void quickSort(int arr[], int low, int high){
55+
if (low < high){
56+
int pivot_index = partition(arr, low, high);
57+
58+
quickSort(arr, low, pivot_index - 1);
59+
quickSort(arr, pivot_index + 1, high)
5560
}
61+
}
62+
63+
int partition(int arr[], int low, int high){
64+
int pivot = arr[high];
65+
int left_index = (low - 1);
5666

57-
int partition(int arr[], int low, int high){
58-
int pivot = arr[high];
59-
int left_index = (low - 1);
60-
61-
for (right_index = low; right_index <= high - 1; right_index++){
62-
if (arr[right_index] <= pivot){
63-
left_index++;
64-
swap(&arr[left_index], &arr[right_index]);
65-
}
67+
for (right_index = low; right_index <= high - 1; right_index++){
68+
if (arr[right_index] <= pivot){
69+
left_index++;
70+
swap(&arr[left_index], &arr[right_index]);
6671
}
67-
swap(&arr[left_index + 1], &arr[high]);
68-
return (left_index + 1);
6972
}
70-
71-
- [ ] 버전 1의 퀵소트의 동작 방법 더 간략히 정리 #context
73+
swap(&arr[left_index + 1], &arr[high]);
74+
return (left_index + 1);
75+
}
76+
7277

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
---
2+
tags: ["ps/boj/bronze/3", "ps/boj/bronze", "cs/algorithms/mathematics/ps","cs/algorithms/implementation/ps","cs/algorithms/greedy/ps"]
3+
---
4+
5+
# Problem
6+
- [https://www.acmicpc.net/problem/10162](https://www.acmicpc.net/problem/10162)
7+
8+
# Logic
9+
##### 문제 유형 파악
10+
11+
##### 제약 조건 파악
12+
13+
##### 수도 코드 작성
14+
15+
##### 구현
16+
17+
##### 최적화 및 테스트
18+
19+
##### 개선 가능점
20+
21+
# My Code
22+
23+
## cpp
24+
25+
```cpp title="boj/10162.cpp"
26+
27+
#include <vector>
28+
void solution() {
29+
vector<int> timer = { 300, 60, 10 };
30+
vector<int> push_count(3, 0);
31+
32+
int t;
33+
cin >> t;
34+
35+
for (size_t i = 0; i < timer.size(); ++i) {
36+
push_count[i] = t / timer[i];
37+
t %= timer[i];
38+
}
39+
40+
if (t) {
41+
cout << -1;
42+
} else {
43+
cout << push_count[0] << " " << push_count[1] << " " << push_count[2];
44+
}
45+
}
46+
47+
48+
```
49+
50+
## python
51+
52+
```python title="boj/10162.py"
53+
54+
55+
56+
def solve():
57+
return
58+
59+
60+
```
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
---
2+
tags: ["ps/boj/bronze/2", "ps/boj/bronze", "cs/algorithms/mathematics/ps","cs/algorithms/implementation/ps"]
3+
---
4+
5+
# Problem
6+
- [https://www.acmicpc.net/problem/10870](https://www.acmicpc.net/problem/10870)
7+
8+
# Logic
9+
##### 문제 유형 파악
10+
11+
##### 제약 조건 파악
12+
13+
##### 수도 코드 작성
14+
15+
##### 구현
16+
17+
##### 최적화 및 테스트
18+
19+
##### 개선 가능점
20+
21+
# My Code
22+
23+
## cpp
24+
25+
```cpp title="boj/10870.cpp"
26+
27+
#include <vector>
28+
int fibo(int n) {
29+
if (n <= 2) {
30+
return 1;
31+
}
32+
return fibo(n - 1) + fibo(n - 2);
33+
}
34+
35+
int dp(int n) {
36+
int before = 1;
37+
int after = 1;
38+
39+
for (int i = 3; i <= n; ++i) {
40+
int temp = before;
41+
before = after;
42+
after = temp + before;
43+
}
44+
45+
return after;
46+
}
47+
48+
void solution() {
49+
int n;
50+
cin >> n;
51+
// dp
52+
cout << dp(n);
53+
// recursive
54+
// cout << fibo(n);
55+
}
56+
57+
58+
```
59+
60+
## python
61+
62+
```python title="boj/10870.py"
63+
64+
65+
66+
def solve():
67+
return
68+
69+
70+
```
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
tags: ["ps/boj/bronze/3", "ps/boj/bronze", "cs/algorithms/mathematics/ps","cs/algorithms/implementation/ps"]
3+
---
4+
5+
# Problem
6+
- [https://www.acmicpc.net/problem/10872](https://www.acmicpc.net/problem/10872)
7+
8+
# Logic
9+
##### 문제 유형 파악
10+
11+
##### 제약 조건 파악
12+
13+
##### 수도 코드 작성
14+
15+
##### 구현
16+
17+
##### 최적화 및 테스트
18+
19+
##### 개선 가능점
20+
21+
# My Code
22+
23+
## cpp
24+
25+
```cpp title="boj/10872.cpp"
26+
27+
#include <vector>
28+
29+
int fac(int n) {
30+
if (n <= 1) {
31+
return 1;
32+
}
33+
return n * fac(n - 1);
34+
}
35+
void solution() {
36+
int n;
37+
cin >> n;
38+
cout << fac(n);
39+
}
40+
41+
42+
```
43+
44+
## python
45+
46+
```python title="boj/10872.py"
47+
48+
49+
50+
def solve():
51+
return
52+
53+
54+
```

0 commit comments

Comments
 (0)