μ± 'λꡬλ μλ£ κ΅¬μ‘°μ μκ³ λ¦¬μ¦' 1 ~ 8μ₯ 볡μ΅
νλ‘κ·Έλλ¨Έμ€ λ¬Έμ νμ΄
[TIL] 211009, [TIL] 211010, [TIL] 211013, [TIL] 211014, [TIL] 211015, [TIL] 211016
μ± 'λꡬλ μλ£ κ΅¬μ‘°μ μκ³ λ¦¬μ¦' 1 ~ 8μ₯
9μ₯λΆν° μ΄λ €μμ§λ κ±° κ°μμ(μ¬μ€μ 8μ₯λΆν°...) μ΄μ κΉμ§ λ°°μ΄ λ΄μ© 볡μ΅ν¨
1 ~ 8μ₯μ λͺ¨λ μ½λλ₯Ό λ€μ μμ±ν΄λ³Έ ν λΈλ‘κ·Έμ μ 리ν΄λ νκΈ°λ€μ λ€μ λ΄
'use strict';
/* 2μ₯. μκ³ λ¦¬μ¦μ΄ μ€μν κΉλ */
// μ λ ¬λ λ°°μ΄ μ ν κ²μ
{
function linearSearch (arr, value) {
for(let i = 0; i < arr.length; i++) {
if (arr[i] === value) {
return `${value}μ(λ) μΈλ±μ€ ${i}μ μλ€`;
}
}
return `λ°°μ΄ μμ μ°Ύκ³ μ νλ ${value}μ΄(κ°) μλ€`;
}
const arr1 = [2, 4, 6, 8];
console.log(linearSearch(arr1, 6)); // 6μ(λ) μΈλ±μ€ 2μ μλ€
}
// μ λ ¬λ λ°°μ΄ μ΄μ§ κ²μ
{
function binarySearch (arr, value) {
let upperIndex = arr.length - 1;
let lowerIndex = 0;
while (lowerIndex <= upperIndex) { // π‘ μ΄ μ‘°κ±΄μμ΄ ν¬μΈνΈ! μ²μμ low < up μ΄μλ€κ° 루νλ₯Ό λλ©΄μ κ²°κ΅ low = upμ΄ λλ€
let midIndex = (function () {
return Math.floor(upperIndex + lowerIndex);
})();
if (value < arr[midIndex]) {
upperIndex = midIndex - 1;
} else if (value > arr[midIndex]) {
lowerIndex = midIndex + 1;
} else {
return `${value}μ(λ) μΈλ±μ€ ${midIndex}μ μλ€`;
}
}
return `λ°°μ΄ μμ μ°Ύκ³ μ νλ ${value}μ΄(κ°) μλ€`;
}
const arr1 = [1, 2, 3, 4, 5, 6, 7];
const arr2 = [1, 2, 3, 4, 5, 6];
console.log(binarySearch(arr1, 3)); // 3μ(λ) μΈλ±μ€ 2μ μλ€
console.log(binarySearch(arr1, 0)); // λ°°μ΄ μμ μ°Ύκ³ μ νλ 0μ΄(κ°) μλ€
}
/* 3μ₯. λΉ
μ€ νκΈ°λ² */
// μ£Όμ΄μ§ μκ° μμμΈμ§ νλ³νλ μκ³ λ¦¬μ¦ β O(N)
{
function isPrimeNumber (num) {
for (let i = 2; i < num; i++) {
if (num % i === 0) {
return false;
}
}
return true;
}
console.log(isPrimeNumber(11)); // true
console.log(isPrimeNumber(10)); // false
}
/* 4μ₯. λΉ
μ€λ‘ μ½λ μλ μ¬λ¦¬κΈ° */
// λ²λΈ μ λ ¬ μ½λ ꡬν
// μ΅μ μ μλ리μ€(ν¨μμ μΈμλ‘ μ μ΄μ μ λ ¬λ λ°°μ΄μ΄ μ λ¬λ κ²½μ°)λ₯Ό κ³ λ €νμ§ μμ μκ³ λ¦¬μ¦
{
function bubbleSort (arr) {
let unsortedUntilIndex = arr.length - 1;
while (unsortedUntilIndex > 0) {
for (let i = 0; i < unsortedUntilIndex; i++) {
if (arr[i] > arr[i+1]) {
let temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
unsortedUntilIndex = unsortedUntilIndex - 1;
}
return arr;
}
const arr1 = [1, 2, 3, 4];
console.log(bubbleSort(arr1));
}
// λ²λΈ μ λ ¬ μ½λ ꡬν
// boolean κ°μ κ°μ§λ sorted λ³μλ₯Ό μΆκ°ν΄ if 쑰건 λΆλ§μ‘± μ while λ¬Έμ μ’
λ£ν μ μλλ‘ ν¨
{
function bubbleSort (arr) {
let unsortedUntilIndex = arr.length - 1;
let sorted = false;
while (!sorted) {
sorted = true;
for (let i = 0; i < unsortedUntilIndex; i++) {
if (arr[i] > arr[i+1]) {
let temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
sorted = false;
}
}
unsortedUntilIndex = unsortedUntilIndex - 1;
}
return arr;
}
const arr1 = [1, 2, 3, 4];
console.log(bubbleSort(arr1));
}
// λ°°μ΄μ μ€λ³΅ κ°μ΄ μλμ§ νμΈνλ λ°©λ²
// μ€μ²© 루ν νμ© o β O(N^2)
{
function hasDuplicateValue (arr) {
for (let i = 0; i < arr.length - 1; i++) {
for (let j = i + 1; j < arr.length; j++) {
if (arr[i] === arr[j]) {
return true;
}
}
}
return false;
}
const arr1 = [3, 4, 7, 3];
console.log(hasDuplicateValue(arr1)); // true
}
// μ€μ²© 루ν νμ© x β O(N)
{
function hasDuplicateValue (arr) {
const checkedValue = [];
for (let i = 0; i < arr.length; i++) {
if (checkedValue[arr[i]]) {
return true;
} else {
checkedValue[arr[i]] = 1;
}
}
return false;
}
const arr1 = [1, 5, 2, 5];
console.log(hasDuplicateValue(arr1));
}
/* 5μ₯. λΉ
μ€λ₯Ό μ¬μ©νκ±°λ μ¬μ©νμ§ μλ μ½λ μ΅μ ν */
// μ ν μ λ ¬ μ½λ ꡬν
// μ΅μ μ μλ리μ€(ν¨μμ μΈμλ‘ μ μ΄μ μ λ ¬λ λ°°μ΄μ΄ μ λ¬λ κ²½μ°)λ₯Ό κ³ λ €νμ§ μμ μκ³ λ¦¬μ¦
{
function selectionSort (arr) {
for (let i = 0; i < arr.length -1; i++) {
let minIndex = i;
for (let j = i + 1; j < arr.length; j++) {
if (arr[i] > arr[j]) {
minIndex = j; // μ΅μκ° μΈλ±μ€ κΈ°λ‘
}
}
let min = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = min;
}
return arr;
}
const arr1 = [4, 3, 2, 1];
console.log(selectionSort(arr1)); // [1, 2, 3, 4]
}
// μ ν μ λ ¬ μ½λ ꡬν
// μ΅μκ° μΈλ±μ€κ° λ³νκ° μμ λλ§ κ΅νμ΄ μΌμ΄λλλ‘ νλ€
{
function selectionSort (arr) {
for (let i = 0; i < arr.length -1; i++) {
let minIndex = i;
for (let j = i + 1; j < arr.length; j++) {
if (arr[i] > arr[j]) {
minIndex = j; // μ΅μκ° μΈλ±μ€ κΈ°λ‘
}
}
if (minIndex !== i) { // μ΅μκ° μΈλ±μ€κ° λ³νλ€λ©΄
let min = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = min;
}
}
return arr;
}
const arr1 = [4, 3, 2, 1];
console.log(selectionSort(arr1)); // [1, 2, 3, 4]
}
// 'κ°μ' λΆλ₯μ μνλ μκ³ λ¦¬μ¦ ν¨μ¨μ± λΉκ΅
// λ μμλ§λ€ νλ κ±Έλ¬ νλλ₯Ό λ½μ μ λ°°μ΄ μμ± - O(1.5N) μκ³ λ¦¬μ¦(κ²°κ΅μ O(N)μ΄κΈ΄ ν¨)
{
function makeNewArr (arr) {
const newArr = [];
for (let i = 0; i < arr.length; i++) { // Nλ²μ 룩μ
if (i % 2 === 0) {
newArr.push(arr[i]); // N/2λ²μ μ½μ
}
}
return newArr;
}
const arr1 = [5, 0, 3, 6, 2, 1];
console.log(makeNewArr(arr1)); // [5, 3, 2]
}
// 'κ°μ' λΆλ₯μ μνλ μκ³ λ¦¬μ¦ ν¨μ¨μ± λΉκ΅
// λ μμλ§λ€ νλ κ±Έλ¬ νλλ₯Ό λ½μ μ λ°°μ΄ μμ± - O(N) μκ³ λ¦¬μ¦
{
function makeNewArr (arr) {
const newArr = [];
for (let i = 0; i < arr.length; i += 2) { // N/2λ²μ 룩μ
newArr.push(arr[i]); // N/2λ²μ μ½μ
}
return newArr;
}
const arr1 = [5, 0, 3, 6, 2, 1];
console.log(makeNewArr(arr1)); // [5, 3, 2]
}
/* 6μ₯. κΈμ μ μΈ μλλ¦¬μ€ μ΅μ ν */
// π₯ μ½μ
μ λ ¬ μ½λ ꡬν π₯
{
function insertSort (arr) {
// ν¨μ€μ€λ£¨
for (let i = 1; i < arr.length; i++) {
let temp_value = arr[i]; // π‘ μμ
let position = i; // μμλ‘ μμ ν κ° temp_valueλ₯Ό μ½μ
ν μΈλ±μ€
while (position > 0 && arr[position-1] > temp_value) { // π‘ λΉκ΅
arr[position] = arr[position-1]; // π‘ μννΈ
position = position - 1; // temp_valueλ₯Ό μ½μ
ν μΈλ±μ€ κΈ°λ‘
}
if (position !== i) {
arr[position] = temp_value; // μ½μ
}
}
return arr;
}
const arr1 = [5, 4, 3, 2];
console.log(insertSort(arr1)); // [2, 3, 4, 5]
}
// λ λ°°μ΄μ κ΅μ§ν© ꡬνκΈ°
{
function intersection (arr1, arr2) {
const intersection = [];
for (let i = 0; i < arr1.length; i++) {
for (let j = 0; j < arr2.length; j++) {
if (arr1[i] === arr2[j]) {
intersection.push(arr1[i]);
break;
}
}
}
return intersection;
}
const firstArr = [3, 5, 1, 7];
const secondArr = [5, 9, 2, 3];
console.log(intersection(firstArr, secondArr)); // [3, 5]
}
/* 7μ₯. ν΄μ ν
μ΄λΈλ‘ λ§€μ° λΉ λ₯Έ 룩μ
*/
// μ μ ν¬ν κΈ°κ³ λ§λ€κΈ°
// λ°©λ² 1. λ°°μ΄ & ν΄μ ν
μ΄λΈ μ΄μ©
{
// λ°°μ΄μ ν¬νλ₯Ό μ μ₯(μ½μ
) β O(1)
const totalVotes = [];
function addVotes (candidate) {
totalVotes.push(candidate);
}
addVotes('A');
addVotes('B');
addVotes('C');
addVotes('B');
// ν΄μ ν
μ΄λΈλ‘ κ° νλ³΄λ³ ν¬ν μ μΈκΈ°(μ½κΈ°) β O(N)
function countVotes (votes) {
const checkedVotes = {};
for (let i = 0; i < votes.length; i++) { // votes μ ν κ²μ
if (checkedVotes[votes[i]]) {
checkedVotes[votes[i]]++;
} else {
checkedVotes[votes[i]] = 1;
}
}
return checkedVotes;
}
const checkedVotes = countVotes(totalVotes);
console.log(checkedVotes['B']); // 2
}
// μ μ ν¬ν κΈ°κ³ λ§λ€κΈ°
// λ°©λ² 2. ν΄μ ν
μ΄λΈ μ΄μ©
{
// μ²μλΆν° ν¬νλ₯Ό ν΄μ ν
μ΄λΈμ μ μ₯(μ½μ
) β O(1)
const totalVotes = {};
function addVotes (candidate) {
if (totalVotes[candidate]) {
totalVotes[candidate]++;
} else {
totalVotes[candidate] = 1;
}
}
addVotes('A');
addVotes('B');
addVotes('C');
addVotes('B');
// ν΄μ ν
μ΄λΈλ‘ κ° νλ³΄λ³ ν¬ν μ μΈκΈ°(μ½κΈ°) β O(1)
console.log(totalVotes['B']); // 2
}
π λ΄κ° μμ±ν νμ΄
function solution(array, commands) {
var answer = [];
for (let i = 0; i < commands.length; i++) {
const subArr = commands[i];
const unsorted = array.slice(subArr[0] - 1, subArr[1]);
const sorted = unsorted.sort((a, b) => a - b);
answer.push(sorted[subArr[2] - 1]);
}
return answer;
}
π λ€λ₯Έ μ¬λμ νμ΄
for 루ν λμ map()μ, slice() λμ filter()λ₯Ό μ΄μ©
λ±μ μμ± μ, λ±νΈκ° νμ λ€μ μ¨λ€ ex) >=, <=, +=, -=
const [a, b, c] = arr;
β μ΄λ κ² μΈ μ μλ€
function solution(array, commands) {
return commands.map(subArr => {
const [sPosition, ePosition, position] = subArr;
const newArr = array
.filter((value, index) => index >= sPosition - 1 && index <= ePosition - 1)
.sort((a, b) => a - b);
return newArr[position - 1];
});
}
π λ΄κ° μμ±ν νμ΄
function solution(answers) {
var answer = [];
const first = [];
for (let i = 0; i < answers.length / 5 + 2; i++) {
first.push(1, 2, 3, 4, 5);
}
first.length = answers.length;
let firstCount = 0;
for (let i = 0; i < answers.length; i++) {
if (first[i] === answers[i]) {
firstCount++;
}
}
const second = [];
for (let i = 0; i < answers.length / 8 + 2; i++) {
second.push(2, 1, 2, 3, 2, 4, 2, 5);
}
second.length = answers.length;
let secondCount = 0;
for (let i = 0; i < answers.length; i++) {
if (second[i] === answers[i]) {
secondCount++;
}
}
const third = [];
for (let i = 0; i < answers.length / 10 + 2; i++) {
third.push(3, 3, 1, 1, 2, 2, 4, 4, 5, 5);
}
third.length = answers.length;
let thirdCount = 0;
for (let i = 0; i < answers.length; i++) {
if (third[i] === answers[i]) {
thirdCount++;
}
}
if (firstCount > secondCount && firstCount > thirdCount) {
answer.push(1);
} else if (secondCount > firstCount && secondCount > thirdCount) {
answer.push(2);
} else if (thirdCount > firstCount && thirdCount > secondCount) {
answer.push(3);
} else if (firstCount === secondCount && firstCount > thirdCount) {
answer.push(1, 2);
} else if (firstCount === thirdCount && firstCount > secondCount) {
answer.push(1, 3);
} else if (secondCount === thirdCount && secondCount > firstCount) {
answer.push(2, 3);
} else if (firstCount === secondCount && firstCount === thirdCount && secondCount === thirdCount) {
answer.push(1, 2, 3);
}
return answer;
}
π λ€λ₯Έ μ¬λμ νμ΄
filter()λ₯Ό μ΄μ©ν΄ κ°κ°μ μ¬λλ€μ΄ λ§ν μ λ΅λ€λ§μΌλ‘ μ΄λ€μ§ λ°°μ΄λ€μ λ§λ¦
answer === first[index % first.length]
κ·Έ κΈΈμ΄λ 곧 κ°κ°μ μ¬λλ€μ΄ λ§ν μ λ΅μ κ°μλ₯Ό μλ―Έν¨
Math.max()
λ‘ μ΅λκ°μ ꡬν μ μλ€
function solution(answers) {
var answer = [];
var first = [1, 2, 3, 4, 5];
var second = [2, 1, 2, 3, 2, 4, 2, 5];
var thrid = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5];
var firstCount = answers
.filter((answer, index) => answer === first[index % first.length])
.length;
var secondCount = answers
.filter((answer, index) => answer === second[index % second.length])
.length;
var thirdCount = answers.
filter((answer, index) => answer === thrid[index % thrid.length])
.length;
var max = Math.max(firstCount, secondCount, thirdCount);
if (firstCount === max) {
answer.push(1)
} else if (secondCount === max) {
answer.push(2)
} else if (thirdCount === max) {
answer.push(3)
}
return answer;
}
π‘ λ°°μ΄μ μ΅λκ°, μ΅μκ° κ΅¬νκΈ°
const arr = [1, 3, 6, 4, 5]; /* μ΅λκ° κ΅¬νκΈ° */ // λ°©λ² 1 : reduce() μ΄μ© const max = arr.reduce((prev, curr) => prev > curr ? prev : curr); // λ°©λ² 2 : reduce() & Math.max μ΄μ© max = arr.reduce((prev, curr) => Math.max(prev, curr)); console.log(max); // 6 /* μ΅μκ° κ΅¬νκΈ° */ // λ°©λ² 1 : reduce() μ΄μ© const min = arr.reduce((prev, curr) => prev < curr ? prev : curr); // λ°©λ² 2 : reduce() & Math.min μ΄μ© min = arr.reduce((prev, curr) => Math.min(prev, curr)); console.log(min); // 1