본문 바로가기

전체 글159

2021.8.10 TIL 괄호 여닫힘 문제 _ 스택 알고리즘 올바른 괄호 괄호가 잘 닫혀 있는지 체크하는 함수 function solution(str) { let answer; // code here return answer; } let ex1 = '(()(()))(()' console.log(solution(ex1)); // false let ex2 = '(()()))' console.log(solution(ex2)); // false let ex3 = '(())()' console.log(solution(ex3)); // true 답) function solution(str) { let open = '(', close = ')'; let sum = 0; for(let el of str) { if(sum < 0) return false; el === open ?.. 2021. 8. 10.
2021.8.8 TIL 학급회장, 아나그램 (해쉬, 투포인터, 슬라이더) 학급 회장 (해쉬) A, B, C, D, E 후보 중에서 가장 많이 뽑힌 후보는 누구인가? function solution(str) { let answer; // code here; return answer; } let str = "BACBACCACCBDEDE"; console.log(solution(str)); // C 답) object 를 이용 object 의 keys 배열 구하기 => Object.keys(obj) object 의 values 배열 구하기 => Object.values(obj) object의 특정 value값을 갖는 key를 구하기 => Object.keys(obj).find(key => obj[key] === value) function solution(str) { let answ.. 2021. 8. 8.
2021.8.7 TIL 공통 원소, 연속 부분수열 합, k일 연속 최대 매출합 공통 원소 A, B 두 개의 집합이 주어지면 두 집합의 공통 원소를 추출하여 오름차순으로 출력 function solution(a, b) { let answer; // code here; return answer; } let a = [1, 3, 9, 5, 2]; let b = [3, 2, 5, 7, 8]; console.log(solution(a, b)); // [2,3,5] 답) 중복을 허용하지 않는 Set()을 이용 function solution(a, b) { let answer = []; let mySet = new Set(a); for(let el of b) { mySet.has(el) ? answer.push(el) : null; } return answer.sort((a, b) => a - .. 2021. 8. 7.
2021.8.6 TIL 졸업선물,k번째합,두 배열 합치기 졸업 선물 학생에게 한정된 예산으로 가장 많은 선물을 할 수 있는 경우의 수는? 50% 할인 쿠폰을 한 번만 쓸 수 있다. 배송비 적용 안됨 상품값과 배송값으로 이루어진 배열이 주어진다. 매개변수는 배열과 선생님의 예산이다. function solution(arr, num) { let answer; // code here; return answer; } let example = [[6, 6], [2, 2], [4, 3], [4, 5], [10, 3]]; console.log(solution(example, 28)); //4 틀린 답) 최대 상품의 값을 구하고 그 값을 50% 할인한다. 그 후 상품값과 배송값을 더 한 배열을 만든다. 그 배열을 작은 수 순으로 정렬하고 예산 안에서 가능한 경우를 구한다. .. 2021. 8. 7.
2021.8.4 TIL 자리수의 합, 뒤집은 소수, 멘토링 경우의 수 자릿수의 합 N개의 자연수가 입력되면 각 자릿수의 합을 구하고, 그 중 합이 최대인 자연수를 출력. 만약 자릿수의 합이 같을 경우 원 숫자가 큰 것을 리턴한다. function solution(arr) { let answer; // code here; return answer; } let example = [128, 460, 603, 40, 521, 137, 123] console.log(solution(example)); //137 답1) 각 배열의 원소들을 돌면서 문자열로 변환 후 한자리씩 쪼개어 최대 합을 구한다. function solution(arr) { let answer= max = Number.MIN_SAFE_INTEGER; for(let el of arr) { let str = '' + .. 2021. 8. 4.
2021.8.3 TIL 문자거리, 문자열 압축 가장 짧은 문자거리 주어진 단어를 하나씩 돌면서, 타겟한 문자와 최소 거리를 리턴 function solution(str, tar) { let answer; // code here; return answer; } let example = "happyecodinge"; let target = 'e'; console.log(solution(example, target)) //[5, 4, 3, 2, 1, 0, 1, 2, 3, 3, 2, 1, 0] 답) indexOf 를 이용하여 해당 문자의 앞뒤로 존재하는 타겟 문자와의 거리의 최소 값을 구한다. function solution(str, tar) { let answer = []; let front = back = 0; for(let i = 0; i < str... 2021. 8. 3.
2021.8.2 TIL 회문문자열,팰린드롬,숫자추출 회문 문자열 체크 회문 문제열 'abba'인지 아닌지 체크. 대/소문자는 구분하지 않는다. 'gooG' => 'true' function solution(str) { let answer; // code here; return answer; } 답) 주어진 문자열을 반으로 갈라서 비교 function solution(str) { let answer = true; let capital = str.toUpperCase(); let n = str.length; let mid = Math.floor(n/2); for(lef i = 0; i < mid; i++) { if(capital[i] !== capital[n - i - 1]){ answer = false; break; } }; return answer; } .. 2021. 8. 2.
2021.7.31 TIL (격차판 최대합, 봉우리) 문제) n:n 격차판 최대 합 구하기 n:n격자판의 가로/세로/대각선 합 중에서 최대값 구하기 function solution(arr) { let answer; // code here; return answer; } let example = [ [10, 13, 10, 12, 15], [12, 39, 30, 23, 11], [11, 25, 50, 53, 15], [19, 27, 29, 37, 27], [19, 13, 30, 13, 19] ]; console.log(solution(example)) // 155 나의 조잡한 해답) 가로/세로/대각선1/대각선 2의 합을 구하고, 비교한다. function solution(arr) { let answer = Number.MIN_SAFE_INTEGER; let n.. 2021. 7. 31.