728x90
반응형
자바스크립트로 구현한 선택정렬 알고리즘 (Selection sort in Javascript) 포스팅 썸네일 이미지

알고리즘/정렬

자바스크립트로 구현한 선택정렬 알고리즘 (Selection sort in Javascript)

선택정렬 알고리즘은 정렬 알고리즘을 공부하다보면 버블정렬만큼 익숙하게 들어본 정렬 알고리즘 중 하나일 것입니다. 버블정렬 만큼 구현하기 쉽고, 기업 코딩테스트를 하다보면 한번씩 문제로 나오곤 하는 선택정렬 알고리즘! 이번 포스팅에서는 선택정렬 알고리즘의 특징과 구현 예시에 대해서 소개하도록 하겠습니다. 선택 정렬이란? 선택정렬 알고리즘의 컨셉은 요소들이 들어갈 위치는 이미 정해져있다는 것에 있습니다. 예를 들어, 배열의 첫 번째 자리에는 배열 요소들 중 가장 작은 요소가 들어가면 되겠죠? 그리고 두 번째 자리에는 그 다음 가장 작은 요소를 선택해서 그 자리에 넣으면 됩니다. 이렇게 배열이 끝날 때까지 이 단계를 진행하면 됩니다. 선택정렬 알고리즘 예시 선택정렬 알고리즘을 좀 더 쉽게 이해하기 위해 코드를..

2020.11.19 게시됨

자바스크립트로 구현한 버블정렬 알고리즘 (Bubble sort in Javascript) 포스팅 썸네일 이미지

알고리즘/정렬

자바스크립트로 구현한 버블정렬 알고리즘 (Bubble sort in Javascript)

'버블정렬' 알고리즘은 코딩 테스트를 준비할 때 가장 처음 접하는 정렬 알고리즘 중 하나입니다. 코딩 테스트에도 한번씩 등장하는 '버블정렬' 알고리즘에 대해서 오늘 확실하게 개념을 잡고 이해해보도록 하겠습니다. 구현 예시의 언어는 자바스크립트로 구현되었으나 알고리즘의 기본 컨셉은 동일하기 때문에 자바스크립트 이외에 다른 언어로 구현하셔도 무방합니다. 버블 정렬이란? 거품 정렬이라고도 불리는 버블 정렬은 두 인접한 원소를 검사하여 정렬(오름차순, 내림차순)하는 방법입니다. (오름차순이라면 작은 값부터 큰 값 순으로 정렬이 될 것이고, 내림차순이라면 큰 값부터 작은 값 순으로 정렬이 되겠죠?) 버블정렬 알고리즘 예시 버블정렬 알고리즘을 좀 더 쉽게 이해하기 위해 코드를 보기 전에 해당 자료를 한번 보시면 좀..

2020.11.16 게시됨

자바스크립트로 피보나치 수열 구현 (fibonacci in javascript) 포스팅 썸네일 이미지

Computer science/알고리즘

자바스크립트로 피보나치 수열 구현 (fibonacci in javascript)

피보나치 수열이란? 0항을 0, 1항을 1로 두고, 두번 째 항부터는 바로 앞의 두 수를 더한 수로 놓는 것이 피보나치 수열이다. 아래와 같은 점화식을 가지고 있다. F0 = 0; F1 = 1; Fn+2 = Fn+1 + Fn;자바스크립트로 구현 임의의 숫자를 매개변수로 받아서 그 숫자만큼 피보나치 수열의 값을 나열하는 로직을 구현해보자. // 피보나치 구현 함수 export function getFibonacci(num) { let i = 0; let value1 = 0; let value2 = 1; let result = []; while (i < num) { let newValue = value1 + value2; result.push(newValue); value1 = value2; value2 =..

2020.06.25 게시됨

[LeetCode] 204. Count Primes 포스팅 썸네일 이미지

Computer science/알고리즘

[LeetCode] 204. Count Primes

Count the number of prime numbers less than a non-negative number, n. Example: Input: 10 Output: 4 Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7. 해당 문제를 풀 때, 처음에는 단순하게 접근하여 이중 for문으로 구현하려고 하였으나 Time Limit Exceeded 제약에 걸려버려 통과를 할 수 없는 상황이 발생했습니다. 해당 문제를 고민하다가 분명 시간복잡도를 줄이기 위한 좋은 아이디어가 존재할거라 생각하여 리서치한 결과 에라토스테네스의 체라는 소수 판별 방법이 존재하는 것을 알게 되었습니다. 접근 방법은 아래와 같습니다. 2부터 소수를 구..

2019.08.07 게시됨

[자바스크립트로 구현한 알고리즘] Sum of the first nth term of Series 포스팅 썸네일 이미지

Computer science/알고리즘

[자바스크립트로 구현한 알고리즘] Sum of the first nth term of Series

