์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ์‹œ๊ฐ„๋ณต์žก๋„์™€ Big-O
๐Ÿ““ Algorithm 2022. 4. 29. 22:22

๋“ค์–ด๊ฐ€๋ฉฐ ๊ธฐ์™•์ด๋ฉด ๊ฐ€์žฅ ๋น ๋ฅธ ๊ธธ๋กœ ๊ฐ€์ž! ์ถœํ‡ด๊ทผ์„ ํ•˜๊ฑฐ๋‚˜, ์นœ๊ตฌ์™€์˜ ์•ฝ์† ๋“ฑ๋“ฑ. ์–ด๋”˜๊ฐ€๋ฅผ ํ–ฅํ•  ๋•Œ ์šฐ๋ฆฌ๋Š” ํ•ญ์ƒ ๋ชฉ์ ์ง€๋กœ ๊ฐ€๋Š” ๋น ๋ฅธ ๋ฃจํŠธ๋ฅผ ์ฐพ์•„๋ณด๋Š” ๊ฒฝํ–ฅ์ด ์žˆ๋‹ค. ์ปดํ“จํ„ฐ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋‹ค. ํ”„๋กœ๊ทธ๋žจ์„ ์‹œํ–‰ํ•  ๋•Œ ์‹œ๊ฐ„ ๋ณต์žก๋„๊ฐ€ ๊ฐ€์žฅ ๋‚ฎ์€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์„ ํƒํ•˜์—ฌ ํšจ์œจ์ ์œผ๋กœ ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•˜๋ ค ํ•œ๋‹ค. ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์–ด๋–ค ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•œ ๊ณผ์ •(๋ช…๋ น์–ด)๋“ค์˜ ๋‹จ๊ณ„์ ์ธ ๋‚˜์—ด์„ ๋งํ•œ๋‹ค. ์–ด๋–ค ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ๋” ํšจ์œจ์ ์ธ๊ฐ€๋ฅผ ํ‰๊ฐ€ํ•˜๋Š” ๋ฐ์—๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ์ง€ํ‘œ๋“ค์ด ์‚ฌ์šฉ๋œ๋‹ค. ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ‰๊ฐ€ ์ง€ํ‘œ ์ •ํ™•์„ฑ ์ž‘์—…๋Ÿ‰ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰ ์ตœ์ ์„ฑ ํšจ์œจ์„ฑ ์‹œ๊ฐ„ ๋ณต์žก๋„ => ์ˆ˜ํ–‰ํ–ˆ์„ ๋•Œ ์–ผ๋งŒํผ์˜ ์‹œ๊ฐ„์ด ์†Œ์š”๋˜๋Š”์ง€? ๊ณต๊ฐ„ ๋ณต์žก๋„ => ์–ผ๋งŒํผ์˜ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์‚ฌ์šฉ๋˜๋Š”์ง€? ๋ณดํ†ต ์•Œ๊ณ ๋ฆฌ์ฆ˜์—์„œ ์ค‘์š”ํ•˜๊ฒŒ ์—ฌ๊ฒจ์ง€๋Š” ๋ถ€๋ถ„์€ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰๊ณผ ํšจ์œจ์„ฑ, ๊ทธ ์ค‘์—์„œ๋„ ์‹œ๊ฐ„ ๋ณต์žก๋„์ด๋‹ค..

[JavaScript] LeetCode : Add Binary
๐Ÿ““ Algorithm/LeetCode 2022. 4. 22. 17:25

๋ฌธ์ œ Given two binary strings a and b, return their sum as a binary string. 2์ง„์ˆ˜ ๋ฌธ์ž์—ด a,b๊ฐ€ ์ฃผ์–ด์ง€๋ฉด ์ด๋“ค์„ ๋”ํ•œ ๊ฐ’์„ ๋‹ค์‹œ 2์ง„์ˆ˜ ๋ฌธ์ž์—ด๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ํ’€์ด let addBinary = function (a, b) { let sum = Bigint('0b' a) + BigInt('0b', 2); let result = sum.toString(2); return result; }; ์ฒ˜์Œ์— ์ƒ๊ฐํ•œ ๊ฒƒ์€ ๋ฌธ์ž์—ด์ธ a์™€ b๋ฅผ ์ •์ˆ˜ํ™” ํ•˜์—ฌ ๋”ํ•˜๋Š” ๊ฒƒ์ด์—ˆ๋‹ค. ๊ทธ๋Ÿฌ๊ธฐ ์œ„ํ•ด parseInt ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ–ˆ์—ˆ๋‹ค. let sum = parseInt(a, 2) + parseInt(b, 2); ๋ณดํ†ต parseInt์— ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฌธ์ž์—ด๋งŒ ๋„ฃ์œผ๋ฉด ๋ฌธ์ž์—ด์„ ๋ฐ”..

