🐥
みんなのコンピュータサイエンス
Chapter 1 Basis
Chapter 2 Complexity
- Time complexity
- Space complexity
Chapter 3 Strategy
- Iteration
- Recursion
- Brute force
- Backtruck
- Heuristic
heuristic.js
function greedy(wCst) {
const items = [{w:10, v:3}, {w:15, v:2}, {w:5, v:1}, {w:20, v:5}];
let wTotal = 0;
let vTotal = 0;
items.sort((a, b)=>(b.v / b.w) - (a.v / a.w));
for (const item of items) {
if (wTotal + item.w <= wCst) {
wTotal += item.w;
vTotal += item.v;
}
}
return [wTotal, vTotal];
}
- Divide and conquer
mergeSort.js
function mergeSort(arr){
if (arr.length == 1) return arr;
const mid = Math.floor(arr.length/2);
const left = mergeSort(arr.slice(0, mid));
const right = mergeSort(arr.slice(mid));
return merge(left, right);
}
function merge(left, right) {
let result = [];
let i = 0;
let j = 0;
while(i<left.length && j<right.length) {
if (left[i]<right[j]) {
result.push(left[i]);
i++;
}
else {
result.push(right[j]);
j++;
}
}
return [...result, ...left.slice(i), ...right.slice(j)]
}
- Dynamically
DynamicPrograming.js
function knapsackDP(items, capacity) {
const n = items.length;
// dp[i][w] = i番目までで重さw以内のときの最大価値
const dp = Array.from({ length: n + 1 }, () => Array(capacity + 1).fill(0));
for (let i = 1; i <= n; i++) {
const { w, v } = items[i - 1];
for (let weight = 0; weight <= capacity; weight++) {
if (w > weight) {
// アイテムが入らない場合は前と同じ
dp[i][weight] = dp[i - 1][weight];
} else {
// 入れない場合と入れた場合の最大値を取る
dp[i][weight] = Math.max(
dp[i - 1][weight], // 入れない
dp[i - 1][weight - w] + v // 入れる
);
}
}
}
return dp[n][capacity]; // 最大価値
}
- Bound
Chapter 4 Data
-
Data Type
- Stack
- Queue
- Priority queue
priorityQueue.jsimport TinyQueue from 'tinyqueue'; const queue = new TinyQueue([], (a, b) => a.priority - b.priority); queue.push({ value: 'A', priority: 2 }); queue.push({ value: 'B', priority: 1 }); console.log(queue.pop()); // { value: 'B', priority: 1 }
- List
- Sorted list
- Map
const map = new Map(); map.set('apple', 100);
- Set
const set = new Set([1, 2, 3, 2, 1]); console.log(set); // Set {1, 2, 3}
-
Data Structure
- Array
- Linked list
- Double linked list
- Tree
- Binary Search tree
- Self-balancing binary tree
- Binary heap
- Graph
- Hash function
Chapter 5 Algorithm
-
Sort
- Selection Sort
- Merge Sort
- Bubble Sort
- Quick Sort
-
Search
- Sequential Search
- Binary Search
-
Graph
- Depth First Search
- Breadth First Search
- Graph coloring
- Dijkstra algorithm
- Bidirectional Search
- Pagerank algorithm
- Operations research
- Linear programming problem
Chapter 6 Database
- Relational model
2. Schema migration
3. Sql
4. Transaction - No sql
- Distributed database
Chapter 7 Computer
- Architecture
- Memory
- CPU
- Processor
- Compiler
- Layer
- Resistor
- L1 cache
- L2 cache
- L3 cache
- RAM
- Disk
Chapter 8 Language
Discussion