Description: Task: Your task is to write a function which returns the sum of following series upto nth term(parameter). Series: 1 + 1/4 + 1/7 + 1/10 + 1/13 + 1/16 +... Rules:· You need to round the answer upto 2 decimal places and return it as String.· If the given value is 0 then it should return 0.00· You will only be given Natural Numbers as arguments. Examples: SeriesSum(1) => 1 = "1" Series..

2016.10.19 게시됨

[자바스크립트로 구현한 알고리즘]  Two to One 포스팅 썸네일 이미지

Computer science/알고리즘

[자바스크립트로 구현한 알고리즘] Two to One

Description:Take 2 strings s1 and s2 including only letters from ato z. Return a new sorted string, the longest possible, containing distinct letters, - each taken only once - coming from s1 or s2.Examples:a = "xyaabbbccccdefww"b = "xxxxyyyyabklmopq"longest(a, b) -> "abcdefklmopqwxy" a = "abcdefghijklmnopqrstuvwxyz"longest(a, a) -> "abcdefghijklmnopqrstuvwxyz" 필자답안 처음에 split() 메소드를 활용해서 문자열 데이터타..

2016.10.19 게시됨

[자바스크립트로 구현한 알고리즘] Sum of two lowest positive integers 포스팅 썸네일 이미지

Computer science/알고리즘

[자바스크립트로 구현한 알고리즘] Sum of two lowest positive integers

Description: Create a function that returns the sum of the two lowest positive numbers given an array of minimum 4 integers. No floats or empty arrays will be passed. For example, when an array is passed like [19,5,42,2,77], the output should be 7. [10,343445353,3453445,3453545353453] should return 3453455. Hint: Do not modify the original array. 전과 비슷한 문제입니다. 이번 문제는 배열에서 가장 낮은 숫자 2개를 구해서 그 숫자 2..

2016.10.16 게시됨

[자바스크립트로 구현한 알고리즘] Largest Elements 포스팅 썸네일 이미지

Computer science/알고리즘

[자바스크립트로 구현한 알고리즘] Largest Elements

Write a program that outputs the top n elements from a list. Example: largest(2, [7,6,5,4,3,2,1]) // => [6,7] 배열의 정렬 메소드와 slice() 메소드를 활용해서 배열에서 제일 큰 값 n개를 뽑아내는 알고리즘입니다.정렬메소드는 문자열 기준으로 동작하므로 숫자를 배열하려면 비교함수를 인자로 넣어줘야되는데요return a - b 를 하면 오름차순return b - a 를 하면 내림차순으로 적용됩니다.

2016.10.16 게시됨

[자바스크립트로 구현한 알고리즘] Fizz / Buzz 포스팅 썸네일 이미지

Computer science/알고리즘

[자바스크립트로 구현한 알고리즘] Fizz / Buzz

Description: Write a function that takes an integer and returns an Array [A, B, C] where A is the number of multiples of 3 (but not 5) less than the given integer, B is the number of multiples of 5 (but not 3) less than the given integer and C is the number of multiples of 3 and 5 less than the given integer. For example, solution(20) should return [5, 2, 1] 3의 배수와 5의 배수 그리고 3과 5의 배수가 동시에 충족시키는 ..

2016.10.16 게시됨

[자바스크립트로 구현한 알고리즘] Sum of odd numbers 포스팅 썸네일 이미지

Computer science/알고리즘

[자바스크립트로 구현한 알고리즘] Sum of odd numbers

Given the triangle of consecutive odd numbers: 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 ... Calculate the row sums of this triangle from the row index (starting at index 1) e.g.: rowSumOddNumbers(1); // 1 rowSumOddNumbers(2); // 3 + 5 = 8 너무 어렵게 생각했다..조금만 더 쉽게 생각했으면 훨씬 쉬운 방법을 생각해낼 수 있었는데..간단하게 바로 생각할 수 있는 문제해결능력을 키우도록 노력해야겠다. 왜 이방법을 생각을 못했을꼬...

2016.10.14 게시됨

[자바스크립트로 구현한 알고리즘] Remove the minimum 포스팅 썸네일 이미지

Computer science/알고리즘

[자바스크립트로 구현한 알고리즘] Remove the minimum

Description: The museum of incredible dull things The museum of incredible dull things wants to get rid of some exhibitions. Miriam, the interior architect, comes up with a plan to remove the most boring exhibitions. She gives them a rating, and then removes the one with the lowest rating. However, just as she finished rating all exhibitions, she's off to an important fair, so she asks you to wr..

2016.10.14 게시됨

[웹폰트] 티스토리 블로그 나눔바른고딕 폰트 적용 포스팅 썸네일 이미지

블로그/티스토리 팁

[웹폰트] 티스토리 블로그 나눔바른고딕 폰트 적용