[JavaScript] LeetCode : Plus One
๐Ÿ““ Algorithm/LeetCode 2022. 4. 21. 21:00

๋ฌธ์ œ You are given a large integer represented as an integer array digits, where each digits[i] is the ith digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading 0's. Increment the large integer by one and return the resulting array of digits. ์ •์ˆ˜์˜ ๋ฐฐ์—ด๋กœ ํ‘œ์‹œ๋˜๋Š” ํฐ ์ •์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ฐ ์š”์†Œ๋Š” ์ •์ˆ˜์˜ ์ž๋ฆฟ์ˆ˜์ด๋‹ค. ์ˆซ์ž๋Š” ์™ผ์ชฝ->์˜ค๋ฅธ..

[JavaScript] LeetCode : Largest Number At Least Twice of Others
๐Ÿ““ Algorithm/LeetCode 2022. 4. 20. 22:10

๋ฌธ์ œ You are given an integer array nums where the largest integer is unique. Determine whether the largest element in the array is at least twice as much as every other number in the array. If it is, return the index of the largest element, or return -1 otherwise. ์ฃผ์–ด์ง€๋Š” ์ •์ˆ˜์˜ ๋ฐฐ์—ด์—์„œ ๊ฐ€์žฅ ํฐ ์ˆ˜๊ฐ€ ๋ฐฐ์—ด ๋‚ด ๋‹ค๋ฅธ ์ˆ˜์˜ ์ตœ์†Œ ๋‘ ๋ฐฐ ์ด์ƒ์ธ์ง€ ํ™•์ธํ•˜๊ณ , ์กฐ๊ฑด์— ์ถฉ์กฑ๋œ๋‹ค๋ฉด ๊ฐ€์žฅ ํฐ ์ˆ˜์˜ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š๋‹ค๋ฉด -1์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ํ’€์ด var dominantIndex = function..

[JavaScript] LeetCode : Find Pivot Index
๐Ÿ““ Algorithm/LeetCode 2022. 4. 20. 21:46

๋ฌธ์ œ Given an array of integers nums, calculate the pivot index of this array. The pivot index is the index where the sum of all the numbers strictly to the left of the index is equal to the sum of all the numbers strictly to the index's right. If the index is on the left edge of the array, then the left sum is 0 because there are no elements to the left. This also applies to the right edge of the..

[JS] ์•Œ๊ณ ๋ฆฌ์ฆ˜ : ๋‚˜๋ฌด ๊ทธ๋ฆฌ๊ธฐ (๋ณ„์ฐ๊ธฐ)
๐Ÿ““ Algorithm 2022. 4. 18. 22:27

๋ฌธ์ œ ์กฐ์นด๊ฐ€ ๋‚˜๋ฌด ๊ทธ๋ฆฌ๊ธฐ๋ฅผ ์–ด๋ ค์›Œ ํ•˜๊ณ  ์žˆ๋‹ค. ์–ด๋ฆฐ ์กฐ์นด๋ฅผ ์œ„ํ•ด ๋‚˜๋ฌด๋ฅผ ๊ทธ๋ ค์ฃผ๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“ค์–ด์ฃผ์ž. ์ž์—ฐ์ˆ˜๋ฅผ ๋†’์ด๋กœ ์ž…๋ ฅ ๋ฐ›๊ณ  ๋Œ€์นญํ˜• ํ˜•ํƒœ๋กœ ๋‚˜๋ฌด ๋ฌธ์ž์—ด์„ ๋งŒ๋“ค์–ด ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๊ฐ ํ–‰ ๋ณ„๋กœ ๊ฐœํ–‰ ๋ฌธ์ž(\n)๋ฅผ ๋„ฃ์–ด์ฃผ๋ฉด์„œ *์„ ์ฐ์œผ๋ฉฐ ์ถœ๋ ฅ๊ฐ’ ํ˜•ํƒœ๋กœ ๋‚˜๋ฌด๋ฅผ ๊ทธ๋ ค์ค€๋‹ค. ์ž…๋ ฅ๊ฐ’ 3,5,7 ์ถœ๋ ฅ๊ฐ’ ๐Ÿ‘ฉ‍๐Ÿ’ป ํ’€์–ด๋ณด๊ธฐ function answer(height) { let str = '\n'; for (let i = 0; i < height; i++) { str += ' '.repeat(height - i - 1) + '*'.repeat(2 * i + 1) + '\n'; } return str; } ๋จผ์ € ์œ„ ์‚ฌ์ง„๊ณผ ๊ฐ™์ด ๊ณต๋ฐฑ๋ถ€๋ถ„๊ณผ *์ด ์ฐํžˆ๋Š” ๋ถ€๋ถ„์„ ๋‚˜๋ˆ„์–ด ์ƒ๊ฐํ•ด์•ผ ํ•œ๋‹ค. ๊ณต๋ฐฑ์€ ๋†’์ด๊ฐ€ 5์ผ ๋•Œ 4๋ถ€ํ„ฐ ์ˆœ์ฐจ์ ์œผ๋กœ ์ค„์–ด๋“ค..

