From 345e956b7438a6b48a92dd9d46924ae1ffffa78a Mon Sep 17 00:00:00 2001 From: yeeZinu Date: Wed, 11 Dec 2024 19:26:28 +0900 Subject: [PATCH 1/7] two sum solution --- two-sum/yeeZinu.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 two-sum/yeeZinu.js diff --git a/two-sum/yeeZinu.js b/two-sum/yeeZinu.js new file mode 100644 index 000000000..54c408f7a --- /dev/null +++ b/two-sum/yeeZinu.js @@ -0,0 +1,30 @@ +/** + * @param {number[]} nums + * @param {number} target + * @return {number[]} + */ + +/* +두 수 더하기 +nums: 숫자 배열 +target: 목표 숫자 + +target 숫자에 맞게 nums 배열중 2개를 더함. + return은 더한 두 수의 index + - for i in nums -> target - i 값이 배열내에 있는지 찾기 +*/ +var twoSum = function (nums, target) { + let indexArray = []; // 답을 저장할 배열 + for (let i = 0; i < nums.length; i++) { + let tNum = nums.lastIndexOf(target - nums[i]); // 배열내에 target - nums[i] 찾기 + if (i === tNum) { // 같은 수 사용 방지 + + } + else if (tNum > 0 && indexArray.lastIndexOf(i) === -1) { // 배열내에 원하는 값이 있다면 + indexArray.push(i); + indexArray.push(tNum); + break; + } + } + return indexArray; +}; From 833cfcf879d60244ad672fc313899c0cc3ecccf1 Mon Sep 17 00:00:00 2001 From: yeeZinu Date: Wed, 11 Dec 2024 22:21:21 +0900 Subject: [PATCH 2/7] contains duplicate solution --- contains-duplicate/yeeZinu.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 contains-duplicate/yeeZinu.js diff --git a/contains-duplicate/yeeZinu.js b/contains-duplicate/yeeZinu.js new file mode 100644 index 000000000..4c712b90d --- /dev/null +++ b/contains-duplicate/yeeZinu.js @@ -0,0 +1,15 @@ +/** + * @param {number[]} nums + * @return {boolean} + + nums 배열내의 아무 정수나 2개 이상 중복되면 true를 반복하는 함수. + + 시간 복잡도: O(n) + */ +var containsDuplicate = function (nums) { + return nums.length !== new Set(nums).size +} + +console.log(containsDuplicate([1, 2, 3, 1])); // true +// console.log(containsDuplicate([1, 2, 3, 4])); // false +// console.log(containsDuplicate([1, 1, 1, 3, 3, 4, 3, 2, 4, 2])); // true \ No newline at end of file From 7816391328929fee32b9f84713d7b7cb2153510a Mon Sep 17 00:00:00 2001 From: yeeZinu Date: Thu, 12 Dec 2024 15:44:46 +0900 Subject: [PATCH 3/7] =?UTF-8?q?=20cl=20=EA=B0=9C=ED=96=89=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contains-duplicate/yeeZinu.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contains-duplicate/yeeZinu.js b/contains-duplicate/yeeZinu.js index 4c712b90d..b2e481a1d 100644 --- a/contains-duplicate/yeeZinu.js +++ b/contains-duplicate/yeeZinu.js @@ -12,4 +12,4 @@ var containsDuplicate = function (nums) { console.log(containsDuplicate([1, 2, 3, 1])); // true // console.log(containsDuplicate([1, 2, 3, 4])); // false -// console.log(containsDuplicate([1, 1, 1, 3, 3, 4, 3, 2, 4, 2])); // true \ No newline at end of file +// console.log(containsDuplicate([1, 1, 1, 3, 3, 4, 3, 2, 4, 2])); // true From a5e4185bcdb01d02648a78c5a6f61a51e3843903 Mon Sep 17 00:00:00 2001 From: yeeZinu Date: Thu, 12 Dec 2024 17:18:22 +0900 Subject: [PATCH 4/7] valid parentheses solve --- valid-parentheses/yeeZinu.js | 43 ++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 valid-parentheses/yeeZinu.js diff --git a/valid-parentheses/yeeZinu.js b/valid-parentheses/yeeZinu.js new file mode 100644 index 000000000..1516d5d7b --- /dev/null +++ b/valid-parentheses/yeeZinu.js @@ -0,0 +1,43 @@ +/** + * @param {string} s + * @return {boolean} + + 괄호 유효성 검사( + 어느 괄호로 열어도 상관은 없지만 열었으면 닫아야지 true 안닫으면 false + ({)} -> 이런식으로 중간에 섞여서 닫혀있지 않다는 전제의 문제 + */ +var isValid = function (s) { + if (s.length % 2 === 1) { + return false + } + + // 괄호들이 들어갈 스택배열 + const stack = []; + + // 괄호 짝 객체 + const pair = { + "(": ")", + "{": "}", + "[": "]", + } + + for (let i = 0; i < s.length; i++) { + // 열린괄호면 스택추가 + if (s[i] in pair) { + stack.push(s[i]); + } + // 닫힌 괄호라면? + else { + // 스택배열의 마지막이 같은 종류의 괄호라면 제거 + if (pair[stack.at(-1)] === s[i]) { + stack.pop(); + } + // 아니면 false + else { + return false + } + } + } + // 스택배열이 비었으면 true + return stack.length === 0; +}; From 59b14a86edca7a67a812e1446a689c537ff1429f Mon Sep 17 00:00:00 2001 From: yeeZinu Date: Thu, 12 Dec 2024 19:18:59 +0900 Subject: [PATCH 5/7] top k frequent elements solution --- top-k-frequent-elements/yeeZinu.js | 31 ++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 top-k-frequent-elements/yeeZinu.js diff --git a/top-k-frequent-elements/yeeZinu.js b/top-k-frequent-elements/yeeZinu.js new file mode 100644 index 000000000..c783f47e8 --- /dev/null +++ b/top-k-frequent-elements/yeeZinu.js @@ -0,0 +1,31 @@ +/** + * @param {number[]} nums + * @param {number} k + * @return {number[]} + * + * nums 배열 내 최빈도 숫자 k개 출력 + * + */ +var topKFrequent = function (nums, k) { + // 빈도 체크할 객체 + let frequent = {}; + + for (let i = 0; i < nums.length; i++) { + // 숫자 중복될때마다 +1 + if (frequent[nums[i]] > 0) { + frequent[nums[i]]++; + } + // 없으면 1로 초기화 + else { + frequent[nums[i]] = 1; + } + } + + // 정렬을 위해 entries를 사용해 배열로 변환 + const frequentEntries = Object.entries(frequent); + frequentEntries.sort((a, b) => b[1] - a[1]); // 내림차순 정렬 + + // k갯수만큼 배열 자르기 및 배열 내부 값 number로 변경 + const topK = frequentEntries.slice(0, k).map((i) => Number(i[0])); + return topK; +}; From 54cbbb5c0210fd48bb6d2f1167e45d1acc134db8 Mon Sep 17 00:00:00 2001 From: yeeZinu Date: Thu, 12 Dec 2024 21:07:10 +0900 Subject: [PATCH 6/7] longest consecutive sequence solution --- longest-consecutive-sequence/yeeZinu.js | 29 +++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 longest-consecutive-sequence/yeeZinu.js diff --git a/longest-consecutive-sequence/yeeZinu.js b/longest-consecutive-sequence/yeeZinu.js new file mode 100644 index 000000000..83b024ac0 --- /dev/null +++ b/longest-consecutive-sequence/yeeZinu.js @@ -0,0 +1,29 @@ +/** + * @param {number[]} nums + * @return {number} + */ +var longestConsecutive = function (nums) { + // set으로 중복된 element 제거 + const numSet = new Set(nums); + // 최대 연속 길이 변수 + let maxLength = 0; + + // 최대 연속 길이 찾기 + for (let num of numSet) { + // 만약 현재 수 -1이 없다면? + if (!numSet.has(num - 1)) { + let currentNum = num; //현재값이 시작 + let currentLength = 1; //최대 길이 1로 초기화 + + // 현재값 +1이 있을 때 까지 반복 + while (numSet.has(currentNum + 1)) { + currentNum++; + currentLength++; + } + + // 최대길이 값과 현재 길이값중 더 높은것이 최대길이값 + maxLength = Math.max(maxLength, currentLength); + } + } + return maxLength; +}; From 243f35d3921d1714b7a0b979c05eb60a2cbfc638 Mon Sep 17 00:00:00 2001 From: yeeZinu Date: Thu, 12 Dec 2024 23:58:04 +0900 Subject: [PATCH 7/7] house robber solution --- house-robber/yeeZinu.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 house-robber/yeeZinu.js diff --git a/house-robber/yeeZinu.js b/house-robber/yeeZinu.js new file mode 100644 index 000000000..d8d80fc83 --- /dev/null +++ b/house-robber/yeeZinu.js @@ -0,0 +1,23 @@ +var rob = function (nums) { + const n = nums.length; + + // 길이가 1이라면 + if (n === 1) { + return nums[0]; + } + + // nums의 길이만큼 0으로 초기화된 배열 + const dp = Array(n).fill(0); + + // 0번은 nums[0] + dp[0] = nums[0]; + // 1번은 0과 1중 큰것을 선택 + dp[1] = Math.max(nums[0], nums[1]); + + // i-1 과 i + i-2 의 합중 더 큰것을 선택하면됨 + for (let i = 2; i < n; i++) { + dp[i] = Math.max(dp[i - 1], nums[i] + dp[i - 2]); + } + // i가 n - 1까지 반복함 + return dp[n - 1]; +};