mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-05 04:32:19 +08:00
feat(ko): add the Korean online edition (#2006)
* feat(ko): add the complete Korean online edition * Use UTF-8 for Python CI output on Windows * Remove print-book promotion from the Korean site * fix(ko): preserve emphasis before Korean particles
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
add_executable(iteration iteration.cpp)
|
||||
add_executable(recursion recursion.cpp)
|
||||
add_executable(space_complexity space_complexity.cpp)
|
||||
add_executable(time_complexity time_complexity.cpp)
|
||||
add_executable(worst_best_time_complexity worst_best_time_complexity.cpp)
|
||||
add_executable(complexity_exercises complexity_exercises.cpp)
|
||||
@@ -0,0 +1,61 @@
|
||||
/**
|
||||
* File: complexity_exercises.cpp
|
||||
* Created Time: 2026-08-18
|
||||
* Author: Hello Algo Team
|
||||
*/
|
||||
|
||||
#include <cassert>
|
||||
|
||||
/* 반복으로 합 구하기 */
|
||||
int sumIter(int n) {
|
||||
int res = 0;
|
||||
for (int i = 1; i <= n; ++i) {
|
||||
res += i;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 재귀로 합 구하기 */
|
||||
int sumRecur(int n) {
|
||||
if (n == 1) {
|
||||
return 1;
|
||||
}
|
||||
return n + sumRecur(n - 1);
|
||||
}
|
||||
|
||||
/* 선형 차수 반복문 */
|
||||
int linearLoop(int n) {
|
||||
int res = 0;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
res += i;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 제곱 차수 반복문 */
|
||||
int quadraticLoop(int n) {
|
||||
int res = 0;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
for (int j = i; j < n; ++j) {
|
||||
res += j;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 로그 차수 반복문 */
|
||||
int logarithmicLoop(int n) {
|
||||
while (n > 1) {
|
||||
n /= 2;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
int main() {
|
||||
assert(sumIter(1) == 1 && sumRecur(1) == 1);
|
||||
assert(sumIter(4) == 10 && sumRecur(4) == 10);
|
||||
assert(linearLoop(4) == 6);
|
||||
assert(quadraticLoop(4) == 20);
|
||||
assert(logarithmicLoop(4) == 1 && logarithmicLoop(5) == 1);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
/**
|
||||
* File: iteration.cpp
|
||||
* Created Time: 2023-08-24
|
||||
* Author: krahets ([email protected])
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* for 반복문 */
|
||||
int forLoop(int n) {
|
||||
int res = 0;
|
||||
// 반복문으로 1, 2, ..., n-1, n의 합을 구한다
|
||||
for (int i = 1; i <= n; ++i) {
|
||||
res += i;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* while 반복문 */
|
||||
int whileLoop(int n) {
|
||||
int res = 0;
|
||||
int i = 1; // 조건 변수를 초기화한다
|
||||
// 반복문으로 1, 2, ..., n-1, n의 합을 구한다
|
||||
while (i <= n) {
|
||||
res += i;
|
||||
i++; // 조건 변수를 갱신한다
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* while 반복문(두 번 갱신) */
|
||||
int whileLoopII(int n) {
|
||||
int res = 0;
|
||||
int i = 1; // 조건 변수를 초기화한다
|
||||
// 반복문으로 1, 4, 10, ...의 합을 구한다
|
||||
while (i <= n) {
|
||||
res += i;
|
||||
// 조건 변수를 갱신한다
|
||||
i++;
|
||||
i *= 2;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 이중 for 반복문 */
|
||||
string nestedForLoop(int n) {
|
||||
ostringstream res;
|
||||
// i = 1, 2, ..., n-1, n을 반복한다
|
||||
for (int i = 1; i <= n; ++i) {
|
||||
// j = 1, 2, ..., n-1, n을 반복한다
|
||||
for (int j = 1; j <= n; ++j) {
|
||||
res << "(" << i << ", " << j << "), ";
|
||||
}
|
||||
}
|
||||
return res.str();
|
||||
}
|
||||
|
||||
/* 실행 코드 */
|
||||
int main() {
|
||||
int n = 5;
|
||||
int res;
|
||||
|
||||
res = forLoop(n);
|
||||
cout << "\nfor 반복문으로 구한 합 res = " << res << endl;
|
||||
|
||||
res = whileLoop(n);
|
||||
cout << "\nwhile 반복문으로 구한 합 res = " << res << endl;
|
||||
|
||||
res = whileLoopII(n);
|
||||
cout << "\nwhile 반복문(두 번 갱신)으로 구한 합 res = " << res << endl;
|
||||
|
||||
string resStr = nestedForLoop(n);
|
||||
cout << "\n이중 for 반복문의 순회 결과 " << resStr << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
/**
|
||||
* File: recursion.cpp
|
||||
* Created Time: 2023-08-24
|
||||
* Author: krahets ([email protected])
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* 재귀 */
|
||||
int recur(int n) {
|
||||
// 종료 조건
|
||||
if (n == 1)
|
||||
return 1;
|
||||
// 내려가기: 재귀 호출
|
||||
int res = recur(n - 1);
|
||||
// 돌아오기: 결과 반환
|
||||
return n + res;
|
||||
}
|
||||
|
||||
/* 반복으로 재귀 구현 */
|
||||
int forLoopRecur(int n) {
|
||||
// 명시적인 스택을 사용하여 시스템 호출 스택을 모방한다
|
||||
stack<int> stack;
|
||||
int res = 0;
|
||||
// 내려가기: 재귀 호출
|
||||
for (int i = n; i > 0; i--) {
|
||||
// 스택에 삽입하여 내려가는 과정을 모방한다
|
||||
stack.push(i);
|
||||
}
|
||||
// 돌아오기: 결과 반환
|
||||
while (!stack.empty()) {
|
||||
// 스택에서 꺼내어 돌아오는 과정을 모방한다
|
||||
res += stack.top();
|
||||
stack.pop();
|
||||
}
|
||||
// res = 1+2+3+...+n
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 꼬리 재귀 */
|
||||
int tailRecur(int n, int res) {
|
||||
// 종료 조건
|
||||
if (n == 0)
|
||||
return res;
|
||||
// 꼬리 재귀 호출
|
||||
return tailRecur(n - 1, res + n);
|
||||
}
|
||||
|
||||
/* 피보나치 수열: 재귀 */
|
||||
int fib(int n) {
|
||||
// 종료 조건 f(1) = 0, f(2) = 1
|
||||
if (n == 1 || n == 2)
|
||||
return n - 1;
|
||||
// 재귀 호출 f(n) = f(n-1) + f(n-2)
|
||||
int res = fib(n - 1) + fib(n - 2);
|
||||
// 결과 f(n)을 반환한다
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 실행 코드 */
|
||||
int main() {
|
||||
int n = 5;
|
||||
int res;
|
||||
|
||||
res = recur(n);
|
||||
cout << "\n재귀 함수로 구한 합 res = " << res << endl;
|
||||
|
||||
res = forLoopRecur(n);
|
||||
cout << "\n반복으로 재귀를 구현하여 구한 합 res = " << res << endl;
|
||||
|
||||
res = tailRecur(n, 0);
|
||||
cout << "\n꼬리 재귀 함수로 구한 합 res = " << res << endl;
|
||||
|
||||
res = fib(n);
|
||||
cout << "\n피보나치 수열의 항 번호: " << n << " ; 해당 항의 값: " << res << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
/**
|
||||
* File: space_complexity.cpp
|
||||
* Created Time: 2022-11-25
|
||||
* Author: krahets ([email protected])
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* 함수 */
|
||||
int func() {
|
||||
// 몇 가지 연산을 수행한다
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 상수 차수 */
|
||||
void constant(int n) {
|
||||
// 상수, 변수, 객체는 O(1) 공간을 차지한다
|
||||
const int a = 0;
|
||||
int b = 0;
|
||||
vector<int> nums(10000);
|
||||
ListNode node(0);
|
||||
// 반복문 안의 변수는 O(1) 공간을 차지한다
|
||||
for (int i = 0; i < n; i++) {
|
||||
int c = 0;
|
||||
}
|
||||
// 반복문 안의 함수는 O(1) 공간을 차지한다
|
||||
for (int i = 0; i < n; i++) {
|
||||
func();
|
||||
}
|
||||
}
|
||||
|
||||
/* 선형 차수 */
|
||||
void linear(int n) {
|
||||
// 길이가 n인 배열은 O(n) 공간을 차지한다
|
||||
vector<int> nums(n);
|
||||
// 길이가 n인 리스트는 O(n) 공간을 차지한다
|
||||
vector<ListNode> nodes;
|
||||
for (int i = 0; i < n; i++) {
|
||||
nodes.push_back(ListNode(i));
|
||||
}
|
||||
// 길이가 n인 해시 테이블은 O(n) 공간을 차지한다
|
||||
unordered_map<int, string> map;
|
||||
for (int i = 0; i < n; i++) {
|
||||
map[i] = to_string(i);
|
||||
}
|
||||
}
|
||||
|
||||
/* 선형 차수(재귀 구현) */
|
||||
void linearRecur(int n) {
|
||||
cout << "재귀 n = " << n << endl;
|
||||
if (n == 1)
|
||||
return;
|
||||
linearRecur(n - 1);
|
||||
}
|
||||
|
||||
/* 제곱 차수 */
|
||||
void quadratic(int n) {
|
||||
// 이차원 리스트는 O(n^2) 공간을 차지한다
|
||||
vector<vector<int>> numMatrix;
|
||||
for (int i = 0; i < n; i++) {
|
||||
vector<int> tmp;
|
||||
for (int j = 0; j < n; j++) {
|
||||
tmp.push_back(0);
|
||||
}
|
||||
numMatrix.push_back(tmp);
|
||||
}
|
||||
}
|
||||
|
||||
/* 제곱 차수(재귀 구현) */
|
||||
int quadraticRecur(int n) {
|
||||
if (n <= 0)
|
||||
return 0;
|
||||
vector<int> nums(n);
|
||||
cout << "재귀 n = " << n << " 일 때 nums의 길이 = " << nums.size() << endl;
|
||||
return quadraticRecur(n - 1);
|
||||
}
|
||||
|
||||
/* 지수 차수(포화 이진 트리 구성) */
|
||||
TreeNode *buildTree(int n) {
|
||||
if (n == 0)
|
||||
return nullptr;
|
||||
TreeNode *root = new TreeNode(0);
|
||||
root->left = buildTree(n - 1);
|
||||
root->right = buildTree(n - 1);
|
||||
return root;
|
||||
}
|
||||
|
||||
/* 실행 코드 */
|
||||
int main() {
|
||||
int n = 5;
|
||||
// 상수 차수
|
||||
constant(n);
|
||||
// 선형 차수
|
||||
linear(n);
|
||||
linearRecur(n);
|
||||
// 제곱 차수
|
||||
quadratic(n);
|
||||
quadraticRecur(n);
|
||||
// 지수 차수
|
||||
TreeNode *root = buildTree(n);
|
||||
printTree(root);
|
||||
|
||||
// 메모리 해제
|
||||
freeMemoryTree(root);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
/**
|
||||
* File: time_complexity.cpp
|
||||
* Created Time: 2022-11-25
|
||||
* Author: krahets ([email protected])
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* 상수 차수 */
|
||||
int constant(int n) {
|
||||
int count = 0;
|
||||
int size = 100000;
|
||||
for (int i = 0; i < size; i++)
|
||||
count++;
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 선형 차수 */
|
||||
int linear(int n) {
|
||||
int count = 0;
|
||||
for (int i = 0; i < n; i++)
|
||||
count++;
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 선형 차수(배열 순회) */
|
||||
int arrayTraversal(vector<int> &nums) {
|
||||
int count = 0;
|
||||
// 반복 횟수는 배열 길이에 비례한다
|
||||
for (int num : nums) {
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 제곱 차수 */
|
||||
int quadratic(int n) {
|
||||
int count = 0;
|
||||
// 반복 횟수는 데이터 크기 n의 제곱에 비례한다
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 제곱 차수(버블 정렬) */
|
||||
int bubbleSort(vector<int> &nums) {
|
||||
int count = 0; // 카운터
|
||||
// 바깥쪽 반복문: 정렬되지 않은 구간은 [0, i]이다
|
||||
for (int i = nums.size() - 1; i > 0; i--) {
|
||||
// 안쪽 반복문: 정렬되지 않은 구간 [0, i]의 최댓값을 교환하여 이 구간의 맨 오른쪽으로 옮긴다
|
||||
for (int j = 0; j < i; j++) {
|
||||
if (nums[j] > nums[j + 1]) {
|
||||
// nums[j]와 nums[j + 1]을 교환한다
|
||||
int tmp = nums[j];
|
||||
nums[j] = nums[j + 1];
|
||||
nums[j + 1] = tmp;
|
||||
count += 3; // 원소 교환에는 단위 연산 3회가 포함된다
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 지수 차수(반복문 구현) */
|
||||
int exponential(int n) {
|
||||
int count = 0, base = 1;
|
||||
// 세포가 매번 둘로 나뉘어 수열 1, 2, 4, 8, ..., 2^(n-1)을 이룬다
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < base; j++) {
|
||||
count++;
|
||||
}
|
||||
base *= 2;
|
||||
}
|
||||
// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 지수 차수(재귀 구현) */
|
||||
int expRecur(int n) {
|
||||
if (n == 1)
|
||||
return 1;
|
||||
return expRecur(n - 1) + expRecur(n - 1) + 1;
|
||||
}
|
||||
|
||||
/* 로그 차수(반복문 구현) */
|
||||
int logarithmic(int n) {
|
||||
int count = 0;
|
||||
while (n > 1) {
|
||||
n = n / 2;
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 로그 차수(재귀 구현) */
|
||||
int logRecur(int n) {
|
||||
if (n <= 1)
|
||||
return 0;
|
||||
return logRecur(n / 2) + 1;
|
||||
}
|
||||
|
||||
/* 선형 로그 차수 */
|
||||
int linearLogRecur(int n) {
|
||||
if (n <= 1)
|
||||
return 1;
|
||||
int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);
|
||||
for (int i = 0; i < n; i++) {
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 팩토리얼 차수(재귀 구현) */
|
||||
int factorialRecur(int n) {
|
||||
if (n == 0)
|
||||
return 1;
|
||||
int count = 0;
|
||||
// 하나에서 n개로 분열한다
|
||||
for (int i = 0; i < n; i++) {
|
||||
count += factorialRecur(n - 1);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 실행 코드 */
|
||||
int main() {
|
||||
// n을 바꾸어 실행하면서 각 복잡도에서 연산 횟수가 어떻게 변하는지 살펴볼 수 있다
|
||||
int n = 8;
|
||||
cout << "입력 데이터 크기 n = " << n << endl;
|
||||
|
||||
int count = constant(n);
|
||||
cout << "상수 차수의 연산 횟수 = " << count << endl;
|
||||
|
||||
count = linear(n);
|
||||
cout << "선형 차수의 연산 횟수 = " << count << endl;
|
||||
vector<int> arr(n);
|
||||
count = arrayTraversal(arr);
|
||||
cout << "선형 차수(배열 순회)의 연산 횟수 = " << count << endl;
|
||||
|
||||
count = quadratic(n);
|
||||
cout << "제곱 차수의 연산 횟수 = " << count << endl;
|
||||
vector<int> nums(n);
|
||||
for (int i = 0; i < n; i++)
|
||||
nums[i] = n - i; // [n,n-1,...,2,1]
|
||||
count = bubbleSort(nums);
|
||||
cout << "제곱 차수(버블 정렬)의 연산 횟수 = " << count << endl;
|
||||
|
||||
count = exponential(n);
|
||||
cout << "지수 차수(반복문 구현)의 연산 횟수 = " << count << endl;
|
||||
count = expRecur(n);
|
||||
cout << "지수 차수(재귀 구현)의 연산 횟수 = " << count << endl;
|
||||
|
||||
count = logarithmic(n);
|
||||
cout << "로그 차수(반복문 구현)의 연산 횟수 = " << count << endl;
|
||||
count = logRecur(n);
|
||||
cout << "로그 차수(재귀 구현)의 연산 횟수 = " << count << endl;
|
||||
|
||||
count = linearLogRecur(n);
|
||||
cout << "선형 로그 차수(재귀 구현)의 연산 횟수 = " << count << endl;
|
||||
|
||||
count = factorialRecur(n);
|
||||
cout << "팩토리얼 차수(재귀 구현)의 연산 횟수 = " << count << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/**
|
||||
* File: worst_best_time_complexity.cpp
|
||||
* Created Time: 2022-11-25
|
||||
* Author: krahets ([email protected])
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* 원소가 { 1, 2, ..., n }이며 순서가 섞인 배열 생성 */
|
||||
vector<int> randomNumbers(int n) {
|
||||
vector<int> nums(n);
|
||||
// 배열 nums = { 1, 2, 3, ..., n }을 생성한다
|
||||
for (int i = 0; i < n; i++) {
|
||||
nums[i] = i + 1;
|
||||
}
|
||||
// 시스템 시간으로 난수 시드를 생성한다
|
||||
unsigned seed = chrono::system_clock::now().time_since_epoch().count();
|
||||
// 배열 원소를 무작위로 섞는다
|
||||
shuffle(nums.begin(), nums.end(), default_random_engine(seed));
|
||||
return nums;
|
||||
}
|
||||
|
||||
/* 배열 nums에서 숫자 1이 있는 인덱스 탐색 */
|
||||
int findOne(vector<int> &nums) {
|
||||
for (int i = 0; i < nums.size(); i++) {
|
||||
// 원소 1이 배열 맨 앞에 있으면 최선 시간 복잡도 O(1)에 도달한다
|
||||
// 원소 1이 배열 맨 뒤에 있으면 최악 시간 복잡도 O(n)에 도달한다
|
||||
if (nums[i] == 1)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* 실행 코드 */
|
||||
int main() {
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
int n = 100;
|
||||
vector<int> nums = randomNumbers(n);
|
||||
int index = findOne(nums);
|
||||
cout << "\n배열 [ 1, 2, ..., n ]을 섞은 결과 = ";
|
||||
printVector(nums);
|
||||
cout << "숫자 1의 인덱스 " << index << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user