본문 바로가기

헬리'Daily/꾸준한 알고리즘17

알고리즘문제 5 너무어려워이거....완젼히 다 맞지는못했다... function minimumTreePath(n, edges, visitNodes) { const INF = 1e9; const dist = Array(n).fill().map(()=> Array(n).fill(INF)); const visit = visitNodes.map(v => v-1); edges.forEach(([u, v]) =>{ dist[u-1][v-1] =1; dist[v-1][u-1] =1; }); for(let k=0; k < n; k++) for(let i=0; i 2023. 5. 29.
알고리즘문제 4 function longestChain(words) { const dp = {}; words.sort((a,b)=>a.length - b.length); let max = 0; for(const word of words){ let longest = 0; for(let i =0; i< word.length; i++){ const sub = word.slice(0,i) + word.slice(i+1); longest = Math.max(longest, (dp[sub] || 0)+1); } dp[word]=longest; max = Math.max(max, longest); } return max; } 2023. 5. 29.
알고리즘문제 3 function maxLength(a, k) { let start = 0; let sum = 0; let maxLen = 0; for(let end = 0; end k) { sum -= a[start]; start++; } maxLen = Math.max(maxLen, end - start + 1); } return maxLen; } 2023. 5. 29.
알고리즘 문제 2 function findRange(num) { const numStr = num.toString(); let min = 0; let max = 0; let changeIndex = 0; if(numStr[changeIndex] === '1') { changeIndex++; while(changeIndex < numStr.length && (numStr[changeIndex] === '0' || numStr[changeIndex] === '1' )) { changeIndex++; } min = numStr.replaceAll(numStr[changeIndex],'0'); } else { min = numStr.replaceAll(numStr[changeIndex],'1'); } changeIndex = 0.. 2023. 5. 29.
알고리즘 문제 1 function getFinalString(s) { let finalString = s; while (finalString.includes("AWS")){ finalString = finalString.replace("AWS", ""); } return finalString.length > 0 ? finalString : -1 ; } 2023. 5. 29.
728x90