하루에 적지않은 양의 글들(주로 웹페이지)을 읽게 되는데 이따금씩 글꼴의 중요성을 실감하게 됩니다. 저는 글꼴 중에도 "나눔바른고딕"을 선호하는데요. 티스토리 블로그를 시작하면서 블로그 디폴트 폰트로 나눔바른고딕으로 설정했습니다. 웹폰트로 폰트를 적용시켰고, 적용시키는 법에 대해서 이설명하도록 하겠습니다 ! HTML / CSS를 모르는 초심자를 대상으로 쉽게 설명할 예정이니 이 글을 쭉 따라서 적용하시면 쉽게 적용하실 수 있을 겁니다. 왼쪽 중간쯤에 있는 HTML / CSS 편집을 들어가면 이렇게 HTML/ CSS을 편집할 수 있는 에디터를 보실 수 있습니다.이제 여기서 몇 가지 코드를 적용을 시켜야 되는데요. 오른쪽 HTML 문서 파일에 부분이 있을겁니다. 그 바로 위에 아래에 코드를 집어 넣도록 하겠..

2016.09.22 게시됨

[자바스크립트로 구현한 알고리즘] IQ Test 포스팅 썸네일 이미지

Computer science/알고리즘

[자바스크립트로 구현한 알고리즘] IQ Test

Bob is preparing to pass IQ test. The most frequent task in this test is to find out which one of the given numbers differs from the others. Bob observed that one number usually differs from the others in evenness. Help Bob — to check his answers, he needs a program that among the given numbers finds one that is different in evenness, and return a position of this number. ! Keep in mind that you..

2016.09.21 게시됨

[자바스크립트로 구현한 알고리즘] Find the next perfect square! 포스팅 썸네일 이미지

Computer science/알고리즘

[자바스크립트로 구현한 알고리즘] Find the next perfect square!

Description: You might know some pretty large perfect squares. But what about the NEXT one? Complete the findNextSquare method that finds the next integral perfect square after the one passed as a parameter. Recall that an integral perfect square is an integer n such that sqrt(n) is also an integer. If the parameter is itself not a perfect square, than -1 should be returned. You may assume the p..

2016.09.21 게시됨

[자바스크립트] 객체 (Javascript - Object) 포스팅 썸네일 이미지

개발/Javascript

[자바스크립트] 객체 (Javascript - Object)

이번 글은 자바스크립트의 핵심이기도 하지만, 이해하기 어려운 객체에 대해서 다뤄보려고 합니다.자바스크립트는 객체 기반의 스크립트 언어이며 자바스크립트를 이루고 있는 거의 모든 것들이 객체로 존재합니다. 그렇기 때문에 자바스크립트를 공부할 때 꼭 정복해야 해야 하는 것이 바로 “객체”이기도 합니다. 한마디로 객체는 자신의 정보를 가지고 있는 독립적인 주체입니다.객체를 이해하려면 '프로퍼티'와 '메소드'를 이해하고 있어야합니다.객체란 것은 결국 포장을 이루는 말이고 실제 객체를 완성시켜주는 것은 프로퍼티와 메소드라는 구성요소이기 때문입니다.이해하기 쉽게 아래 그림으로 객체에 대해 쉽게 설명해 보겠습니다. 우리가 객체라고 부르는 것은 바로 컴퓨터 케이스에 해당하는데요. 실제 컴퓨터를 구성하는 것은 메모리, 그..

2016.09.21 게시됨

[자바스크립트로 구현한 알고리즘] Isograms 포스팅 썸네일 이미지

Computer science/알고리즘

[자바스크립트로 구현한 알고리즘] Isograms

Description: An isogram is a word that has no repeating letters, consecutive or non-consecutive. Implement a function that determines whether a string that contains only letters is an isogram. Assume the empty string is an isogram. Ignore letter case. isIsogram( "Dermatoglyphics" ) == true isIsogram( "aba" ) == false isIsogram( "moOse" ) == false // -- ignore letter case 같은 문자가 나오면 false를 return..

2016.09.20 게시됨

[자바스크립트로 구현한 알고리즘] Credit Card Mask 포스팅 썸네일 이미지

Computer science/알고리즘

[자바스크립트로 구현한 알고리즘] Credit Card Mask

Description: Usually when you buy something, you're asked whether your credit card number, phone number or answer to your most secret question is still correct. However, since someone could look over your shoulder, you don't want that shown on your screen. Instead, we mask it. Your task is to write a function maskify, which changes all but the last four characters into '#'. Examples maskify("455..

2016.09.20 게시됨

[자바스크립트로 구현한 알고리즘] Descending Order 포스팅 썸네일 이미지

Computer science/알고리즘

[자바스크립트로 구현한 알고리즘] Descending Order

Description:Your task is to make a function that can take any non-negative integer as a argument and return it with it's digits in descending order. Descending order means that you take the highest digit and place the next highest digit immediately after it.Examples:Input: 145263 Output: 654321Input: 1254859723 Output: 9875543221 Number 데이터 타입을 String 데이터 타입으로 변환 후 배열로 정렬하고 내림차순으로 정렬한 다음 다시 Numb..

2016.09.20 게시됨

728x90
반응형