๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿง  ์•Œ๊ณ ๋ฆฌ์ฆ˜31

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค]๊ด‘๋ฌผ ์บ๊ธฐ JS ๋งจ์ฒ˜์Œ์—๋Š” DFS๋กœ ์ ‘๊ทผํ–ˆ๋Š”๋ฐ ๋ญ”๊ฐ€ ์ž˜๋ชป ์งฐ๋Š”์ง€ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋‚ฌ๋‹ค ๊ทธ๋ž˜์„œ ๊ทธ๋ฆฌ๋””๋กœ ์ ‘๊ทผํ–ˆ๋‹ค ๐Ÿ’ก์ ‘๊ทผ๋ฐฉ์‹ ๊ด‘๋ฌผ์€ ์ˆœ์„œ๋Œ€๋กœ ์บ์•ผ ํ•˜์ง€๋งŒ ๊ณก๊ดญ์ด ์ˆœ์„œ๋Š” ์ƒ๊ด€์ด ์—†์œผ๋ฏ€๋กœ ๊ด‘๋ฌผ์„ 5๊ฐœ์”ฉ ๋ฌถ์Œ์œผ๋กœ ์ž˜๋ผ์„œ ๋Œ๊ณก๊ดญ์ด ํ”ผ๋กœ๋„ ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด์ค€๋‹ค ๊ณก๊ดญ์ด๋กœ ์บ˜ ์ˆ˜ ์žˆ๋Š” ๋งŒํผ ๊ด‘๋ฌผ ๋ฌถ์Œ์„ ์ž˜๋ผ์ค€๋‹ค ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ณก๊ดญ์ด๋กœ ๊ด‘๋ฌผ์„ ๋‹ค ์บ์ง€ ๋ชปํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์บ˜ ์ˆ˜ ์—†๋Š” ๊ด‘๋ฌผ์€ ์ œ๊ฑฐ ํ”ผ๋กœ๋„ ์ˆœ์œผ๋กœ ์ •๋ ฌ๋œ ๊ด‘๋ฌผ ๋ฌถ์Œ์„ ๋‹ค์ด์•„ > ์ฒ  > ๋Œ ๊ณก๊ดญ์ด ์ˆœ์œผ๋กœ ์บ๊ธฐ function solution(picks, minerals) { //๊ณก๊ดญ์ด ์ˆ˜ let pickCnt = picks.reduce((acc,cur)=>acc+cur, 0); //์ตœ์ข… ํ”ผ๋กœ๋„ let result=0; const pick = ['diamond', 'iron', 'sto.. 2023. 12. 27.
[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์†Œ์ˆ˜์ฐพ๊ธฐ JS ๐Ÿ’ก์ ‘๊ทผ๋ฐฉ๋ฒ• numbers ๋ฐฐ์—ด์—์„œ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ์ˆซ์ž๋“ค์„ ์ฐพ๊ธฐ ํ•ด๋‹น ์ˆซ์ž๊ฐ€ ์†Œ์ˆ˜์ธ์ง€ ํŒ๋ณ„ ์ค‘๋ณต๋˜๋Š” ์ˆซ์ž๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ set ์‚ฌ์šฉ function solution(numbers) { const numArr = numbers.split(''); let visited = new Array(numbers.length).fill(false); const resultSet = new Set(); function isPrime(num){ if(num 2023. 12. 27.
[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์—ฌํ–‰๊ฒฝ๋กœ JS ์ ‘๊ทผ๋ฐฉ๋ฒ•๐Ÿ’ก ๋ชจ๋“  ํ‹ฐ์ผ“๋“ค์˜ ์ถœ๋ฐœ์ง€๋ฅผ ํ‚ค๊ฐ’์œผ๋กœ ํ•˜๋Š” ์ธ์ ‘๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค๊ธฐ ์•ŒํŒŒ๋ฒณ ์ˆœ์œผ๋กœ ์ •๋ ฌ dfs๋กœ ํƒ์ƒ‰ ํƒˆ์ถœ ์กฐ๊ฑด์€ ๊ฒฝ๋กœ์˜ ๊ธธ์ด๊ฐ€ tickets์˜ length์™€ ๊ฐ™์„ ๋•Œ function solution(tickets) { const adjacencyList = {}; let result=[]; //์ธ์ ‘๋ฆฌ์ŠคํŠธ [key=์ถœ๋ฐœ์ง€]:๋„์ฐฉ์ง€[] tickets.forEach(t=>{ const [dep, arr]=t; const tmp=adjacencyList[dep]||[]; tmp.push(arr); adjacencyList[dep]=tmp; }); //์•ŒํŒŒ๋ฒณ ์ˆœ์œผ๋กœ ์ •๋ ฌ for(key in adjacencyList){ adjacencyList[key].sort(); } //์žฌ๊ท€ํ•จ์ˆ˜ dfs //path=์ง€๊ธˆ๊นŒ์ง€์˜.. 2023. 12. 27.
[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ํƒ€๊ฒŸ ๋„˜๋ฒ„ JS ๐Ÿ’ก์ ‘๊ทผ ๋ฐฉ์‹ 1. ์ˆซ์ž ์•ž์˜ ๋ถ€ํ˜ธ๋งŒ +,-๋กœ ๋ฐ”๊ฟ”์ฃผ๋ฉด ๋œ๋‹ค(์ด์ง„ํŠธ๋ฆฌ ์ƒ๊ฐํ•˜๊ธฐ) 2. dfs๋กœ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜ ์ฐพ์•„์ฃผ๊ธฐ 3. numbers์˜ ๋งˆ์ง€๋ง‰์— ๋„๋‹ฌํ–ˆ์„ ๋•Œ return; function solution(numbers, target) { let answer = 0; //level=numbers ๋ฐฐ์—ด ์š”์†Œ ์ธ๋ฑ์Šค(ํŠธ๋ฆฌ์—์„œ level) function dfs(level, sum){ //ํƒˆ์ถœ ์กฐ๊ฑด if(level===numbers.length){ if(sum===target){ answer++; } return; } dfs(level+1, sum+numbers[level]); dfs(level+1, sum-numbers[level]) } dfs(0,0); return answer; } https://sch.. 2023. 12. 27.
[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค]๋‘ ์› ์‚ฌ์ด์˜ ์ •์ˆ˜ ์Œ JS ๐Ÿ’ก์ ‘๊ทผ๋ฐฉ๋ฒ• ์›์˜ ๋ฐฉ์ •์‹ ํ™œ์šฉํ•˜๊ธฐ! (x-a)^2 + (y-b)^2 = r^2 ์›์˜ ์ค‘์‹ฌ์ด ์›์ ์ด๊ธฐ ๋•Œ๋ฌธ์— x^2 + y^2 = r^2 ์›์˜ ๋ฐฉ์ •์‹์„ ํ™œ์šฉํ•ด์„œ x๊ฐ’์— ๋”ฐ๋ฅธ ์›์˜ y๊ฐ’์„ ๊ตฌํ•˜๊ธฐ ๊ฐ x๊ฐ’์— ๋”ฐ๋ฅธ ์ ์˜ ๋ฒ”์œ„ ์ž‘์€ ์›์˜ y๊ฐ’ 2023. 12. 27.
[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค]์—ฐ์†๋œ ๋ถ€๋ถ„ ์ˆ˜์—ด์˜ ํ•ฉ JS ๐Ÿ’ก์ ‘๊ทผ ๋ฐฉ์‹ ํˆฌํฌ์ธํ„ฐ ์‹œ์ž‘ ํฌ์ธํ„ฐ์™€ ๋ ํฌ์ธํ„ฐ ๋‘ ๊ฐ€์ง€๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด์„œ ๋ฒ”์œ„๋ฅผ ๋„“ํ˜€๋‚˜๊ฐ‘๋‹ˆ๋‹ค. ํฌ์ธํ„ฐ ์‚ฌ์ด์˜ ๋ชจ๋“  ์›์†Œ์˜ ํ•ฉ์ด k์ดํ•˜๋ฉด ๋ ํฌ์ธํ„ฐ ๊ฐ’์„ ๋Š˜๋ฆฌ๊ณ  k์ดˆ๊ณผ๋ฉด ์‹œ์ž‘ ํฌ์ธํ„ฐ ๊ฐ’์„ ๋Š˜๋ฆฌ๊ธฐ function solution(sequence, k) { //ํˆฌํฌ์ธํ„ฐ //์‹œ์ž‘ ํฌ์ธํ„ฐ์™€ ๋ ํฌ์ธํ„ฐ ๋‘ ๊ฐ€์ง€๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด์„œ ๋ฒ”์œ„๋ฅผ ๋„“ํ˜€๋‚˜๊ฐ‘๋‹ˆ๋‹ค. //ํฌ์ธํ„ฐ ์‚ฌ์ด์˜ ๋ชจ๋“  ์›์†Œ์˜ ํ•ฉ์ด k์ดํ•˜๋ฉด ๋ ํฌ์ธํ„ฐ ๊ฐ’์„ ๋Š˜๋ฆฌ๊ณ  //k์ดˆ๊ณผ๋ฉด ์‹œ์ž‘ ํฌ์ธํ„ฐ ๊ฐ’์„ ๋Š˜๋ฆฌ๊ธฐ let start = 0; let end = 0; let sum = sequence[0]; let range = [-Infinity, Infinity]; //๋ ํฌ์ธํ„ฐ๊ฐ€ ๋ฐฐ์—ด์˜ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜๋ฉด ํƒˆ์ถœ = sum์ด k๋ณด๋‹ค ์ž‘๊ธฐ ๋•Œ๋ฌธ์— while (end < sequence.len.. 2023. 12. 27.