Skip to content

Commit eea96ad

Browse files
committed
Quartz sync: Aug 22, 2025, 9:12 PM
1 parent 04dc90f commit eea96ad

File tree

58 files changed

+4745
-40
lines changed

Some content is hidden

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

58 files changed

+4745
-40
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
---
2-
created: 2023/11/03 23:49:16
3-
modified: 2025/5/01 18:43:38
2+
description:
3+
aliases:
4+
created: 2025-05-18
5+
modified: 2025-08-21
46
tags:
57
- field/computer
68
- format/article
79
references:
810
- https://www.youtube.com/watch?v=PgBzjlCcFvc
911
---
12+
1013
# Choice of Pivot
1114
- 처음, 마지막 요소 피벗
1215
- 랜덤 요소 피벗
@@ -64,7 +67,7 @@ references:
6467
int pivot = arr[high];
6568
int left_index = (low - 1);
6669

67-
for (right_index = low; right_index <= high - 1; right_index++){
70+
for (int right_index = low; right_index < high; right_index++){
6871
if (arr[right_index] <= pivot){
6972
left_index++;
7073
swap(&arr[left_index], &arr[right_index]);

content/Computer Science/1 Foundations & Theory/Algorithms/math/소수 판별법.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,24 @@ references:
4545
- 밀러-라빈 테스트
4646
- 여러 숫자 검증
4747
- 에라토스테네스의 체
48-
- 특정 범위까지 `2의 배수... n의 배수` false 판별한 brutal force 배열을 사용
48+
- 특정 범위까지 `2의 배수... n의 배수` false 판별한 brutal force 배열을 사용
49+
- ```cpp
50+
vector<bool> t(b + 1, true);
51+
t[1] = false;
52+
53+
for (int i = 2; i <= sqrt(b); ++i) {
54+
if (!t[i]) {
55+
continue;
56+
}
57+
for (int j = i * i; j <= b; j += i) {
58+
t[j] = false;
59+
}
60+
}
61+
- `sqrt(n)` 까지만 비교하는 이유
62+
- 합성수 라면 $a * a < a * b = n; a <= \sqrt n$
63+
- 작은 값으로 나눠지는 경우를 확인
64+
- a 작은 값, b 큰 값
65+
- `j = i * i`로 시작하는 이유
66+
- 이전 i의 가능값 `(2, 3, 4, ... i)`에서 배수로 이미 걸러졌기 때문
67+
- `i = 5` 인경우
68+
- `i * 1, i * 2, i * 3, i * 4`가 이미 이전에 판단완료
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
---
2+
tags: ["ps/boj/silver/4", "ps/boj/silver", "cs/algorithms/mathematics/ps","cs/algorithms/greedy/ps","cs/algorithms/sorting/ps"]
3+
---
4+
5+
# Problem
6+
- [https://www.acmicpc.net/problem/1026](https://www.acmicpc.net/problem/1026)
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/1026.cpp"
37+
// https://www.acmicpc.net/problem/1026
38+
#include <iostream>
39+
using namespace std;
40+
41+
#ifdef LOCAL
42+
# define LOG clog
43+
#else
44+
struct nullstream : ostream {
45+
nullstream()
46+
: ostream(nullptr) {}
47+
};
48+
nullstream LOG;
49+
#endif
50+
51+
//--------------------------------------------------------------------------------------------------
52+
53+
#define MAX (1234567891)
54+
#define MIN (-1234567891)
55+
56+
#include <algorithm>
57+
#include <iostream>
58+
#include <vector>
59+
60+
int partition(vector<int>& a, int l, int h, function<bool(int, int)> cmp) {
61+
int p = h;
62+
int left = l - 1;
63+
64+
for (int right = l; right < h; ++right) {
65+
if (cmp(a[right], a[p])) {
66+
left++;
67+
swap(a[left], a[right]);
68+
}
69+
}
70+
71+
left++;
72+
swap(a[left], a[p]);
73+
return left;
74+
}
75+
76+
void qsort(vector<int>& a, int l, int h, function<bool(int, int)> cmp) {
77+
if (l < h) {
78+
int p = partition(a, l, h, cmp);
79+
qsort(a, l, p - 1, cmp);
80+
qsort(a, p + 1, h, cmp);
81+
}
82+
}
83+
84+
int main() {
85+
// logic
86+
87+
int n;
88+
cin >> n;
89+
90+
vector<int> a(n), b(n);
91+
92+
for (int i = 0; i < n; ++i) {
93+
cin >> a[i];
94+
}
95+
for (int i = 0; i < n; ++i) {
96+
cin >> b[i];
97+
}
98+
99+
// a는 내림차, b는 오름차 (혹은 그 반대)
100+
qsort(a, 0, n - 1, [](int a, int b) { return a <= b; });
101+
qsort(b, 0, n - 1, [](int a, int b) { return a >= b; });
102+
103+
int answer = 0;
104+
for (int i = 0; i < n; ++i) {
105+
answer += (a[i] * b[i]);
106+
}
107+
108+
cout << answer;
109+
}
110+
111+
```
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
---
2+
tags: ["ps/boj/silver/4", "ps/boj/silver", "cs/algorithms/mathematics/ps","cs/algorithms/greedy/ps","cs/algorithms/string/ps","cs/algorithms/sorting/ps","cs/algorithms/number-theory/ps"]
3+
---
4+
5+
# Problem
6+
- [https://www.acmicpc.net/problem/10610](https://www.acmicpc.net/problem/10610)
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/10610.cpp"
37+
// https://www.acmicpc.net/problem/10610
38+
#include <iostream>
39+
using namespace std;
40+
41+
#ifdef LOCAL
42+
# define LOG clog
43+
#else
44+
struct nullstream : ostream {
45+
nullstream()
46+
: ostream(nullptr) {}
47+
};
48+
nullstream LOG;
49+
#endif
50+
51+
//--------------------------------------------------------------------------------------------------
52+
53+
#define MAX (1234567891)
54+
#define MIN (-1234567891)
55+
56+
#include <algorithm>
57+
#include <iostream>
58+
#include <string>
59+
#include <vector>
60+
61+
int partition(vector<int>& a, int l, int h) {
62+
int p = h;
63+
int left = l - 1;
64+
65+
for (int right = l; right < h; ++right) {
66+
if (a[right] > a[p]) {
67+
left++;
68+
swap(a[left], a[right]);
69+
}
70+
}
71+
left++;
72+
swap(a[left], a[p]);
73+
return left;
74+
}
75+
76+
void sort(vector<int>& a, int l, int h) {
77+
if (l < h) {
78+
int p = partition(a, l, h);
79+
80+
sort(a, l, p - 1);
81+
sort(a, p + 1, h);
82+
}
83+
}
84+
85+
int main() {
86+
// logic
87+
string input;
88+
89+
cin >> input;
90+
91+
vector<int> arr;
92+
93+
long long check_sum = 0;
94+
bool has_zero = false;
95+
96+
for (size_t i = 0; i < input.size(); ++i) {
97+
int temp_num = input[i] - '0';
98+
check_sum += (temp_num);
99+
arr.push_back(temp_num);
100+
if (temp_num == 0) {
101+
has_zero = true;
102+
}
103+
}
104+
if (!has_zero || check_sum % 3 != 0) {
105+
LOG << check_sum;
106+
cout << -1;
107+
} else {
108+
sort(arr, 0, arr.size() - 1);
109+
for (const auto& i: arr) {
110+
cout << i;
111+
}
112+
}
113+
}
114+
115+
```
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
---
2+
tags: ["ps/boj/bronze/1", "ps/boj/bronze", "cs/algorithms/implementation/ps","cs/algorithms/string/ps"]
3+
---
4+
5+
# Problem
6+
- [https://www.acmicpc.net/problem/10798](https://www.acmicpc.net/problem/10798)
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/10798.cpp"
37+
// https://www.acmicpc.net/problem/10798
38+
// https://codeyoma.github.io/Computer-Science/1-Foundations--and--Theory/Algorithms/ps/boj/10798/10798
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>
58+
#include <string>
59+
#include <vector>
60+
61+
int main() {
62+
// logic
63+
64+
int n = 5, max_len = 0;
65+
vector<string> list;
66+
for (int i = 0; i < n; ++i) {
67+
string temp;
68+
cin >> temp;
69+
70+
list.push_back(temp);
71+
if (max_len < temp.length()) {
72+
max_len = temp.length();
73+
}
74+
}
75+
76+
for (int i = 0; i < max_len; ++i) {
77+
for (int j = 0; j < n; ++j) {
78+
if (list[j].length() - 1 < i) {
79+
continue;
80+
}
81+
cout << list[j][i];
82+
}
83+
}
84+
}
85+
86+
```

0 commit comments

Comments
 (0)