[JavaScript] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค : OXํ€ด์ฆˆ
๐Ÿ““ Algorithm 2022. 12. 12. 20:48

๋ฌธ์ œ ๋ง์…ˆ, ๋บ„์…ˆ ์ˆ˜์‹๋“ค์ด 'X [์—ฐ์‚ฐ์ž] Y = Z' ํ˜•ํƒœ๋กœ ๋“ค์–ด์žˆ๋Š” ๋ฌธ์ž์—ด ๋ฐฐ์—ด quiz๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ์ˆ˜์‹์ด ์˜ณ๋‹ค๋ฉด "O"๋ฅผ ํ‹€๋ฆฌ๋‹ค๋ฉด "X"๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ๋‹ด์€ ๋ฐฐ์—ด์„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”. ์ž…์ถœ๋ ฅ ์˜ˆ quiz result ["3 - 4 = -3", "5 + 6 = 11"] ["X", "O"] ["19 - 6 = 13", "5 + 66 = 71", "5 - 15 = 63", "3 - 1 = 2"] ["O", "O", "X", "O"] ํ’€์ด =์„ ๊ธฐ์ค€์œผ๋กœ ์™ผ์ชฝ์˜ ์—ฐ์‚ฐ์ด ์˜ค๋ฅธ์ชฝ์˜ ์ˆ˜์™€ ์ผ์น˜ํ•˜๋Š”์ง€๋ฅผ ๋”ฐ์ ธ O, X๋กœ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฌธ์ œ๋‹ค. quiz ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ๋ฅผ ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ split ํ•ด์•ผ๊ฒ ๋‹ค๋Š” ์ƒ๊ฐ์ด ๋จผ์ € ๋“ค์—ˆ๋‹ค. function solution(quiz..

[Javascript] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค : ์˜ˆ์‚ฐ
๐Ÿ““ Algorithm 2022. 12. 9. 20:43

๋ฌธ์ œ S์‚ฌ์—์„œ๋Š” ๊ฐ ๋ถ€์„œ์— ํ•„์š”ํ•œ ๋ฌผํ’ˆ์„ ์ง€์›ํ•ด ์ฃผ๊ธฐ ์œ„ํ•ด ๋ถ€์„œ๋ณ„๋กœ ๋ฌผํ’ˆ์„ ๊ตฌ๋งคํ•˜๋Š”๋ฐ ํ•„์š”ํ•œ ๊ธˆ์•ก์„ ์กฐ์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜, ์ „์ฒด ์˜ˆ์‚ฐ์ด ์ •ํ•ด์ ธ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ชจ๋“  ๋ถ€์„œ์˜ ๋ฌผํ’ˆ์„ ๊ตฌ๋งคํ•ด ์ค„ ์ˆ˜๋Š” ์—†์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ตœ๋Œ€ํ•œ ๋งŽ์€ ๋ถ€์„œ์˜ ๋ฌผํ’ˆ์„ ๊ตฌ๋งคํ•ด ์ค„ ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋ฌผํ’ˆ์„ ๊ตฌ๋งคํ•ด ์ค„ ๋•Œ๋Š” ๊ฐ ๋ถ€์„œ๊ฐ€ ์‹ ์ฒญํ•œ ๊ธˆ์•ก๋งŒํผ์„ ๋ชจ๋‘ ์ง€์›ํ•ด ์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด 1,000์›์„ ์‹ ์ฒญํ•œ ๋ถ€์„œ์—๋Š” ์ •ํ™•ํžˆ 1,000์›์„ ์ง€์›ํ•ด์•ผ ํ•˜๋ฉฐ, 1,000์›๋ณด๋‹ค ์ ์€ ๊ธˆ์•ก์„ ์ง€์›ํ•ด ์ค„ ์ˆ˜๋Š” ์—†์Šต๋‹ˆ๋‹ค. ๋ถ€์„œ๋ณ„๋กœ ์‹ ์ฒญํ•œ ๊ธˆ์•ก์ด ๋“ค์–ด์žˆ๋Š” ๋ฐฐ์—ด d์™€ ์˜ˆ์‚ฐ budget์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ์ตœ๋Œ€ ๋ช‡ ๊ฐœ์˜ ๋ถ€์„œ์— ๋ฌผํ’ˆ์„ ์ง€์›ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”. ์ž…์ถœ๋ ฅ ์˜ˆ์‹œ d budget resu..

[JavaScript] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค : ๋ฐฐ์—ด ํšŒ์ „ ์‹œํ‚ค๊ธฐ
๐Ÿ““ Algorithm 2022. 12. 2. 20:34

๋ฌธ์ œ ์ •์ˆ˜๊ฐ€ ๋‹ด๊ธด ๋ฐฐ์—ด numbers์™€ ๋ฌธ์ž์—ด direction๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ๋ฐฐ์—ด numbers์˜ ์›์†Œ๋ฅผ direction๋ฐฉํ–ฅ์œผ๋กœ ํ•œ ์นธ์”ฉ ํšŒ์ „์‹œํ‚จ ๋ฐฐ์—ด์„ returnํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”. ์ž…์ถœ๋ ฅ ์˜ˆ์‹œ [1, 2, 3] "right" [3, 1, 2] [4, 455, 6, 4, -1, 45, 6] "left" [455, 6, 4, -1, 45, 6, 4] ํ’€์ด ์ฒ˜์Œ์— ๋ฐฉํ–ฅ์— ๋”ฐ๋ผ ๋ฐฐ์—ด์ด ์–ด๋–ป๊ฒŒ ๋ฐ”๋€Œ๋Š” ๊ฒƒ์ธ์ง€ ์ดํ•ด๋ฅผ ์ž˜ ๋ชปํ–ˆ์—ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ „์ฒด์ ์œผ๋กœ for๋ฌธ์„ ๋Œ์•„์•ผํ•˜๋Š” ๊ฑธ๊นŒ ??? ์‹ถ๋‹ค๊ฐ€ ๋ฌธ์ œ๋ฅผ ๋‹ค์‹œ ๋ณด๊ณ  ํ์™€ ๋น„์Šทํ•˜๊ฒ ๊ตฌ๋‚˜ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค. ํ•œ ์นธ์”ฉ ํšŒ์ „์‹œํ‚จ๋‹ค๋Š” ๋ง์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค. ๋ฐฉํ–ฅ์ด ์˜ค๋ฅธ์ชฝ์ด๋ผ๋ฉด ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ ์š”์†Œ๋ฅผ ๋นผ์„œ ๋งจ ์•ž์œผ๋กœ ๋„ฃ์–ด์ฃผ๊ณ , ์™ผ์ชฝ์ด๋ผ๋ฉด ๊ฐ€์žฅ ๋งจ ์•ž ์š”์†Œ๋ฅผ..

[JavaScript] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค : ์ตœ๋นˆ๊ฐ’ ๊ตฌํ•˜๊ธฐ
๐Ÿ““ Algorithm 2022. 11. 15. 21:03

๋ฌธ์ œ ์ตœ๋นˆ๊ฐ’์€ ์ฃผ์–ด์ง„ ๊ฐ’ ์ค‘์—์„œ ๊ฐ€์žฅ ์ž์ฃผ ๋‚˜์˜ค๋Š” ๊ฐ’์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ •์ˆ˜ ๋ฐฐ์—ด array๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ์ตœ๋นˆ๊ฐ’์„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด๋ณด์„ธ์š”. ์ตœ๋นˆ๊ฐ’์ด ์—ฌ๋Ÿฌ ๊ฐœ๋ฉด -1์„ return ํ•ฉ๋‹ˆ๋‹ค. ์ž…์ถœ๋ ฅ ์˜ˆ์‹œ๋ฅผ ๋ณด๋ฉด ์ตœ๋นˆ๊ฐ’์ด 1๊ฐœ์ธ ๊ฒฝ์šฐ์—๋Š” ๊ทธ ์ตœ๋นˆ๊ฐ’์— ํ•ด๋‹นํ•˜๋Š” ์›์†Œ๋ฅผ, ์ตœ๋นˆ๊ฐ’์ด ์—ฌ๋Ÿฌ ๊ฐœ์ผ ๋•Œ๋Š” -1์„ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ํ•œ๋‹ค. ํ’€์ด ๊ณผ์ • ์ž๋ฃŒ๊ตฌ์กฐ ์ค‘ ํ•˜๋‚˜์ธ Map์„ ์ด์šฉํ•ด์„œ ํ’€๋ ค๊ณ  ํ–ˆ๋Š”๋ฐ, 70์ ๊นŒ์ง€๋ฐ–์— ๋ฐ›์ง€ ๋ชปํ•ด์„œ (4๊ฐœ์˜ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ์‹คํŒจ ๐Ÿ˜‚...) ๊ฒฐ๊ตญ ๋น„์Šทํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ํ’€์ดํ•œ ๋ถ„์„ ์ฐธ๊ณ ํ•ด์„œ Map ์ดˆ๊ธฐํ™” ๋ถ€๋ถ„์„ ์ถ”๊ฐ€ํ–ˆ๋‹ค. ใ… ใ…  ์ฒ˜์Œ์— ์ƒ๊ฐํ•œ ๊ณผ์ •์€ ์ด๋žฌ๋‹ค. Map ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉฐ Map์— ๋นˆ๋„์ˆ˜๋ฅผ ์—…๋ฐ์ดํŠธํ•œ๋‹ค. ์ตœ๋Œ€ ์ตœ๋นˆ๊ฐ’์„ ์ฐพ๋Š”๋‹ค. ์ตœ๋นˆ๊ฐ’์ด ์—ฌ๋Ÿฌ ๊ฐœ์ธ์ง€..

[JavaScript] leetCode : Single Number
๐Ÿ““ Algorithm/LeetCode 2022. 5. 26. 16:09

๋ฌธ์ œ Given a non-empty array of integers nums, every element appears twice except for one. Find that single one. You must implement a solution with a linear runtime complexity and use only constant extra space. ์ •์ˆ˜์˜ ๋ฐฐ์—ด์—์„œ ๋™์ผํ•˜๊ฒŒ ์ง์ง€์–ด์ง€์ง€ ์•Š๋Š” ํ•˜๋‚˜์˜ ์ˆ˜๋ฅผ ์ฐพ์•„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฌธ์ œ. ํ’€์ด let result = nums[0]; for (let i = 1; i < nums.length; i++) { result = result ^ nums[i]; } return result; ๊ฐ„๋‹จํ•ด๋ณด์ด๋ฉด์„œ๋„ ๋ฌ˜ํ•˜๊ฒŒ ๊ฐ„๋‹จํ•˜์ง€ ์•Š์•„์„œ ๊ฒฐ๊ตญ ๋‹ค๋ฅธ ์ฝ”๋“œ๋ฅผ ๋ณด๊ฒŒ ๋˜์—ˆ๋‹ค..

[JavaScript] leetCode : Reverse Words in a String
๐Ÿ““ Algorithm/LeetCode 2022. 5. 16. 20:25

๋ฌธ์ œ Given an input string s, reverse the order of the words. A word is defined as a sequence of non-space characters. The words in s will be separated by at least one space. Return a string of the words in reverse order concatenated by a single space. Note that s may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separa..

[JavaScript] leetCode : Implement strStr()
๐Ÿ““ Algorithm/LeetCode 2022. 5. 12. 22:53

๋ฌธ์ œ Given two strings needle and haystack, return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. ๋‘๊ฐœ์˜ ๋ฌธ์ž์—ด needle, haystack์ด ์ฃผ์–ด์งˆ ๋•Œ, haystack์—์„œ needle์˜ ์ฒซ๋ฒˆ์งธ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ , needle์ด haystack์˜ ํ•œ ๋ถ€๋ถ„์ด ์•„๋‹ˆ๋ผ๋ฉด -1์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ํ’€์ด const strStr = function(haystack, needle) { if (needle.length === 0) return 0; let index = -1; if (haystack.includes(needle)) { let firstLette..

[JavaScript] leetCode : Longest Common Prefix
๐Ÿ““ Algorithm/LeetCode 2022. 5. 9. 23:16

๋ฌธ์ œ Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string "". ํ•œ ๋‹จ์–ด๊ฐ€ ์š”์†Œ์ธ ๋ฐฐ์—ด์ด ์ฃผ์–ด์งˆ ๋•Œ, ์š”์†Œ์˜ ๋ฌธ์ž์—ด์—์„œ ์กด์žฌํ•˜๋Š” ๊ฐ€์žฅ ๊ธด ์ ‘๋‘์‚ฌ๋ฅผ ์ฐพ์•„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์—†๋‹ค๋ฉด ๋นˆ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜. ํ’€์ด const longestCommonPrefix = (strs) => { if (strs.length === 0) return ''; if (strs.length === 1) return strs[0]; let prefix = ''; let prev = ''; for (let i = 0; i < strs[0].lengt..

[JavaScript] leetCode : Linked List Cycle II
๐Ÿ““ Algorithm/LeetCode 2022. 5. 3. 23:56

๋ฌธ์ œ Given the head of a linked list, return the node where the cycle begins. If there is no cycle, return null. There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail's next pointer is connected to (0-indexed). It is -1 if there is no cycle. Note tha..

[JavaScript] leetCode : Valid Mountain Array
๐Ÿ““ Algorithm/LeetCode 2022. 5. 2. 21:57

๋ฌธ์ œ Given an array of integers arr, return true if and only if it is a valid mountain array. Recall that arr is a mountain array if and only if arr.length >= 3 There exists some i with 0 arr[i + 1] > ... > arr[arr.length - 1] ๋ฐฐ์—ด์ด ์‚ฐ์ฒ˜๋Ÿผ ์˜ค๋ฆ„์ฐจ์ˆœ-๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ด๋ฃจ์–ด์ ธ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์—ฌ boolean์œผ๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฌธ์ œ. ์กฐ๊ฑด์€ ๋ฐฐ์—ด์˜ ๊ธธ์ด๊ฐ€ 3 ์ด์ƒ์ด์–ด์•ผ ํ•˜๊ณ , ์˜ค๋ฆ„์ฐจ์ˆœ๊ณผ ๋‚ด๋ฆผ์ฐจ์ˆœ์˜ ..