From a74616df6e443a0fbffde83f33bd82d889509cea Mon Sep 17 00:00:00 2001 From: YeonguChoe Date: Sat, 10 Aug 2024 23:47:30 -0400 Subject: [PATCH 1/7] Create yeongu.cpp --- contains-duplicate/yeongu.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 contains-duplicate/yeongu.cpp diff --git a/contains-duplicate/yeongu.cpp b/contains-duplicate/yeongu.cpp new file mode 100644 index 000000000..2afd5659b --- /dev/null +++ b/contains-duplicate/yeongu.cpp @@ -0,0 +1,13 @@ +class Solution { +public: + bool containsDuplicate(vector& nums) { + set unique; + for (int i : nums) { + if (unique.find(i) != unique.end()) { + return true; + } + unique.insert(i); + } + return false; + } +}; From ec54f9bad9f2259ac2d6b3c08bd47520c66a5449 Mon Sep 17 00:00:00 2001 From: YeonguChoe Date: Sat, 10 Aug 2024 23:55:12 -0400 Subject: [PATCH 2/7] Create yeongu.cpp --- number-of-1-bits/yeongu.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 number-of-1-bits/yeongu.cpp diff --git a/number-of-1-bits/yeongu.cpp b/number-of-1-bits/yeongu.cpp new file mode 100644 index 000000000..1951308b7 --- /dev/null +++ b/number-of-1-bits/yeongu.cpp @@ -0,0 +1,12 @@ +class Solution { +public: + int hammingWeight(int n) { + int cnt = 0; + while(n>0){ + int val = n%2; + cnt+=val; + n/=2; + } + return cnt; + } +}; From 6c4376ea90d21b982b5974bd0a883b73d90e9d89 Mon Sep 17 00:00:00 2001 From: YeonguChoe Date: Sun, 11 Aug 2024 02:06:54 -0400 Subject: [PATCH 3/7] Create yeongu.cpp --- top-k-frequent-elements/yeongu.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 top-k-frequent-elements/yeongu.cpp diff --git a/top-k-frequent-elements/yeongu.cpp b/top-k-frequent-elements/yeongu.cpp new file mode 100644 index 000000000..da612e80e --- /dev/null +++ b/top-k-frequent-elements/yeongu.cpp @@ -0,0 +1,30 @@ +// TC: O(n logn) +// SC: O(n) + +vector topKFrequent(vector& nums, int k) { + unordered_map frequency; + for (int n : nums) { + frequency[n] += 1; + } + auto comparator = [&frequency](int n1, int n2) { + return frequency[n1] > frequency[n2]; + }; + + priority_queue, decltype(comparator)> min_heap( + comparator); + + for (auto& entry : frequency) { + min_heap.push(entry.first); + if (min_heap.size() > k) { + min_heap.pop(); + } + } + + vector output; + + for (int i = 0; i < k; i++) { + output.push_back(min_heap.top()); + min_heap.pop(); + } + return output; +} From d4c79cc74fc2ff77a2d0852e995a839c58aa7784 Mon Sep 17 00:00:00 2001 From: YeonguChoe Date: Sun, 11 Aug 2024 11:20:25 -0400 Subject: [PATCH 4/7] Create yeongu.cpp --- kth-smallest-element-in-a-bst/yeongu.cpp | 27 ++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 kth-smallest-element-in-a-bst/yeongu.cpp diff --git a/kth-smallest-element-in-a-bst/yeongu.cpp b/kth-smallest-element-in-a-bst/yeongu.cpp new file mode 100644 index 000000000..3c13c0844 --- /dev/null +++ b/kth-smallest-element-in-a-bst/yeongu.cpp @@ -0,0 +1,27 @@ +TC: O(n log n) 왜냐하면, heap에 push연산이 log n이고 각 노드마다 실행하므로. +SC: O(n) queue가 n까지 커질수 있음. + + int kthSmallest(TreeNode* root, int k) { + priority_queue, greater> minHeap; + + // bfs + queue q; + if (root != nullptr) { + q.push(root); + }; + while (!q.empty()) { + minHeap.push(q.front()->val); + if (q.front()->left != nullptr) { + q.push(q.front()->left); + } + if (q.front()->right != nullptr) { + q.push(q.front()->right); + } + q.pop(); + } + + for (int i = 0; i < k - 1; i++) { + minHeap.pop(); + } + return minHeap.top(); + } From 68bc62b42501ecff8812da051198306ff6a2b5e7 Mon Sep 17 00:00:00 2001 From: YeonguChoe Date: Sun, 11 Aug 2024 13:06:08 -0400 Subject: [PATCH 5/7] Create yeongu.cpp --- palindromic-substrings/yeongu.cpp | 42 +++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 palindromic-substrings/yeongu.cpp diff --git a/palindromic-substrings/yeongu.cpp b/palindromic-substrings/yeongu.cpp new file mode 100644 index 000000000..274ea6eba --- /dev/null +++ b/palindromic-substrings/yeongu.cpp @@ -0,0 +1,42 @@ +// TC: O(n^2) For문 안에서 while문 사용하는 함수 호출 +// SC: O(1) + +class Solution { +public: + int find_palindrome1(string& original_text, int index) { + int cnt = 0; + int left_ptr = index, right_ptr = index; + + while (left_ptr >= 0 and right_ptr < original_text.size() and + original_text[left_ptr] == original_text[right_ptr]) { + cnt += 1; + left_ptr -= 1; + right_ptr += 1; + } + return cnt; + } + + int find_palindrome2(string& original_text, int index) { + int cnt = 0; + int left_ptr = index, right_ptr = index + 1; + + while (left_ptr >= 0 and right_ptr < original_text.size() and + original_text[left_ptr] == original_text[right_ptr]) { + cnt++; + left_ptr--; + right_ptr++; + } + return cnt; + } + + int countSubstrings(string& s) { + int output = 0; + for (int i = 0; i < s.size(); i++) { + output += find_palindrome1(s, i); + } + for (int i = 0; i < s.size() - 1; i++) { + output += find_palindrome2(s, i); + } + return output; + } +}; From 9401aaa226b5a62d5394f5db2eb09c8e3ef64bb9 Mon Sep 17 00:00:00 2001 From: YeonguChoe Date: Tue, 13 Aug 2024 03:05:30 -0400 Subject: [PATCH 6/7] Update yeongu.cpp --- number-of-1-bits/yeongu.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/number-of-1-bits/yeongu.cpp b/number-of-1-bits/yeongu.cpp index 1951308b7..cd17eeb10 100644 --- a/number-of-1-bits/yeongu.cpp +++ b/number-of-1-bits/yeongu.cpp @@ -1,3 +1,6 @@ +// Time complexity: O(n) 왜냐하면, while문 +// Space complexity: O(1) + class Solution { public: int hammingWeight(int n) { From a457daec48243fda0b795bb4c6413effe41cf350 Mon Sep 17 00:00:00 2001 From: YeonguChoe Date: Tue, 13 Aug 2024 03:09:11 -0400 Subject: [PATCH 7/7] Update yeongu.cpp --- contains-duplicate/yeongu.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/contains-duplicate/yeongu.cpp b/contains-duplicate/yeongu.cpp index 2afd5659b..1357aabbe 100644 --- a/contains-duplicate/yeongu.cpp +++ b/contains-duplicate/yeongu.cpp @@ -1,3 +1,6 @@ +// Time complexity: o(n log n) 왜냐하면, for 문이 n이고 find 연산이 log n이기 때문. +// Space complexity: O(n) 왜냐하면, 다 다른경우 n + class Solution { public: bool containsDuplicate(vector& nums) {