2차원 리스트 90도 회전
def rotate_a_matrix_by_90_degree(a):
n = len(a)
m = len(a[0])
result = [[0]*n for _ in range(m)]
for i in range(n):
for j in range(m):
result[j][n -i -1] = a[i][j]
return result
function rotate(a){
let n = a.length;
let m = a[0].length;
let result = Array.from(Array(n), () => new Array(m));
for(let i=0; i<n; i++){
for(let j=0; j<m; j++){
result[j][n -i -1] = a[i][j];
}
}
return result;
}
조합
const getCombinations = function (arr, length) {
const result = [];
if (length === 1) return arr.map((value) => [value]);
arr.forEach((fixed, idx, origin) => {
if (arr.length - idx < length) return;
const rest = origin.slice(idx + 1);
const combinations = getCombinations(rest, length - 1);
const attached = combinations.map((combination) => [fixed, ...combination]);
result.push(...attached);
});
return result;
}
const exampleArr = [1,2,3,4];
getCombinations(exampleArr, 3);
function getCombinations(arr, length) {
let result = [];
let temp = Array.from({length:length},()=>0);
function DFS(n, s) {
if (n === length) {
result.push(temp.slice());
} else {
for (let i = s; i < arr.length; i++) {
temp[n] = arr[i];
DFS(n+1, i+1);
}
}
}
DFS(0, 0);
console.log(result);
}
getCombinations([1,2,3,4],3);
중복 조합
function getCombinationsRe(arr, length) {
const result = [];
if (length === 1) return arr.map((value) => [value]);
arr.forEach((fixed, idx, origin) => {
const rest = origin.slice(idx);
const combinations = getCombinationsRe(rest, length - 1);
const attached = combinations.map((combination) => [fixed, ...combination]);
result.push(...attached);
});
return result;
}
getCombinationsRe([1,2,3,4],3);
순열
const getPermutations= function (arr, length) {
const result = [];
if (length === 1) return arr.map((value) => [value]);
arr.forEach((fixed, idx, origin) => {
const rest = [...origin.slice(0, idx), ...origin.slice(idx+1)]
const permutations = getPermutations(rest, length - 1);
const attached = permutations.map((permutation) => [fixed, ...permutation]);
result.push(...attached);
});
return result;
};
getPermutations([1,2,3], 2);
중복 순열
function getPermutationsRep(arr, length) {
let result = [];
let temp = [];
function DFS(n) {
if (n === length) {
result.push([...temp]);
} else {
for (let i = 0; i < arr.length; i++) {
temp[n] = arr[i];
DFS(n + 1);
}
}
}
DFS(0);
return result;
}
getPermutationsRep([1,2,3],2);
BFS(Breadth First Search)
const graph = {
A: ["B", "C"],
B: ["A", "D"],
C: ["A", "G", "H", "I"],
D: ["B", "E", "F"],
E: ["D"],
F: ["D"],
G: ["C"],
H: ["C"],
I: ["C", "J"],
J: ["I"]
};
const bfs = (graph, startNode) => {
let visited = [];
let needVisit = [];
needVisit.push(startNode);
while (needVisit.length !== 0) {
const node = needVisit.shift();
if (!visited.includes(node)) {
visited.push(node);
needVisit = [...needVisit, ...graph[node]];
}
}
return visited;
};
bfs(graph, "A");
DFS(Depth First Search)
const graph = {
A: ["B", "C"],
B: ["A", "D"],
C: ["A", "G", "H", "I"],
D: ["B", "E", "F"],
E: ["D"],
F: ["D"],
G: ["C"],
H: ["C"],
I: ["C", "J"],
J: ["I"],
};
const dfs = (graph, startNode) => {
let needVisitStack = [];
let visitedQueue = [];
needVisitStack.push(startNode);
while (needVisitStack.length !== 0) {
const node = needVisitStack.pop();
if (!visitedQueue.includes(node)) {
visitedQueue.push(node);
needVisitStack = [...needVisitStack, ...graph[node]];
}
}
return visitedQueue;
};
dfs(graph, "A");
유클리드 호제법(최대공약수, 최소공배수)
function getGcd(a, b) {
const mod = a % b;
if (mod === 0) {
return b;
}
return getGcd(b, mod);
}
function getLcm(a, b) {
const gcd = getGcd(a, b);
const lcm = (a * b) / gcd;
return lcm;
}
소수 판별
function isPrime(num){
if(num === 1) return false;
if(num === 2) return true;
for(let i=2; i<=Math.floor(Math.sqrt(num)); i++){
if(num % i === 0){
return false;
}
}
return true;
}
function countPrime(num){
let arr = Array(num+1).fill(true).fill(false, 0, 2);
for(let i=2; i*i <= num; i++){
if(arr[i]){
for(let j=i*i; j<=num; j+=i){
arr[j] = false;
}
}
}
return arr.filter(e => e).length;
}
이진 탐색(재귀 사용)
const binarySearch = (arr, target, start, end) => {
if ( start > end ) {
return;
}
const mid = Math.floor(( start + end ) / 2);
if ( arr[mid] === target ) {
return mid + 1;
} else if ( arr[mid] > target ) {
return binarySearch(arr, target, start, mid-1);
} else {
return binarySearch(arr, target, mid+1, end);
}
};
퀵 정렬
function quickSort(arr, start, end) {
if (start >= end) return;
let pivot = start;
let left = start + 1;
let right = end;
let temp;
while (left <= right) {
while (left <= end && arr[left] <= arr[pivot]) {
left++;
}
while (right > start && arr[right] >= arr[pivot]) {
right--;
}
if (left > right) {
temp = arr[right];
arr[right] = arr[pivot];
arr[pivot] = temp;
} else {
temp = arr[right];
arr[right] = arr[left];
arr[left] = temp;
}
}
quickSort(arr, start, right - 1);
quickSort(arr, right + 1, end);
return arr;
}