알고리즘
-
[개념] BigO 표기법알고리즘 2023. 11. 6. 22:55
빅오표기법이란? 우리는 알고리즘 문제를 풀던 코드를 짜던 무엇이 가장 효율적인지 시간상 짧게 걸리는지를 확인 할 필요가 있다. ( 성능과 직접적으로 연결되는 부분이기 때문 ) 빅오표기법 상수시간 알고리즘(Constant Time Algorithm) - 가장 이상적인 알고리즘으로써 실행되는 횟수가 상수값으로 나타나서 그 횟수만큼만 실행을 하기때문에 Input되는 데이터가 어떻든 같은 시간안에 처리하게 된다. const array = [1, 2, 3, 4, 5] const fnc = (array) => { console.log(array[0]) } 만약 아래 코드와 같이 출력하는 값이 여러개로 늘어난다하더라도 상수는 제외하고 나타내기 때문에 결국은 O(1)으로 표기된다. const array = [1, 2,..
-
[개념] 검색알고리즘 (선형 VS 이진)알고리즘 2023. 11. 4. 11:44
검색 알고리즘 우리는 프로젝트를 하던 알고리즘 문제를 풀던 어떠한 값을 찾기위해서 배열 또는 객체를 만들어서 그 값을 찾는 로직을 구현을 하는 모습을 자주 보인다. 그렇다면 이러한 검색을 하기위해서 가장 최적의 방법은 무엇일까? 에 대한 고민을 한번쯤은 해봐야 한다. 선형검색 알고리즘(linear search) 아래 이미지는 선형검색 알고리즘의 실행 과정을 보여준다. 선형검색 알고리즘은 0번째 index부터의 비교를 통해서 값을 찾을 수 있다 장점 값을 추가할 때 강점이 있음 간단하고 직관적인 코드 (걍 반복문 돌리면 됨) 단점 크기가 커질수록 step이 비례적으로 증가하기때문에 점진적으로 비효율적으로 되어버림 최악의수로는 배열의 마지막 index에 위치한 값을 찾는 것이다. function linear..
-
[개념] 유클리드 호제법을 통한 최대공약수 + 최소공배수알고리즘 2023. 11. 2. 11:01
유클리드 호제법이란? 최대공약수를 구하는데 가장 흔하게 사용되는 알고리즘으로써 chatGPT나 구글링을 좀 해봤는데 거의 대부분 이 방법을 사용하고 있음 말로써 설명하자면 비교대상인 두 수 중 큰수(a)와 작은수 (b)가 있을 떄 a를 b로 나눈 수를 또 b로 나누고 반복하다가 나머지가 0이 되었을 때 그떄의 a 값이 최대공약수라는 내용임 사용방법 흔히 재귀함수를 통해서 사용하는 방법과 while반복문을 사용하는 방법 2가지가 있음 시간복잡도 상 크게 다르지 않음 O(log(min(a,b)))을 가지기에 필자는 좀더 짧고 이해하기 쉬운 재귀함수를 추천 1. 재귀함수 const gcd = (a, b) => { if (b === 0) return a; return gcd(b, a % b); } 2. whil..
-
[프로그래머스 알고리즘] 2중반복문알고리즘 2023. 6. 22. 09:57
K번째 수 function solution(array, commands) { let answer = [] for(const i of commands) { let newArr = [] for (let j = i[0] - 1; j a-b)[i[2] - 1]) } return answer } 다른사람 답 function solution(array, commands) { return commands.map(command => { const [sPosition, ePosition, position] = command const newArray = array .filter((value,..