본문 바로가기

Web development/Algorithm64

[LeetCode] 46. Permutations 순열 (Javascript) https://leetcode.com/problems/permutations/ var permute = function(nums) { return nums.reduce( function(list, element) { var newlist = []; list.forEach(function(seq) { for (var i = seq.length; i >= 0; i--) { var newseq = [].concat(seq); newseq.splice(i, 0, element); newlist.push(newseq); } }); return newlist; }, [[]] ); }; 순열 이해하기가 왜이렇게 어려운지.. 요약하면, 반복을 돌며 값을 고정해나가는 방식이다. [1, 2]이 있다고 하면, 배열 길이만큼.. 2020. 7. 22.
[LeetCode] 11. Container With Most Water (Javascript) https://leetcode.com/problems/container-with-most-water/submissions/ 막대들이 주어지고, 가장 넓은 면적을 찾는 문제다. 이중포문과 완전탐색으로 풀었는데, 문제를 딱 봤을때 직관적으로 떠오르는 풀이방법이긴 하지만... 더 좋은 방법이 있지 않을까? var maxArea = function(height) { // 위치 index, 높이 h인 object 배열을 만든다. const map = height.map((h, index)=> ({index, h})); let answer = 0; // 이중 포문...으로 모든 조합을 찾는다. for (let i = 0 ; i < map.length; i++) { for (let j = i + 1; j < map... 2020. 7. 21.
[LeetCode] 349, 350. Intersection of Two Arrays I, II (Javascript) 배열의 겹치는 부분을 찾는 문제다. 1은 여러번 겹쳐도 하나만 리턴하면 되고, 2는 여러번 겹치면 여러개를 리턴해야 한다. Intersection of Two Arrays I var intersection = function(nums1, nums2) { const answer = [] for (n1 of nums1) { if(nums2.includes(n1)){ answer.push(n1) } } return Array.from(new Set(answer)); }; nums1 배열을 반복을 돌면서 nums2에도 있다면 삭제하고 answer 배열에 담는 방식으로 풀었다. var intersection = function(nums1, nums2) { const answer = nums1.filter((n1).. 2020. 7. 20.
[LeetCode] 189. Rotate Array (Javascript) k번만큼 배열 앞에서 뒤로 아이템을 옮기는 문제다. var rotate = function(nums, k) { for(let i = 0; i < k; i++) { const pop = nums.pop(); nums.unshift(pop); } return nums; }; 문제 흐름대로 k번 반복을 돌며 pop과 unshift를 해도 되지만 var rotate = function(nums, k) { const spliced = nums.splice(nums.length - k, nums.length); nums.unshift(...spliced); return nums; }; k개만큼 떼서 한번에 unshift하는게 훨씬 낫다. 2020. 7. 20.
[HackerRank 30 Days of Code] Day 5 ~ Day 7 Day 5 : Loops 주어진 n의 구구단을 출력하면 된다. function main() { const n = parseInt(readLine(), 10); for(let i = 1; i 2020. 7. 13.
[javascript] 큐, 스택 구현 큐 구현 // 큐 const q = []; const push = (item) => q.push(item); const pop = () => q.pop(); q.push(1); console.log(q); // [ 1 ] q.push(2); console.log(q); // [ 1, 2 ] q.push(3); console.log(q); // [ 1, 2, 3 ] q.pop(); q.pop(); q.push(4); console.log(q); // [ 1, 4 ] 스택 구현 // 스택 const stack = []; const push = (item) => stack.push(item); const shift = () => stack.shift(); stack.push(1); stack.push(2); .. 2020. 7. 8.
베스트앨범 function solution(genres, plays) { var answer = []; const arr = []; const uniqG = Array.from(new Set(genres)); for(let i in genres){ arr.push({ i: i, // 고유번호 g: genres[i], // 장르 p: plays[i] // 재생된 횟수 }) } const cnt = {}; // 장르별 재생횟수 const countG = uniqG.map((ug) => { let gItems = arr.filter(a=>a.g === ug); cnt[ug] = 0 return { g: ug, pCnt: gItems.reduce((acc,cur)=> {return acc + cur.p},0) }; });.. 2020. 7. 8.
[HackerRank 30 Days of Code] Day 3, Day 4 Day 3 https://www.hackerrank.com/challenges/30-conditional-statements/problem 간단한 조건문 구현이다. 홀수면 'Weird' 짝수이고 2~5면 'Not Weird' 짝수이고 6~20이면 'Weird' 짝수이고 20보다 크면 'Not Weird'를 출력한다. function main() { const N = parseInt(readLine(), 10); const isOdd = N % 2 === 1; if (isOdd) { console.log('Weird'); } else { if((N > 1 && N 20){ console.log('Not Weird'); } else { console.log('Weird'); } } } .. 2020. 7. 7.
[HackerRank 30 Days of Code] Day 0 ~ Day 2 Day 0 https://www.hackerrank.com/challenges/30-hello-world/problem 아주 간단한, 주어진 문자열을 로그로 찍는 문제다. function processData(inputString) { // This line of code prints the first line of output console.log("Hello, World."); // Write the second line of output that prints the contents of 'inputString' here. console.log(inputString); } Day 1 https://www.hackerrank.com/challenges/30-data-types/problem int, d.. 2020. 7. 7.