Notice
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |
Tags
- 맥린이
- typeScript
- TS
- 웹개발종합반
- 스파르타코딩클럽
- 프론트엔드
- 항해99
- 사전준비
- 알pdf #파일탐색기미리보기안될때
- ChatGPT
- 챗GPT
- Ai
- ReactNative
- 알고리즘기초주차
- 코린이
- Expo
- 팀워크최고
- D반8조
- 7기
- REACT
- 달리기반
- Programmers
- 프로그래머스
- NotionAI
- 멍친구
- TDD
- rn
- 필수강의
- 실전프로젝트
- 리액트
Archives
- Today
- Total
FrontEnd :-)
Divide and Conquer 본문
Udemy - 【한글자막】 JavaScript 알고리즘 & 자료구조 마스터클래스
Divide and Conquer 분할과 정복 패턴
This pattern involves dividing a data set into smaller chunks and then repeating a process with a subset of data. This pattern can tremendously decrease time complexity
#1. Divide and Conquer
Given a sorted array of integers, write a function called search, that accepts a value and returns the index where the value passed to the function is located. If the value is not found, return -1
search([1,2,3,4,5,6],4) // 3
search([1,2,3,4,5,6],6) // 5
search([1,2,3,4,5,6],11) // -1
//Linear Search
//Time Complexity O(N)
function search(arr, val){
for(let i = 0; i < arr.length; i++){
if(arr[i] === val){
return i;
}
}
return -1;
}
||
REFACTOR
V
//Time Complexity - Log(N) - Binary Search!
function search(array, val) {
let min = 0;
let max = array.length - 1;
while (min <= max) {
let middle = Math.floor((min + max) / 2);
let currentElement = array[middle];
if (array[middle] < val) {
min = middle + 1;
}
else if (array[middle] > val) {
max = middle - 1;
}
else {
return middle;
}
}
return -1;
}
'JavaScript > Algorithm' 카테고리의 다른 글
재귀(Recursion) (0) | 2023.03.26 |
---|---|
SLIDING WINDOW (0) | 2023.03.24 |
Frequency Counter - Multiple Pointers (0) | 2023.03.17 |
Frequency Counter - sameFrequency (0) | 2023.03.14 |
3. 문제 해결 접근법 (0) | 2023.03.13 |
Comments