[JavaScript/section 2] 03 - ๊ฐ€์œ„ ๋ฐ”์œ„ ๋ณด
ยท
Algorithm/์ธํ”„๋Ÿฐ(inflearn)
๐Ÿ“Œ ๋ฌธ์ œ A, B ๋‘ ์‚ฌ๋žŒ์ด ๊ฐ€์œ„๋ฐ”์œ„๋ณด ๊ฒŒ์ž„์„ ํ•œ๋‹ค. ์ด N๋ฒˆ์˜ ๊ฒŒ์ž„์„ ํ•˜์—ฌ A๊ฐ€ ์ด๊ธฐ๋ฉด A๋ฅผ ์ถœ๋ ฅํ•˜๊ณ , B๊ฐ€ ์ด๊ธฐ๋ฉด B๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ๋น„๊ธธ๊ฒฝ์šฐ์—๋Š” D๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ๊ฐ€์œ„, ๋ฐ”์œ„, ๋ณด์˜ ์ •๋ณด๋Š” 1: ๊ฐ€์œ„, 2: ๋ฐ”์œ„, 3: ๋ณด๋กœ ์ •ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด N = 5์ด๋ฉด ํšŒ์ˆ˜ 1 2 3 4 5 A์˜ ์ •๋ณด 2 3 3 1 3 B์˜ ์ •๋ณด 1 1 2 2 3 ์Šน์ž A B A B D ๋‘ ์‚ฌ๋žŒ์˜ ๊ฐ ํšŒ์˜ ๊ฐ€์œ„, ๋ฐ”์œ„, ๋ณด ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง€๋ฉด ๊ฐ ํšŒ๋ฅผ ๋ˆ„๊ฐ€ ์ด๊ฒผ๋Š”์ง€ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ๋‚˜๋Š” ๊ฐ€์œ„๋ฐ”์œ„๋ณด์—์„œ ์ƒ๊ธธ ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์ฐพ์•„ if ์กฐ๊ฑด๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜์˜€๋‹ค. ํ•˜์ง€๋งŒ ์ด ๋ฌธ์ œ๋Š” ๊ธฐ์ค€ ํ•˜๋‚˜๋ฅผ ์ž˜ ์ •ํ•œ๋‹ค๋ฉด ์ฝ”๋“œ๋ฅผ ๋งŽ์ด ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค. ๋น„๊ธธ ๊ฒฝ์šฐ, A๊ฐ€ ์ด๊ฒผ์„ ๊ฒฝ์šฐ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํ•œ๋‹ค๋ฉด ๋‚˜๋จธ์ง€๋Š” B๊ฐ€ ์ด๊ฒผ์„ ๊ฒฝ์šฐ์ด๊ธฐ ๋•Œ..
[JavaScript/section 2] 02 - ๋ณด์ด๋Š” ํ•™์ƒ
ยท
Algorithm/์ธํ”„๋Ÿฐ(inflearn)
๐Ÿ“Œ ๋ฌธ์ œ ์„ ์ƒ๋‹˜์ด N๋ช…์˜ ํ•™์ƒ์„ ์ผ๋ ฌ๋กœ ์„ธ์› ๋‹ค. ์ผ๋ ฌ๋กœ ์„œ ์žˆ๋Š” ํ•™์ƒ์˜ ํ‚ค๊ฐ€ ์•ž์—์„œ๋ถ€ํ„ฐ ์ˆœ์„œ๋Œ€๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ๋งจ ์•ž์— ์„œ ์žˆ๋Š” ์„ ์ƒ๋‹˜์ด ๋ณผ ์ˆ˜ ์žˆ๋Š” ํ•™์ƒ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. (์•ž์— ์„œ ์žˆ๋Š” ์‚ฌ๋žŒ๋“ค๋ณด๋‹ค ํฌ๋ฉด ๋ณด์ด๊ณ , ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉด ๋ณด์ด์ง€ ์•Š๋Š”๋‹ค.) ์ œ์ผ ์•ž์— ์žˆ๋Š” ํ•™์ƒ์˜ ํ‚ค๋ฅผ max ๋ณ€์ˆ˜์— ํ• ๋‹นํ•œ ํ›„ ๋’ค์— ์žˆ๋Š” ํ•™์ƒ๋“ค์˜ ํ‚ค์™€ ๋น„๊ตํ•œ๋‹ค. for ๋ฌธ์„ ๋Œ๋ฉด์„œ arr[i]๊ฐ€ max ๋ณ€์ˆ˜์— ์ €์žฅ๋œ ๊ฐ’๋ณด๋‹ค ํด ๊ฒฝ์šฐ max ๋ณ€์ˆ˜์— ๊ฐ’์„ ์žฌํ• ๋‹นํ•˜๊ณ , answer ๋ณ€์ˆ˜์— ์ €์žฅ๋œ ๊ฐ’์— 1์„ ๋”ํ•ด์ฃผ๋ฉด ๋œ๋‹ค. ๐Ÿ“ ํ’€์ด function solution(arr) { let answer = 1, max = arr[0]; for (let i = 1; i < arr.length; i++) { if (max < arr[i]) { ..
[JavaScript/section 2] 01 - ํฐ ์ˆ˜ ์ถœ๋ ฅํ•˜๊ธฐ
ยท
Algorithm/์ธํ”„๋Ÿฐ(inflearn)
๐Ÿ“Œ ๋ฌธ์ œ N๊ฐœ์˜ ์ •์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›์•„, ์ž์‹ ์˜ ๋ฐ”๋กœ ์•ž ์ˆ˜๋ณด๋‹ค ํฐ ์ˆ˜๋งŒ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. (์ฒซ ๋ฒˆ์งธ ์ˆ˜๋Š” ๋ฌด์กฐ๊ฑด ์ถœ๋ ฅํ•œ๋‹ค.) ์ฒซ ๋ฒˆ์งธ ์ˆ˜๋Š” ๋ฏธ๋ฆฌ ๋ฐฐ์—ด์— ์ถ”๊ฐ€๋ฅผ ์‹œ์ผœ์ค€ ํ›„ for ๋ฌธ์„ ๋Œ๋ฉด์„œ ์ž์‹ ์˜ ๋ฐ”๋กœ ์•ž ์ˆ˜๋ณด๋‹ค ํฐ ๊ฒฝ์šฐ, push() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐฐ์—ด์— ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด ๋œ๋‹ค. ๐Ÿ“ ํ’€์ด function solution(arr) { const answer = []; answer.push(arr[0]); for (let i = 1; i < arr.length; i++) { if (arr[i - 1] < arr[i]) answer.push(arr[i]); } return answer.join(' '); } let arr = [7, 3, 9, 5, 6, 12]; console.log(solution(arr));
[JavaScript/section 1] 17 - ์ค‘๋ณต๋‹จ์–ด์ œ๊ฑฐ
ยท
Algorithm/์ธํ”„๋Ÿฐ(inflearn)
๐Ÿ“Œ ๋ฌธ์ œ N๊ฐœ์˜ ๋ฌธ์ž์—ด์ด ์ž…๋ ฅ๋˜๋ฉด ์ค‘๋ณต๋œ ๋ฌธ์ž์—ด์€ ์ œ๊ฑฐํ•˜๊ณ  ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ž์—ด์€ ์›๋ž˜์˜ ์ž…๋ ฅ์ˆœ์„œ๋ฅผ ์œ ์ง€ํ•œ๋‹ค. filter() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์กฐ๊ฑด์— ์ ํ•ฉํ•œ ์š”์†Œ๋งŒ ํฌํ•จ๋œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜๋ฐ›๋Š”๋‹ค. ์กฐ๊ฑด์˜ ๊ฒฝ์šฐ ์ค‘๋ณต๋ฌธ์ž์ œ๊ฑฐ ๋ฌธ์ œ์—์„œ ์‚ฌ์šฉํ•œ indexOf() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. ๐Ÿ“ ํ’€์ด // filter() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ ๋ฐฉ๋ฒ• function solution(s) { let answer; answer = s.filter((str, i) => { return i === s.indexOf(str); }); return answer; } const str = ['good', 'time', 'good', 'time', 'student']; console.log(solution(str)); ..
[JavaScript/section 1] 16 - ์ค‘๋ณต๋ฌธ์ž์ œ๊ฑฐ
ยท
Algorithm/์ธํ”„๋Ÿฐ(inflearn)
๐Ÿ“Œ ๋ฌธ์ œ ์†Œ๋ฌธ์ž๋กœ ๋œ ํ•œ๊ฐœ์˜ ๋ฌธ์ž์—ด์ด ์ž…๋ ฅ๋˜๋ฉด ์ค‘๋ณต๋œ ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ์ œ๊ฑฐ๋œ ๋ฌธ์ž์—ด์˜ ๊ฐ ๋ฌธ์ž๋Š” ์›๋ž˜ ๋ฌธ์ž์—ด์˜ ์ˆœ์„œ๋ฅผ ์œ ์ง€ํ•œ๋‹ค. ๋ฌธ์ž์—ด์˜ ๊ฐ ๋ฌธ์ž๋“ค์„ ๋น„๊ตํ•˜๋ฉด์„œ ํ’€์ดํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์ง€๋งŒ Set ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•ด๋„ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ๋‹ค. ๐Ÿ“ ํ’€์ด // includes() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ function solution(s) { let answer = ''; for (let x of s) { if (!answer.includes(x)) answer += x; } return answer; } console.log(solution('ksekkset')); // indexOf() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ function solution(s) { let answer = ''; for (let..
[JavaScript/section 1] 15 - ๊ฐ€์šด๋ฐ ๋ฌธ์ž ์ถœ๋ ฅ
ยท
Algorithm/์ธํ”„๋Ÿฐ(inflearn)
๐Ÿ“Œ ๋ฌธ์ œ ์†Œ๋ฌธ์ž๋กœ ๋œ ๋‹จ์–ด(๋ฌธ์ž์—ด)๊ฐ€ ์ž…๋ ฅ๋˜๋ฉด ๊ทธ ๋‹จ์–ด์˜ ๊ฐ€์šด๋ฐ ๋ฌธ์ž๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ๋‹จ, ๋‹จ์–ด์˜ ๊ธธ์ด๊ฐ€ ์ง์ˆ˜์ผ ๊ฒฝ์šฐ ๊ฐ€์šด๋ฐ 2๊ฐœ์˜ ๋ฌธ์ž๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ๋‚˜๋Š” ๋ฌธ์ž์—ด์˜ ์ธ๋ฑ์Šค๋ฅผ ์ง์ ‘ ์ ‘๊ทผํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜์˜€์ง€๋งŒ substring() ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ๊ฒƒ์ด ๋” ์ผ๋ฐ˜์ ์ธ ๋ฐฉ๋ฒ•์ด์ง€ ์•Š์„๊นŒ ์ƒ๊ฐํ•œ๋‹ค. ๐Ÿ“ ํ’€์ด // ๋ฌธ์ž์—ด์˜ ์ธ๋ฑ์Šค๋ฅผ ์ง์ ‘ ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ function solution(s) { let answer; const mid = Math.floor(s.length / 2); if (s.length % 2 === 0) answer = str[mid - 1] + str[mid]; else answer = str[mid]; return answer; } console.log(so..
[JavaScript/section 1] 14 - ๊ฐ€์žฅ ๊ธด ๋ฌธ์ž์—ด
ยท
Algorithm/์ธํ”„๋Ÿฐ(inflearn)
๐Ÿ“Œ ๋ฌธ์ œ N๊ฐœ์˜ ๋ฌธ์ž์—ด์ด ์ž…๋ ฅ๋˜๋ฉด ๊ทธ ์ค‘ ๊ฐ€์žฅ ๊ธด ๋ฌธ์ž์—ด์„ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ์ตœ๋Œ“๊ฐ’, ์ตœ์†Ÿ๊ฐ’์„ ๊ตฌํ•ด์•ผ ํ•  ๊ฒฝ์šฐ Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER(์•ˆ์ „ํ•œ ์ƒ์ˆ˜)๋ฅผ ์ž˜ ํ™œ์šฉํ•˜๋ฉด ์ข‹๋‹ค. ๐Ÿ“ ํ’€์ด function solution(s) { let answer = '', max = Number.MIN_SAFE_INTEGER; for (let x of s) { if (max < x.length) { max = x.length; answer = x; } } return answer; } let str = ['teacher', 'time', 'student', 'beautiful', 'good']; console.log(solution(str)); function ..
[JavaScript/section 1] 13 - ๋Œ€์†Œ๋ฌธ์ž ๋ณ€ํ™˜
ยท
Algorithm/์ธํ”„๋Ÿฐ(inflearn)
๐Ÿ“Œ ๋ฌธ์ œ ๋Œ€๋ฌธ์ž์™€ ์†Œ๋ฌธ์ž๊ฐ€ ๊ฐ™์ด ์กด์žฌํ•˜๋Š” ๋ฌธ์ž์—ด์„ ์ž…๋ ฅ๋ฐ›์•„ ๋Œ€๋ฌธ์ž๋Š” ์†Œ๋ฌธ์ž๋กœ ์†Œ๋ฌธ์ž๋Š” ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. toUpperCase() ๋ฉ”์„œ๋“œ์™€ toLowerCase() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ„๋‹จํ•˜๊ฒŒ ๋ฌธ์ œ ํ•ด๊ฒฐ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. for...of ๋ช…๋ น๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋น™๋น™ ๋Œ๋ฉด์„œ ์†Œ๋ฌธ์ž์ธ ๊ฒฝ์šฐ toUpperCase() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•œ ํ›„ answer ๋ณ€์ˆ˜์— ๋ˆ„์ ์‹œ์ผœ์ฃผ๊ณ , ๋Œ€๋ฌธ์ž์ธ ๊ฒฝ์šฐ toLowerCase() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์†Œ๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•œ ํ›„ answer ๋ณ€์ˆ˜์— ๋ˆ„์ ์‹œ์ผœ์ฃผ๋ฉด ๋œ๋‹ค. ๐Ÿ“ ํ’€์ด // toUpperCase() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŒ๋‹จ function solution(s) { let answer = ''; for (let x of s) { if (x === x.toUpperCa..
_์„ฑํ˜ธ_
'Algorithm' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๊ธ€ ๋ชฉ๋ก (10 Page)