[JS] ์•Œ๊ณ ๋ฆฌ์ฆ˜ : ์ผ๊ณฑ ๋‚œ์Ÿ์ด
๐Ÿ““ Algorithm 2022. 4. 15. 23:53

๋ฌธ์ œ ์ผํ„ฐ์— ๋‚˜๊ฐ”๋˜ ๋‚œ์Ÿ์ด 9๋ช…์ด ์™€์„œ๋Š” ๋ชจ๋‘ ์ž๊ธฐ๊ฐ€ ์ผ๊ณฑ ๋‚œ์Ÿ์ด์ค‘ ํ•˜๋‚˜๋ผ๊ณ  ์šฐ๊ธฐ๊ณ  ์žˆ๋‹ค. ๋ชจ๋“  ๋‚œ์Ÿ์ด์˜ ๊ฐ€์Šด์—๋Š” ์ˆซ์ž๊ฐ€ ํ‘œ์‹œ๋œ ๋ฐฐ์ง€๊ฐ€ ์žˆ๋Š”๋ฐ, ๋‹คํ–‰ํžˆ๋„ ์ผ๊ณฑ ๋‚œ์Ÿ์ด์˜ ๋ฐฐ์ง€์— ํ‘œ์‹œ๋œ ์ˆซ์ž์˜ ํ•ฉ์ด 100์ด๋ผ๋Š” ๋‹จ์„œ๋กœ ์ผ๊ณฑ ๋‚œ์Ÿ์ด๋ฅผ ๋ถ„๋ณ„ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ผ๊ณฑ ๋‚œ์Ÿ์ด๋ฅผ ๋ถ„๋ณ„ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ๋ฐฐ์ง€ ๊ฐ’์€ 100์ดํ•˜ ์ž์—ฐ์ˆ˜๋กœ ๋“ค์–ด์˜ค๋ฉฐ, ์ผ๊ณฑ ๋‚œ์Ÿ์ด์˜ ๋ฐฐ์ง€ ๊ฐ’์„ ๊ธฐ์กด ์ˆœ์„œ๋Œ€๋กœ ๋ฐฐ์—ด์— ๋„ฃ์–ด ๋ฐ˜ํ™˜ํ•œ๋‹ค. ++ ๋‹จ, 2๋ช…์„ ์ œ์™ธํ•ด์„œ 100์ด ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋Š” 1๊ฐœ ๋ฟ์ด๋‹ค. ๐Ÿ‘ฉ‍๐Ÿ’ป ํ’€์–ด๋ณด๊ธฐ function answer(dwarf) { let result = []; let sum = dwarf.reduce(function (prev, curr) { return prev + curr; }); sum -= 100;..

[JS] ์•Œ๊ณ ๋ฆฌ์ฆ˜ : ๋‘ ์ˆ˜์˜ ์ตœ๋Œ€ ํ•ฉ
๐Ÿ““ Algorithm 2022. 4. 14. 21:48

๋ฌธ์ œ ์ˆ˜์—ด์ด ์ฃผ์–ด์งˆ ๋•Œ, ์ด์ค‘ ๋‘ ๊ฐœ์˜ ์ˆ˜๋ฅผ ์„ ํƒํ•˜์—ฌ ์ตœ๋Œ€ ํ•ฉ์ด ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋„๋ก ํ”„๋กœ๊ทธ๋žจ์„ ์ œ์ž‘ํ•˜๋ผ. ์ž…๋ ฅ์€ ์ •์ˆ˜๋กœ ๋œ ๋ฐฐ์—ด์„ ๋ฐ›๊ณ , ์ตœ๋Œ€ ํ•ฉ์ด ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋Š” ๋‘ ์ˆ˜๋ฅผ ๋ฐฐ์—ด ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๋ฐฐ์—ด๋กœ ์ž…๋ ฅ๋˜๋Š” ์ •์ˆ˜๋Š” 10 ~ 20๊ฐœ ์‚ฌ์ด์ด๋ฉฐ, ์ •์ˆ˜์˜ ๋ฒ”์œ„๋Š” -20 ~ +20 ์‚ฌ์ด์˜ ๊ฐ’์ด ์ž…๋ ฅ๋œ๋‹ค. ์ž…๋ ฅ๊ฐ’ [-11, 5, 18, -2, -3, 6, 4, 17, 10, 9] [3, 7, -14, 2, -6, 13, -20, -2, -7, 6, -17, -5, 14, -9, 19] [-15, -4, -8, 12, 12, -8, -8, 9, 10, 15, -2, 10, -14, 2, 13, 19, -9, 3, -18, 14] ์ถœ๋ ฅ๊ฐ’ #1 [18, 17] #2 [19, 14] #3 [19, 15] ๐Ÿ‘ฉ‍๐Ÿ’ป ํ’€์–ด๋ณด๊ธฐ ..

[JS] ์•Œ๊ณ ๋ฆฌ์ฆ˜ : ์ฒด์Šค ์„ธํŠธ
๐Ÿ““ Algorithm 2022. 4. 14. 21:42

๋ฌธ์ œ ์˜ค๋ž˜๋œ ์ฐฝ๊ณ ์—์„œ ์ฒด์ŠคํŒ๊ณผ ์ฒด์Šค ๊ธฐ๋ฌผ์„ ๋ฐœ๊ฒฌํ–ˆ๋‹ค. ๋ถˆํ–‰ํžˆ๋„ ๊ธฐ๋ฌผ๋ณ„ ๊ฐœ์ˆ˜๊ฐ€ ๋ถ€์กฑํ•˜๊ฑฐ๋‚˜ ๋งŽ์•„, ์™„์ „ํ•œ ํ•œ ์„ธํŠธ๋ฅผ ์ด๋ฃจ๊ณ  ์žˆ์ง€ ๋ชปํ•˜๊ณ  ์žˆ์–ด ๋ณด์ธ๋‹ค. ๊ฒŒ์ž„์„ ํ•˜๊ธฐ ์œ„ํ•ด ๋ถ€์กฑํ•˜๊ฑฐ๋‚˜ ๋งŽ์€ ๊ธฐ๋ฌผ์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ œ์ž‘ํ•˜์‹œ์˜ค. ๊ธฐ๋ฌผ์˜ ๊ฐœ์ˆ˜๋Š” ๋ฐฐ์—ด ํ˜•ํƒœ๋กœ ์•„๋ž˜์™€ ๊ฐ™์ด king๋ถ€ํ„ฐ pawns ์ˆœ์œผ๋กœ ๋“ค์–ด์˜ค๋ฉฐ ํ•œ ๊ฒŒ์ž„์„ ํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ๊ธฐ๋ฌผ์˜ ๊ฐœ์ˆ˜๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค. ์ˆœ์„œ ๋ฐ ๊ธฐ๋ฌผ ํ•„์š” ๊ฐœ์ˆ˜: king(1), queen(1), rooks(2), bishops(2), knights(2), pawns(8) ์ž…๋ ฅ๊ฐ’ [0,1,2,2,2,7] [2,1,2,1,2,1] [0,1,1,5,3,6] ์ถœ๋ ฅ๊ฐ’ #1 [1,0,0,0,0,1] #2 [-1,0,0,1,0,7] #3 [1,0,1,-3,1,2] ๐Ÿ‘ฉ‍๐Ÿ’ป ํ’€์–ด๋ณด๊ธฐ f..

[JS] ์•Œ๊ณ ๋ฆฌ์ฆ˜ : ์ตœ์†Œ๊ฐ’ ์œ„์น˜
๐Ÿ““ Algorithm 2022. 4. 14. 21:36

๋ฌธ์ œ ์ˆ˜์—ด์ด ์ฃผ์–ด์งˆ ๋•Œ, ์ด ์ˆ˜์—ด์˜ ์žˆ๋Š” ์ˆ˜ ์ค‘ ์ตœ์†Œ๊ฐ’์˜ ์œ„์น˜๋ฅผ ๋ชจ๋‘ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ์ž…๋ ฅ์€ ์ž์—ฐ์ˆ˜๋กœ ๋œ ๋ฐฐ์—ด์„ ๋ฐ›๊ณ , ์‹œ์ž‘ ์œ„์น˜๋Š” 0์œผ๋กœ ๊ณ„์‚ฐํ•˜์—ฌ ์ตœ์†Œ๊ฐ’์˜ ์œ„์น˜๋ฅผ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๋ชจ๋“  ์ˆ˜๋Š” 100์ดํ•˜์˜ ์ž์—ฐ์ˆ˜๋กœ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. ์ž…๋ ฅ๊ฐ’ [5, 2, 10, 2] [4, 5, 7, 4, 8] [12, 11, 11, 16, 11, 12] ์ถœ๋ ฅ๊ฐ’ #1 [1, 3] #2 [0, 3] #3 [1, 2, 4] ๐Ÿ‘ฉ‍๐Ÿ’ป ํ’€์–ด๋ณด๊ธฐ function answer(nums) { let result = []; let min = nums[0]; for (let i = 0; i nums[i]) { min = nums[i]; } } for (let i = 0; i