[LeetCode] 1011.Capacity To Ship Packages Within D Days [JS]
·
알고리즘(Algorithm)
문제A conveyor belt has packages that must be shipped from one port to another within days days.The ith package on the conveyor belt has a weight of weights[i]. Each day, we load the ship with packages on the conveyor belt (in the order given by weights). We may not load more weight than the maximum weight capacity of the ship.Return the least weight capacity of the ship that will result in all th..
[LeetCode/CodeWars] - Two Sum [JS]
·
알고리즘(Algorithm)
난이도 LeetCode => easy CodeWars => 6kyu 1) 문제를 읽고 바로 떠올릴 수 있는 풀이 2개의 반복문으로 값들을 더하면서 target이 되는 값들의 index를 return해준다. function twoSum(numbers, target) { for (let i = 0; i < numbers.length-1; i++) { for (let j = i+1; j < numbers.length; j++) { if (numbers[i] + numbers[j] === target) return [i, j]; } } } 2)자바스크립트가 가지고 있는 indexOf를 사용해서도 풀이가 가능하다 function twoSum(numbers, target) { for (i = 0; i < numbe..
[CodeWars] Give me a Diamond [JS / 6kyu] 다이아 찍기
·
알고리즘(Algorithm)
다이아 찍기 최대한 for문을 하나만 쓰려고 고민했다 function diamond(n){ if(n===1) return '*\n' if(n
프로그래머스 - 영어 끝말잇기[JS]
·
알고리즘(Algorithm)
풀이 - player는 나머지 연산자를 사용해 n이하의 값으로 반복 1. 마지막글자, 앞글자 비교하기 2. 같은 글자 찾기 3. player가 n과 같아지면 차례++ function solution(n, words) { let first=words[0] let count=1 for (let i = 1; i j===words[i])!==i) return [player,count] first=words[i] if(player===n) count++ } return [0,0] }
프로그래머스 - 숫자의 표현 [JS]
·
알고리즘(Algorithm)
풀이 연속된 자연수들의 합으로 자연수 n을 만들어라 완전탐색 -> 시간 초과 1,2,3... 이런식으로 더해나가다가 n이 되면 count++ 다음 숫자는 2부터 시작, 3부터 시작 ... 더해서 n이되면 count++, n (자기자신) count++ 간단하게 이중 for문으로 구현 function solution(n) { let count=0 for (let i = 1; i n이 15일때 연속된 자연수의 합 중 가장 큰 자연수(자기 자신 제외)의 조합은 7+8 즉, 최대 범위는 -> n/2 자기자신을 포함하기 때문에 count에 미리 1을 주고 풀었다 재귀와 합쳐서 만든 2번째 풀이 -> 통과 function solution(n) { let count=1 const helper=(num,sum)=>{ i..
프로그래머스 - 명예의 전당
·
알고리즘(Algorithm)
풀이 먼저, score를 한개씩 보내기 위해 forEach를 사용해 helper 함수에 전달 score.forEach((i)=>{ helper(i) }) 만약, fame(명예의 전당에 오른 점수)의 길이가 k보다 작을 경우 fame에 값을 push하고 최솟값을 구해서 결과값에 push if(fame.lengthmin){ fame.splice(fame.indexOf(min),1) fame.push(point) min=Math.min(...fame) res.push(min) } else if(point{ if(fame.lengthmin){ fame.splice(fame.indexOf(min),1) fame.push(point) min=Math.min(...fame) res.push(min) } else if..