mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-21 11:13:00 +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,62 @@
|
||||
/**
|
||||
* File: complexity_exercises.java
|
||||
* Created Time: 2026-08-18
|
||||
* Author: Hello Algo Team
|
||||
*/
|
||||
|
||||
package chapter_computational_complexity;
|
||||
|
||||
public class complexity_exercises {
|
||||
/* 반복으로 합 구하기 */
|
||||
static int sumIter(int n) {
|
||||
int res = 0;
|
||||
for (int i = 1; i <= n; i++) {
|
||||
res += i;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 재귀로 합 구하기 */
|
||||
static int sumRecur(int n) {
|
||||
if (n == 1) {
|
||||
return 1;
|
||||
}
|
||||
return n + sumRecur(n - 1);
|
||||
}
|
||||
|
||||
/* 선형 차수 반복문 */
|
||||
static int linearLoop(int n) {
|
||||
int res = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
res += i;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 제곱 차수 반복문 */
|
||||
static 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;
|
||||
}
|
||||
|
||||
/* 로그 차수 반복문 */
|
||||
static int logarithmicLoop(int n) {
|
||||
while (n > 1) {
|
||||
n /= 2;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
/**
|
||||
* File: iteration.java
|
||||
* Created Time: 2023-08-24
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_computational_complexity;
|
||||
|
||||
public class iteration {
|
||||
/* for 반복문 */
|
||||
static int forLoop(int n) {
|
||||
int res = 0;
|
||||
// 반복문으로 1, 2, ..., n-1, n의 합을 구한다
|
||||
for (int i = 1; i <= n; i++) {
|
||||
res += i;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* while 반복문 */
|
||||
static int whileLoop(int n) {
|
||||
int res = 0;
|
||||
int i = 1; // 조건 변수를 초기화한다
|
||||
// 반복문으로 1, 2, ..., n-1, n의 합을 구한다
|
||||
while (i <= n) {
|
||||
res += i;
|
||||
i++; // 조건 변수를 갱신한다
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* while 반복문(두 번 갱신) */
|
||||
static int whileLoopII(int n) {
|
||||
int res = 0;
|
||||
int i = 1; // 조건 변수를 초기화한다
|
||||
// 반복문으로 1, 4, 10, ...의 합을 구한다
|
||||
while (i <= n) {
|
||||
res += i;
|
||||
// 조건 변수를 갱신한다
|
||||
i++;
|
||||
i *= 2;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 이중 for 반복문 */
|
||||
static String nestedForLoop(int n) {
|
||||
StringBuilder res = new StringBuilder();
|
||||
// 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.append("(" + i + ", " + j + "), ");
|
||||
}
|
||||
}
|
||||
return res.toString();
|
||||
}
|
||||
|
||||
/* 실행 코드 */
|
||||
public static void main(String[] args) {
|
||||
int n = 5;
|
||||
int res;
|
||||
|
||||
res = forLoop(n);
|
||||
System.out.println("\nfor 반복문으로 구한 합 res = " + res);
|
||||
|
||||
res = whileLoop(n);
|
||||
System.out.println("\nwhile 반복문으로 구한 합 res = " + res);
|
||||
|
||||
res = whileLoopII(n);
|
||||
System.out.println("\nwhile 반복문(두 번 갱신)으로 구한 합 res = " + res);
|
||||
|
||||
String resStr = nestedForLoop(n);
|
||||
System.out.println("\n이중 for 반복문의 순회 결과 " + resStr);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
/**
|
||||
* File: recursion.java
|
||||
* Created Time: 2023-08-24
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_computational_complexity;
|
||||
|
||||
import java.util.Stack;
|
||||
|
||||
public class recursion {
|
||||
/* 재귀 */
|
||||
static int recur(int n) {
|
||||
// 종료 조건
|
||||
if (n == 1)
|
||||
return 1;
|
||||
// 내려가기: 재귀 호출
|
||||
int res = recur(n - 1);
|
||||
// 돌아오기: 결과 반환
|
||||
return n + res;
|
||||
}
|
||||
|
||||
/* 반복으로 재귀 구현 */
|
||||
static int forLoopRecur(int n) {
|
||||
// 명시적인 스택을 사용하여 시스템 호출 스택을 모방한다
|
||||
Stack<Integer> stack = new Stack<>();
|
||||
int res = 0;
|
||||
// 내려가기: 재귀 호출
|
||||
for (int i = n; i > 0; i--) {
|
||||
// 스택에 삽입하여 내려가는 과정을 모방한다
|
||||
stack.push(i);
|
||||
}
|
||||
// 돌아오기: 결과 반환
|
||||
while (!stack.isEmpty()) {
|
||||
// 스택에서 꺼내어 돌아오는 과정을 모방한다
|
||||
res += stack.pop();
|
||||
}
|
||||
// res = 1+2+3+...+n
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 꼬리 재귀 */
|
||||
static int tailRecur(int n, int res) {
|
||||
// 종료 조건
|
||||
if (n == 0)
|
||||
return res;
|
||||
// 꼬리 재귀 호출
|
||||
return tailRecur(n - 1, res + n);
|
||||
}
|
||||
|
||||
/* 피보나치 수열: 재귀 */
|
||||
static 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;
|
||||
}
|
||||
|
||||
/* 실행 코드 */
|
||||
public static void main(String[] args) {
|
||||
int n = 5;
|
||||
int res;
|
||||
|
||||
res = recur(n);
|
||||
System.out.println("\n재귀 함수로 구한 합 res = " + res);
|
||||
|
||||
res = forLoopRecur(n);
|
||||
System.out.println("\n반복으로 재귀를 구현하여 구한 합 res = " + res);
|
||||
|
||||
res = tailRecur(n, 0);
|
||||
System.out.println("\n꼬리 재귀 함수로 구한 합 res = " + res);
|
||||
|
||||
res = fib(n);
|
||||
System.out.println("\n피보나치 수열의 항 번호: " + n + " ; 해당 항의 값: " + res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
/**
|
||||
* File: space_complexity.java
|
||||
* Created Time: 2022-11-25
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_computational_complexity;
|
||||
|
||||
import utils.*;
|
||||
import java.util.*;
|
||||
|
||||
public class space_complexity {
|
||||
/* 함수 */
|
||||
static int function() {
|
||||
// 몇 가지 연산을 수행한다
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 상수 차수 */
|
||||
static void constant(int n) {
|
||||
// 상수, 변수, 객체는 O(1) 공간을 차지한다
|
||||
final int a = 0;
|
||||
int b = 0;
|
||||
int[] nums = new int[10000];
|
||||
ListNode node = new ListNode(0);
|
||||
// 반복문 안의 변수는 O(1) 공간을 차지한다
|
||||
for (int i = 0; i < n; i++) {
|
||||
int c = 0;
|
||||
}
|
||||
// 반복문 안의 함수는 O(1) 공간을 차지한다
|
||||
for (int i = 0; i < n; i++) {
|
||||
function();
|
||||
}
|
||||
}
|
||||
|
||||
/* 선형 차수 */
|
||||
static void linear(int n) {
|
||||
// 길이가 n인 배열은 O(n) 공간을 차지한다
|
||||
int[] nums = new int[n];
|
||||
// 길이가 n인 리스트는 O(n) 공간을 차지한다
|
||||
List<ListNode> nodes = new ArrayList<>();
|
||||
for (int i = 0; i < n; i++) {
|
||||
nodes.add(new ListNode(i));
|
||||
}
|
||||
// 길이가 n인 해시 테이블은 O(n) 공간을 차지한다
|
||||
Map<Integer, String> map = new HashMap<>();
|
||||
for (int i = 0; i < n; i++) {
|
||||
map.put(i, String.valueOf(i));
|
||||
}
|
||||
}
|
||||
|
||||
/* 선형 차수(재귀 구현) */
|
||||
static void linearRecur(int n) {
|
||||
System.out.println("재귀 n = " + n);
|
||||
if (n == 1)
|
||||
return;
|
||||
linearRecur(n - 1);
|
||||
}
|
||||
|
||||
/* 제곱 차수 */
|
||||
static void quadratic(int n) {
|
||||
// 행렬은 O(n^2) 공간을 차지한다
|
||||
int[][] numMatrix = new int[n][n];
|
||||
// 이차원 리스트는 O(n^2) 공간을 차지한다
|
||||
List<List<Integer>> numList = new ArrayList<>();
|
||||
for (int i = 0; i < n; i++) {
|
||||
List<Integer> tmp = new ArrayList<>();
|
||||
for (int j = 0; j < n; j++) {
|
||||
tmp.add(0);
|
||||
}
|
||||
numList.add(tmp);
|
||||
}
|
||||
}
|
||||
|
||||
/* 제곱 차수(재귀 구현) */
|
||||
static int quadraticRecur(int n) {
|
||||
if (n <= 0)
|
||||
return 0;
|
||||
// 배열 nums의 길이는 n, n-1, ..., 2, 1이다
|
||||
int[] nums = new int[n];
|
||||
System.out.println("재귀 n = " + n + " 일 때 nums의 길이 = " + nums.length);
|
||||
return quadraticRecur(n - 1);
|
||||
}
|
||||
|
||||
/* 지수 차수(포화 이진 트리 구성) */
|
||||
static TreeNode buildTree(int n) {
|
||||
if (n == 0)
|
||||
return null;
|
||||
TreeNode root = new TreeNode(0);
|
||||
root.left = buildTree(n - 1);
|
||||
root.right = buildTree(n - 1);
|
||||
return root;
|
||||
}
|
||||
|
||||
/* 실행 코드 */
|
||||
public static void main(String[] args) {
|
||||
int n = 5;
|
||||
// 상수 차수
|
||||
constant(n);
|
||||
// 선형 차수
|
||||
linear(n);
|
||||
linearRecur(n);
|
||||
// 제곱 차수
|
||||
quadratic(n);
|
||||
quadraticRecur(n);
|
||||
// 지수 차수
|
||||
TreeNode root = buildTree(n);
|
||||
PrintUtil.printTree(root);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,167 @@
|
||||
/**
|
||||
* File: time_complexity.java
|
||||
* Created Time: 2022-11-25
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_computational_complexity;
|
||||
|
||||
public class time_complexity {
|
||||
/* 상수 차수 */
|
||||
static int constant(int n) {
|
||||
int count = 0;
|
||||
int size = 100000;
|
||||
for (int i = 0; i < size; i++)
|
||||
count++;
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 선형 차수 */
|
||||
static int linear(int n) {
|
||||
int count = 0;
|
||||
for (int i = 0; i < n; i++)
|
||||
count++;
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 선형 차수(배열 순회) */
|
||||
static int arrayTraversal(int[] nums) {
|
||||
int count = 0;
|
||||
// 반복 횟수는 배열 길이에 비례한다
|
||||
for (int num : nums) {
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 제곱 차수 */
|
||||
static 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;
|
||||
}
|
||||
|
||||
/* 제곱 차수(버블 정렬) */
|
||||
static int bubbleSort(int[] nums) {
|
||||
int count = 0; // 카운터
|
||||
// 바깥쪽 반복문: 정렬되지 않은 구간은 [0, i]이다
|
||||
for (int i = nums.length - 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;
|
||||
}
|
||||
|
||||
/* 지수 차수(반복문 구현) */
|
||||
static 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;
|
||||
}
|
||||
|
||||
/* 지수 차수(재귀 구현) */
|
||||
static int expRecur(int n) {
|
||||
if (n == 1)
|
||||
return 1;
|
||||
return expRecur(n - 1) + expRecur(n - 1) + 1;
|
||||
}
|
||||
|
||||
/* 로그 차수(반복문 구현) */
|
||||
static int logarithmic(int n) {
|
||||
int count = 0;
|
||||
while (n > 1) {
|
||||
n = n / 2;
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 로그 차수(재귀 구현) */
|
||||
static int logRecur(int n) {
|
||||
if (n <= 1)
|
||||
return 0;
|
||||
return logRecur(n / 2) + 1;
|
||||
}
|
||||
|
||||
/* 선형 로그 차수 */
|
||||
static 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;
|
||||
}
|
||||
|
||||
/* 팩토리얼 차수(재귀 구현) */
|
||||
static 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;
|
||||
}
|
||||
|
||||
/* 실행 코드 */
|
||||
public static void main(String[] args) {
|
||||
// n을 바꾸어 실행하면서 각 복잡도에서 연산 횟수가 어떻게 변하는지 살펴볼 수 있다
|
||||
int n = 8;
|
||||
System.out.println("입력 데이터 크기 n = " + n);
|
||||
|
||||
int count = constant(n);
|
||||
System.out.println("상수 차수의 연산 횟수 = " + count);
|
||||
|
||||
count = linear(n);
|
||||
System.out.println("선형 차수의 연산 횟수 = " + count);
|
||||
count = arrayTraversal(new int[n]);
|
||||
System.out.println("선형 차수(배열 순회)의 연산 횟수 = " + count);
|
||||
|
||||
count = quadratic(n);
|
||||
System.out.println("제곱 차수의 연산 횟수 = " + count);
|
||||
int[] nums = new int[n];
|
||||
for (int i = 0; i < n; i++)
|
||||
nums[i] = n - i; // [n,n-1,...,2,1]
|
||||
count = bubbleSort(nums);
|
||||
System.out.println("제곱 차수(버블 정렬)의 연산 횟수 = " + count);
|
||||
|
||||
count = exponential(n);
|
||||
System.out.println("지수 차수(반복문 구현)의 연산 횟수 = " + count);
|
||||
count = expRecur(n);
|
||||
System.out.println("지수 차수(재귀 구현)의 연산 횟수 = " + count);
|
||||
|
||||
count = logarithmic(n);
|
||||
System.out.println("로그 차수(반복문 구현)의 연산 횟수 = " + count);
|
||||
count = logRecur(n);
|
||||
System.out.println("로그 차수(재귀 구현)의 연산 횟수 = " + count);
|
||||
|
||||
count = linearLogRecur(n);
|
||||
System.out.println("선형 로그 차수(재귀 구현)의 연산 횟수 = " + count);
|
||||
|
||||
count = factorialRecur(n);
|
||||
System.out.println("팩토리얼 차수(재귀 구현)의 연산 횟수 = " + count);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/**
|
||||
* File: worst_best_time_complexity.java
|
||||
* Created Time: 2022-11-25
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_computational_complexity;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class worst_best_time_complexity {
|
||||
/* 원소가 { 1, 2, ..., n }이며 순서가 섞인 배열 생성 */
|
||||
static int[] randomNumbers(int n) {
|
||||
Integer[] nums = new Integer[n];
|
||||
// 배열 nums = { 1, 2, 3, ..., n }을 생성한다
|
||||
for (int i = 0; i < n; i++) {
|
||||
nums[i] = i + 1;
|
||||
}
|
||||
// 배열 원소를 무작위로 섞는다
|
||||
Collections.shuffle(Arrays.asList(nums));
|
||||
// Integer[] -> int[]
|
||||
int[] res = new int[n];
|
||||
for (int i = 0; i < n; i++) {
|
||||
res[i] = nums[i];
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 배열 nums에서 숫자 1이 있는 인덱스 탐색 */
|
||||
static int findOne(int[] nums) {
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
// 원소 1이 배열 맨 앞에 있으면 최선 시간 복잡도 O(1)에 도달한다
|
||||
// 원소 1이 배열 맨 뒤에 있으면 최악 시간 복잡도 O(n)에 도달한다
|
||||
if (nums[i] == 1)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* 실행 코드 */
|
||||
public static void main(String[] args) {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
int n = 100;
|
||||
int[] nums = randomNumbers(n);
|
||||
int index = findOne(nums);
|
||||
System.out.println("\n배열 [ 1, 2, ..., n ]을 섞은 결과 = " + Arrays.toString(nums));
|
||||
System.out.println("숫자 1의 인덱스 " + index);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user