알고리즘
-
[프로그래머스 알고리즘] while과 reduce의 합작알고리즘 2023. 6. 21. 12:07
예산 function solution(d, budget) { var answer = 0, sum = 0; d.sort((a,b) => a - b); for(let i = 0; i budget) answer--; } return answer; } 정렬이 포인트인듯 근데 다른사람 풀이를 보니 filter를 이용한 것을 while의 조건으로 줘서 해결하는 경우도 있던데 이게 좀 신박하다 그리고 계속 변수 = 0 선언하고 로직을 짜면 좀 더 쉬워지는데 그걸 자꾸 간과하고 따른짓하다가 오래걸렸다.... function solution(d, budget) { d.sort((a, b) => a - b); while (d.reduc..
-
[프로그래머스 알고리즘] 1~N까지 배열만들기, 제곱근의 갯수와 약수의 관계알고리즘 2023. 6. 21. 11:36
약수의 개수와 덧셈 function solution(left, right) { const arr = [...Array(right+1).keys()].slice(1); const newArr = arr.filter(e => e >= left) const answer = newArr.map(e => numYacksu(e)).reduce((a, b) => a += b) return answer; } const numYacksu = (n) => { let count = 0; for (let i = 1; i
-
[프로그래머스 level.1] join(), split()알고리즘 2023. 6. 21. 11:01
숫자문자열과 영단어 function solution(s) { let numbers = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]; var answer = s; for(let i = 0; i < numbers.length; i++) { let arr = answer.split(numbers[i]); console.log(arr) answer = arr.join(i); console.log(answer) } return Number(answer); } spit()는 받은 인자를 기준으로 하여 문자열을 나누기때문에 여기서 사용하게 된다면 문자열을 2개로 만들면서 기준이 되는 문자열을 없앨 수 있다. 이때 같이..