Skip to content

Commit 855edab

Browse files
committed
Quartz sync: Aug 30, 2025, 11:44 PM
1 parent eea96ad commit 855edab

File tree

81 files changed

+7112
-341
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+7112
-341
lines changed

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

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
description:
33
aliases:
44
created: 2025-05-18
5-
modified: 2025-08-21
5+
modified: 2025-08-30
66
tags:
77
- field/computer
88
- format/article
@@ -40,41 +40,44 @@ references:
4040
return qsort(left) + [pivot] + qsort(right)
4141

4242
## Lomuto Partition
43+
- when `left_index < right_index`
4344
- 최초 조건
44-
- left_index = -1 (pivot 보다 같거나 작은 값 )
45-
- right_index = 0 (looping 용 index)
46-
- left_index < right_index
47-
- 맨 오른쪽 요소를 pivot으로 삼는다
45+
- `left_index = low -1` (pivot 보다 같거나 작은 값 )
46+
- `right_index = low` (looping 용 index)
47+
- 맨 오른쪽 요소를 `pivot`으로 삼는다
4848
- loop
49-
- right_index 요소와 pivot 비교
50-
- 같거나 작으면 `left_index++` + `swap(arr[left_index], arr[right_index])`
51-
- 크면 pass
52-
- `right_index++`
53-
- right_index가 pivot 위치 전까지 오면 `swap(arr[left_index + 1], arr[pivot])`
54-
- pivot은 제자리에 가게됨
55-
- left_side, right_side 재귀로 quick_sort 호출
49+
- `if cmp(right_index, pivot)`
50+
- true -> `left_index++` + `swap(arr[left_index], arr[right_index])`
51+
- cmp`<` 라면
52+
- 현재 left_index는 피벗 보다 큰 값
53+
- 현재 right_index는 피벗 보다 작은 값, 고로 swap
54+
- `right_index++` loop
55+
- `right_index``pivot` 위치 전까지 오면(loop가 끝나면) `swap(arr[left_index + 1], arr[pivot])`
56+
- `pivot`은 순서상 제자리에 가게됨
57+
- `left_side, right_side` 재귀로 `quick_sort` 호출
5658
- ```cpp
57-
void quickSort(int arr[], int low, int high){
58-
if (low < high){
59-
int pivot_index = partition(arr, low, high);
59+
template<typename T, typename Compare>
60+
int partition(vector<T>& arr, int low, int high, Compare cmp) {
61+
int pivot = high;
62+
int left = low - 1;
6063

61-
quickSort(arr, low, pivot_index - 1);
62-
quickSort(arr, pivot_index + 1, high)
63-
}
64+
for (int right = low; right < high; ++right) {
65+
if (cmp(arr[right], arr[pivot])) {
66+
left++;
67+
swap(arr[left], arr[right]);
68+
}
69+
}
70+
left++;
71+
swap(arr[left], arr[pivot]);
72+
return left;
6473
}
6574

66-
int partition(int arr[], int low, int high){
67-
int pivot = arr[high];
68-
int left_index = (low - 1);
69-
70-
for (int right_index = low; right_index < high; right_index++){
71-
if (arr[right_index] <= pivot){
72-
left_index++;
73-
swap(&arr[left_index], &arr[right_index]);
74-
}
75-
}
76-
swap(&arr[left_index + 1], &arr[high]);
77-
return (left_index + 1);
78-
}
75+
template<typename T, typename Compare>
76+
void q_sort(vector<T>& arr, int low, int high, Compare cmp) {
77+
if (low < high) {
78+
int pivot = partition(arr, low, high, cmp);
7979

80-
80+
q_sort(arr, low, pivot - 1, cmp);
81+
q_sort(arr, pivot + 1, high, cmp);
82+
}
83+
}

content/Computer Science/1 Foundations & Theory/Algorithms/ps/boj/10162/10162.md

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,59 @@ tags: ["ps/boj/bronze/3", "ps/boj/bronze", "cs/algorithms/mathematics/ps","cs/al
55
# Problem
66
- [https://www.acmicpc.net/problem/10162](https://www.acmicpc.net/problem/10162)
77

8+
<br/>
9+
810
# Logic
9-
##### 문제 유형 파악
1011

11-
##### 제약 조건 파악
12+
### *분석*
13+
- 문제 유형 (알고리즘...)
14+
- 제약 조건 (인풋 크기, 예외 값, 시공간 복잡도...)
1215

13-
##### 수도 코드 작성
16+
### *설계*
17+
1. 알고리즘 선택
18+
2. 자료구조 선택
19+
3. 수도 코드 작성
20+
4. 정합판단
21+
- `1 ~ 3`과정으로 도출된 로직이 문제를 해결하는가
22+
- 그렇다 -> **구현**
23+
- 잘 모르겠다 -> **구현**
24+
- 아니다 -> 1번부터 다시 점검
1425

15-
##### 구현
26+
### *구현*
27+
- 로직 검증
1628

17-
##### 최적화 및 테스트
29+
### *테스트*
30+
- 예외 케이스 고려
1831

19-
##### 개선 가능점
32+
<br/>
2033

2134
# My Code
22-
2335
## cpp
24-
2536
```cpp title="boj/10162.cpp"
26-
37+
// https://www.acmicpc.net/problem/10162
38+
// https://codeyoma.github.io/Computer-Science/1-Foundations--and--Theory/Algorithms/ps/boj/10162/10162
39+
#include <iostream>
40+
using namespace std;
41+
42+
#ifdef LOCAL
43+
# define LOG clog
44+
#else
45+
struct nullstream : ostream {
46+
nullstream()
47+
: ostream(nullptr) {}
48+
};
49+
nullstream LOG;
50+
#endif
51+
52+
//--------------------------------------------------------------------------------------------------
53+
54+
#define MAX (1234567891)
55+
#define MIN (-1234567891)
56+
57+
#include <iostream>
2758
#include <vector>
28-
void solution() {
59+
60+
int main() {
2961
vector<int> timer = { 300, 60, 10 };
3062
vector<int> push_count(3, 0);
3163

@@ -43,18 +75,4 @@ void solution() {
4375
cout << push_count[0] << " " << push_count[1] << " " << push_count[2];
4476
}
4577
}
46-
47-
48-
```
49-
50-
## python
51-
52-
```python title="boj/10162.py"
53-
54-
55-
56-
def solve():
57-
return
58-
59-
6078
```
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
---
2+
tags: ["ps/boj/silver/3", "ps/boj/silver", "cs/algorithms/data-structures/ps","cs/algorithms/deque/ps"]
3+
---
4+
5+
# Problem
6+
- [https://www.acmicpc.net/problem/1021](https://www.acmicpc.net/problem/1021)
7+
8+
<br/>
9+
10+
# Logic
11+
12+
### *분석*
13+
- 문제 유형 (알고리즘...)
14+
- 제약 조건 (인풋 크기, 예외 값, 시공간 복잡도...)
15+
16+
### *설계*
17+
1. 알고리즘 선택
18+
2. 자료구조 선택
19+
3. 수도 코드 작성
20+
4. 정합판단
21+
- `1 ~ 3`과정으로 도출된 로직이 문제를 해결하는가
22+
- 그렇다 -> **구현**
23+
- 잘 모르겠다 -> **구현**
24+
- 아니다 -> 1번부터 다시 점검
25+
26+
### *구현*
27+
- 로직 검증
28+
29+
### *테스트*
30+
- 예외 케이스 고려
31+
32+
<br/>
33+
34+
# My Code
35+
## cpp
36+
```cpp title="boj/1021.cpp"
37+
// https://www.acmicpc.net/problem/1021
38+
// https://codeyoma.github.io/Computer-Science/1-Foundations--and--Theory/Algorithms/ps/boj/1021/1021
39+
#include <iostream>
40+
using namespace std;
41+
42+
#ifdef LOCAL
43+
# define LOG clog
44+
#else
45+
struct nullstream : ostream {
46+
nullstream()
47+
: ostream(nullptr) {}
48+
};
49+
nullstream LOG;
50+
#endif
51+
52+
//--------------------------------------------------------------------------------------------------
53+
54+
#define MAX (1234567891)
55+
#define MIN (-1234567891)
56+
57+
#include <deque>
58+
#include <iostream>
59+
60+
int main() {
61+
// logic
62+
// 양 끝단 기준으로 추정 숫자가 더 가까운 위치로 이동하기
63+
64+
int n, t;
65+
66+
cin >> n >> t;
67+
68+
deque<int> q;
69+
deque<int> t_q;
70+
71+
for (int i = 1; i <= n; ++i) {
72+
q.push_back(i);
73+
}
74+
75+
while (t--) {
76+
int temp;
77+
cin >> temp;
78+
t_q.push_back(temp);
79+
}
80+
81+
int answer = 0;
82+
83+
for (const int& target: t_q) {
84+
int index;
85+
86+
// find index
87+
for (int i = 0; i < q.size(); ++i) {
88+
if (q[i] == target) {
89+
index = i;
90+
}
91+
}
92+
93+
if (index <= (q.size() / 2)) {
94+
for (int i = 0; i < index; ++i) {
95+
q.push_back(q.front());
96+
q.pop_front();
97+
}
98+
answer += index;
99+
} else {
100+
// 앞에서만 뽑을 수 있음 (q.size() - (index + 1)) 아 아닌 이유
101+
int reverse_index = q.size() - index;
102+
for (int i = 0; i < reverse_index; ++i) {
103+
q.push_front(q.back());
104+
q.pop_back();
105+
}
106+
answer += reverse_index;
107+
}
108+
q.pop_front();
109+
}
110+
cout << answer;
111+
}
112+
113+
```
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
---
2+
tags: ["ps/boj/bronze/3", "ps/boj/bronze", "cs/algorithms/mathematics/ps","cs/algorithms/implementation/ps","cs/algorithms/arithmetic/ps"]
3+
---
4+
5+
# Problem
6+
- [https://www.acmicpc.net/problem/10250](https://www.acmicpc.net/problem/10250)
7+
8+
<br/>
9+
10+
# Logic
11+
12+
### *분석*
13+
- 문제 유형 (알고리즘...)
14+
- 제약 조건 (인풋 크기, 예외 값, 시공간 복잡도...)
15+
16+
### *설계*
17+
1. 알고리즘 선택
18+
2. 자료구조 선택
19+
3. 수도 코드 작성
20+
4. 정합판단
21+
- `1 ~ 3`과정으로 도출된 로직이 문제를 해결하는가
22+
- 그렇다 -> **구현**
23+
- 잘 모르겠다 -> **구현**
24+
- 아니다 -> 1번부터 다시 점검
25+
26+
### *구현*
27+
- 로직 검증
28+
29+
### *테스트*
30+
- 예외 케이스 고려
31+
32+
<br/>
33+
34+
# My Code
35+
## cpp
36+
```cpp title="boj/10250.cpp"
37+
// https://www.acmicpc.net/problem/10250
38+
// https://codeyoma.github.io/Computer-Science/1-Foundations--and--Theory/Algorithms/ps/boj/10250/10250
39+
#include <iostream>
40+
using namespace std;
41+
42+
#ifdef LOCAL
43+
# define LOG clog
44+
#else
45+
struct nullstream : ostream {
46+
nullstream()
47+
: ostream(nullptr) {}
48+
};
49+
nullstream LOG;
50+
#endif
51+
52+
void fast_io() {
53+
ios_base::sync_with_stdio(false);
54+
cin.tie(nullptr);
55+
}
56+
57+
//--------------------------------------------------------------------------------------------------
58+
59+
#define MAX (1234567891)
60+
#define MIN (-1234567891)
61+
62+
#include <iostream>
63+
64+
int main() {
65+
fast_io();
66+
67+
// logic
68+
// 왼쪽열의 아래부터 채워지는 형태
69+
// 몫과 나머지로 가능
70+
71+
int t;
72+
cin >> t;
73+
74+
while (t--) {
75+
int h, w, n;
76+
cin >> h >> w >> n;
77+
78+
int f = n % h ? n % h : h;
79+
int c = ((n - 1) / h) + 1;
80+
81+
cout << f << ((c < 10) ? "0" : "") << c << "\n";
82+
}
83+
}
84+
85+
```

0 commit comments

Comments
 (0)