File tree Expand file tree Collapse file tree 5 files changed +130
-0
lines changed
kth-smallest-element-in-a-bst Expand file tree Collapse file tree 5 files changed +130
-0
lines changed Original file line number Diff line number Diff line change
1
+ // Time complexity: o(n log n) ์๋ํ๋ฉด, for ๋ฌธ์ด n์ด๊ณ find ์ฐ์ฐ์ด log n์ด๊ธฐ ๋๋ฌธ.
2
+ // Space complexity: O(n) ์๋ํ๋ฉด, ๋ค ๋ค๋ฅธ๊ฒฝ์ฐ n
3
+
4
+ class Solution {
5
+ public:
6
+ bool containsDuplicate (vector<int >& nums) {
7
+ set<int > unique;
8
+ for (int i : nums) {
9
+ if (unique.find (i) != unique.end ()) {
10
+ return true ;
11
+ }
12
+ unique.insert (i);
13
+ }
14
+ return false ;
15
+ }
16
+ };
Original file line number Diff line number Diff line change
1
+ TC: O(n log n) ์๋ํ๋ฉด, heap์ push์ฐ์ฐ์ด log n์ด๊ณ ๊ฐ ๋
ธ๋๋ง๋ค ์คํํ๋ฏ๋ก.
2
+ SC: O(n) queue๊ฐ n๊น์ง ์ปค์ง์ ์์.
3
+
4
+ int kthSmallest (TreeNode* root, int k) {
5
+ priority_queue<int , vector<int >, greater<int >> minHeap;
6
+
7
+ // bfs
8
+ queue<TreeNode*> q;
9
+ if (root != nullptr ) {
10
+ q.push (root);
11
+ };
12
+ while (!q.empty ()) {
13
+ minHeap.push (q.front ()->val );
14
+ if (q.front ()->left != nullptr ) {
15
+ q.push (q.front ()->left );
16
+ }
17
+ if (q.front ()->right != nullptr ) {
18
+ q.push (q.front ()->right );
19
+ }
20
+ q.pop ();
21
+ }
22
+
23
+ for (int i = 0 ; i < k - 1 ; i++) {
24
+ minHeap.pop ();
25
+ }
26
+ return minHeap.top ();
27
+ }
Original file line number Diff line number Diff line change
1
+ // Time complexity: O(n) ์๋ํ๋ฉด, while๋ฌธ
2
+ // Space complexity: O(1)
3
+
4
+ class Solution {
5
+ public:
6
+ int hammingWeight (int n) {
7
+ int cnt = 0 ;
8
+ while (n>0 ){
9
+ int val = n%2 ;
10
+ cnt+=val;
11
+ n/=2 ;
12
+ }
13
+ return cnt;
14
+ }
15
+ };
Original file line number Diff line number Diff line change
1
+ // TC: O(n^2) For๋ฌธ ์์์ while๋ฌธ ์ฌ์ฉํ๋ ํจ์ ํธ์ถ
2
+ // SC: O(1)
3
+
4
+ class Solution {
5
+ public:
6
+ int find_palindrome1 (string& original_text, int index) {
7
+ int cnt = 0 ;
8
+ int left_ptr = index, right_ptr = index;
9
+
10
+ while (left_ptr >= 0 and right_ptr < original_text.size () and
11
+ original_text[left_ptr] == original_text[right_ptr]) {
12
+ cnt += 1 ;
13
+ left_ptr -= 1 ;
14
+ right_ptr += 1 ;
15
+ }
16
+ return cnt;
17
+ }
18
+
19
+ int find_palindrome2 (string& original_text, int index) {
20
+ int cnt = 0 ;
21
+ int left_ptr = index, right_ptr = index + 1 ;
22
+
23
+ while (left_ptr >= 0 and right_ptr < original_text.size () and
24
+ original_text[left_ptr] == original_text[right_ptr]) {
25
+ cnt++;
26
+ left_ptr--;
27
+ right_ptr++;
28
+ }
29
+ return cnt;
30
+ }
31
+
32
+ int countSubstrings (string& s) {
33
+ int output = 0 ;
34
+ for (int i = 0 ; i < s.size (); i++) {
35
+ output += find_palindrome1 (s, i);
36
+ }
37
+ for (int i = 0 ; i < s.size () - 1 ; i++) {
38
+ output += find_palindrome2 (s, i);
39
+ }
40
+ return output;
41
+ }
42
+ };
Original file line number Diff line number Diff line change
1
+ // TC: O(n logn)
2
+ // SC: O(n)
3
+
4
+ vector<int > topKFrequent (vector<int >& nums, int k) {
5
+ unordered_map<int , int > frequency;
6
+ for (int n : nums) {
7
+ frequency[n] += 1 ;
8
+ }
9
+ auto comparator = [&frequency](int n1, int n2) {
10
+ return frequency[n1] > frequency[n2];
11
+ };
12
+
13
+ priority_queue<int , vector<int >, decltype (comparator)> min_heap (
14
+ comparator);
15
+
16
+ for (auto & entry : frequency) {
17
+ min_heap.push (entry.first );
18
+ if (min_heap.size () > k) {
19
+ min_heap.pop ();
20
+ }
21
+ }
22
+
23
+ vector<int > output;
24
+
25
+ for (int i = 0 ; i < k; i++) {
26
+ output.push_back (min_heap.top ());
27
+ min_heap.pop ();
28
+ }
29
+ return output;
30
+ }
You canโt perform that action at this time.
0 commit comments