블류_
chill days
블류_

블로그 메뉴

  • TIL
  • CS
  • 졸업작품[2018]
전체 방문자
오늘
어제
  • 분류 전체보기 (22)
    • logs (2)
      • 2018 (1)
    • TIL (17)
      • 졸업 작품 (3)
    • 책 (0)

공지사항

인기 글

최근 댓글

최근 글

hELLO · Designed By 정상우.
블류_

chill days

TIL

알고리즘 강의 정리 (간략)

2023. 1. 3. 19:59
반응형

** 여기 나온 코드는 제가 나중에 보았을 때 기억이 날 정도로.. 작성했기 때문에 참고할 만 코드는 아닙니다!

문제 풀이 패턴

1. Frequency Counter Pattern

아나그램

array [a, a, i, y, i] → { "a" : 2, "i" : 2, "y": 1 }

2. 다중 포인터 

* 정렬된 배열이여야함

ex) 인자들의 합이 x 값인 인자 둘을 구하라 

//주어진 arr에 두 인자의 합이 10인 경우가 존재하는가
let x = 10 
let arr = [1,2,4,5,6,14,30]

let i = 0;
let j = arr.length - 1

while(i < j) {
	if(arr[i] + arr[j] > x) {
		j--
	} else if(arr[i] + arr[j] <x) {
		i++
	} else 
    	return true
}

return false

 

3. 슬라이딩 윈도우

ex) 연속된 num개의 인자의 최대합 

// 3개의 연속된 값
let a = [1,45,63,24,5,6,9,1,333,4,2]

let maxSum = a.slice(0,3).reduce((ac,cur) => (ac + cur),0)
let tmpWindow = maxSum

for(let i = num; i < arr.length; i++ ) {
	tmpWindow += (arr[i] - arr[i-num])
    maxSum = Math.max(maxSum, tmpWindow)
}
return maxSum;

 

4. 분할 정복

문제를 나눌 수 없을 때까지 나눠서 다시 합병하여 문제의 답을 얻는 알고리즘

 1 ) divide : 문제 분할

 2) Conquer : 분할 가능할 때까지 분할, 분할 더 안될 시 문제를 해결함

 3) Combine : 2번의 Conquer한 문제들을 통합하여 원래 문제의 답을 얻음 

저작자표시 (새창열림)

'TIL' 카테고리의 다른 글

prisma relation deleteMany (개인 기록용)  (0) 2023.01.29
prisma - update error : An operation failed because it depends on one or more records that were required but not found. Record to update not found.  (0) 2023.01.05
가상메모리 - page/segmentation  (0) 2022.12.13
MySQL grant all privileges 문법  (0) 2022.11.15
20210521:: font-face / div 둥굴게 만들기 / div 투명하게 만들기  (0) 2021.05.22
    'TIL' 카테고리의 다른 글
    • prisma relation deleteMany (개인 기록용)
    • prisma - update error : An operation failed because it depends on one or more records that were required but not found. Record to update not found.
    • 가상메모리 - page/segmentation
    • MySQL grant all privileges 문법
    블류_
    블류_
    github : https://github.com/eclatchung

    티스토리툴바