프로그래머스 - 체육복(Greedy) [JS]
·
알고리즘(Algorithm)
풀이 function solution(n, lost, reserve) { let checkLost=lost.filter(i=>!reserve.includes(i)).sort() let checkReserve=reserve.filter(i=>!lost.includes(i)).sort() checkLost=checkLost.filter(i=>{ let temp=checkReserve.find(j=>Math.abs(i-j)k!==temp) }) return n-checkLost.length } lost와 reserve에 둘 다 있는 숫자를 먼저 제거후 정렬-> 체육복을 잃어버렸지만 여분을 가진 친구들 ex) solution(3, [1], [1]) 체육복을 잃어버린 학생들의 배열 ( checkLost )의 값이..
프로그래머스 - 다트게임 [JS]
·
알고리즘(Algorithm)
풀이 function solution(dartResult) { let check = dartResult.match(/\d{1,2}[SDT][*|#]?/g) let res=[] for (let i = 0; i < 3; i++) { let num=check[i].match(/\d{1,2}/g) let bonus=check[i].match(/S|D|T/g) let option=check[i].match(/[*|#]?/g) if(bonus[0]==='S') num=+num else if(bonus[0]==='D') num=Math.pow(+num,2) else if(bonus[0]==='T') num=Math.pow(+num,3) if(option.includes('*')){ num*=2 if(i!==0){ r..
프로그래머스 - 비밀지도 [JS]
·
알고리즘(Algorithm)
내 풀이 function solution(n, arr1, arr2) { let res=[] arr1=arr1.map((e)=>{ return e.toString(2).padStart(arr1.length,0) }) arr2=arr2.map((e)=>{ return e.toString(2).padStart(arr2.length,0) }) for (let i = 0; i < arr1.length; i++) { arr1[i]=arr1[i].replaceAll(1,'#') arr2[i]=arr2[i].replaceAll(1,'#') let temp='' for (let j = 0; j < n; j++) { if(arr1[i][j]==='#'||arr2[i][j]==='#') temp+='#' else temp+..
프로그래머스 - 소수 만들기[JS]
·
알고리즘(Algorithm)
풀이 완전탐색말고 방법이 있다면 다시 풀어서 올릴 예정 삼중 for문으로 경우의 수를 모두 찾고 그 중에서 소수인 애들만 ++ function solution(nums) { let total=0 for (let i = 0; i { for (let i = 2; i
프로그래머스 - 소수 찾기[JS]
·
알고리즘(Algorithm)
풀이 [ 에라토스테네스의 체 ] const sqrt=parseInt(Math.sqrt(n)) 제곱근 사용 √4, √5, √8 , √11, √24 2, √5 , 2√2, √11, 2√2*√3 즉, n이라는 수가 주어졌을 때 루트를 씌워서 나눠지면 √n의 배수라는 뜻 -> 소수가 아님 const arr=Array(n+1).fill(true).fill(false,0,2) 왜 n+1을 했고, 0~2 index는 false로 했는가? 배열의 index는 0부터 시작 모든 배열의 값을 소수라고 가정한 뒤 n=10일 때, [false,false,true,true,true,true,true,true,true,true,true] => [0,1,2,3,4,5,6,7,8,9,10] 0과1은 소수가 아니므로 false for..
프로그래머스 - 숫자 문자열과 영단어 [JS][2021 카카오 인턴]
·
알고리즘(Algorithm)
풀이 ["one22one"] 이런식의 형태로 s가 주어질 수 있는 것을 생각해서 while(isNaN(s))을 이용해 무한 반복문 생성 s 자체를 replace하기에 모든 문자가 없어지면 isNaN이 false -> 반복문 탈출 function solution(s) { let enWord = { zero: 0, one: 1, two: 2, three: 3, four: 4, five: 5, six: 6, seven: 7, eight: 8, nine: 9, } while(isNaN(s)){ for(let i in enWord){ if(s.includes(i)){ s=s.replace(i,enWord[i]) } } } const answer = Number(s); return answer; }
프로그래머스 - 모의고사 [JS]
·
알고리즘(Algorithm)
핵심 제출 시 5번 이후로 거의 다 실패가 나온다면 그건 answers의 길이와 각 수포자가 찍는 방식의 대한 길이를 고려하지 X 단순하게 아래처럼 코드를 만들었다고 가정해보자 answers.forEach((i,idx)=>{ if(i===ex1[idx]){ ans.push(1) } if(i===ex2[idx]){ ans.push(2) } if(i===ex3[idx]){ ans.push(3) } }) 만약 answers가 [1,2,3,4,5,2,2,2,3,4,2,5,1,2,5,5,3] 이런식으로 되어있다면 idx값은 계속해서 증가한다. 그래서 ex1[10] ← undefined 라는 결과가 나오기에 인덱스 자체를 각 수포자가 찍는 방식의 길이로 나눠서 해결하면 되는 문제였다. answers.forEach(..
프로그래머스 - 크기가 작은 부분문자열[JS]
·
알고리즘(Algorithm)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring String.prototype.substring() - JavaScript | MDN The substring() method returns the part of the string from the start index up to and excluding the end index, or to the end of the string if no end index is supplied. developer.mozilla.org function solution(t, p) { let pLen=p.length; let tLen=t.length;..
프로그래머스 - 문자열 나누기[JS]
·
알고리즘(Algorithm)
문제 설명 문자열 s가 입력되었을 때 다음 규칙을 따라서 이 문자열을 여러 문자열로 분해하려고 합니다. 먼저 첫 글자를 읽습니다. 이 글자를 x라고 합시다. 이제 이 문자열을 왼쪽에서 오른쪽으로 읽어나가면서, x와 x가 아닌 다른 글자들이 나온 횟수를 각각 셉니다. 처음으로 두 횟수가 같아지는 순간 멈추고, 지금까지 읽은 문자열을 분리합니다. s에서 분리한 문자열을 빼고 남은 부분에 대해서 이 과정을 반복합니다. 남은 부분이 없다면 종료합니다. 만약 두 횟수가 다른 상태에서 더 이상 읽을 글자가 없다면, 역시 지금까지 읽은 문자열을 분리하고, 종료합니다. 문자열 s가 매개변수로 주어질 때, 위 과정과 같이 문자열들로 분해하고, 분해한 문자열의 개수를 return 하는 함수 solution을 완성하세요. ..
프로그래머스 - 가장 가까운 글자 [JS]
·
알고리즘(Algorithm)
solution("banana") if 이후-> overlap : ['b','a','n'] else{ [1] : overlap.length=3 overlap.lastIndexOf(s[i])=1 -> overlap.push(s[i]) = 'a' overlap : ['b','a','n','a'] [2] : overlap.length=4 overlap.lastIndexOf(s[i])=2 [3] : overlap.length=5 overlap.lastIndexOf(s[i])=3 lastIndexOf를 사용함으로써 가장 위쪽의 있는 글자와 비교를 하기 위해서 function solution(s) { let overlap=[] let res=[] for (const i in s) { if(!overlap.inclu..