File tree 4 files changed +121
-0
lines changed
kth-smallest-element-in-a-bst
4 files changed +121
-0
lines changed Original file line number Diff line number Diff line change
1
+ // time : O(n)
2
+ // space : O(n)
3
+
4
+ class Solution {
5
+ public boolean containsDuplicate (int [] nums ) {
6
+ Set <Integer > set = new HashSet <>();
7
+ for (int i : nums ) {
8
+ if (set .contains (i )) {
9
+ return true ;
10
+ }
11
+ set .add (i );
12
+ }
13
+ return false ;
14
+ }
15
+ }
16
+
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for a binary tree node.
3
+ * public class TreeNode {
4
+ * int val;
5
+ * TreeNode left;
6
+ * TreeNode right;
7
+ * TreeNode() {}
8
+ * TreeNode(int val) { this.val = val; }
9
+ * TreeNode(int val, TreeNode left, TreeNode right) {
10
+ * this.val = val;
11
+ * this.left = left;
12
+ * this.right = right;
13
+ * }
14
+ * }
15
+ */
16
+ class Solution {
17
+
18
+ private int count = 0 ;
19
+ private TreeNode resultNode = null ;
20
+
21
+ public int kthSmallest (TreeNode root , int k ) {
22
+ searchNode (root , k );
23
+ return resultNode .val ;
24
+ }
25
+
26
+ public void searchNode (TreeNode node , int k ) {
27
+
28
+ if (resultNode != null ) return ;
29
+
30
+ if (node .left != null ) {
31
+ searchNode (node .left , k );
32
+ }
33
+
34
+ count ++;
35
+
36
+ if (count == k ) {
37
+ resultNode = node ;
38
+ return ;
39
+ }
40
+
41
+ if (node .right != null ) {
42
+ searchNode (node .right , k );
43
+ }
44
+ }
45
+ }
46
+
47
+ // n : ๋
ธ๋ ๊ฐ์
48
+ // time : O(n) ์ต์
์ ๊ฒฝ์ฐ ๋ชจ๋ ๋
ธ๋๋ฅผ ํ์ํด์ผํจ
49
+ // space : O(n) ์ต์
์ ๊ฒฝ์ฐ ํ ์ชฝ์ผ๋ก ๋
ธ๋๊ฐ ์น์ฐ์ณ์ ธ ์์
50
+ // -> ์ฌ๊ท ํธ์ถ์ด ์ด๋ฃจ์ด์ง๋ฏ๋ก ์คํ์ ์์ -> ํ ์ชฝ์ผ๋ก ์ ๋ ค ์์ผ๋ฉด ํธ๋ฆฌ์ ๋์ด๊ฐ n์ด ๋จ (ํธ๋ฆฌ์ ์ต๋ ๋์ด๊ฐ ์คํ์ ์ต๋ ๊น์ด)
Original file line number Diff line number Diff line change
1
+ // time : O(1)
2
+ // space : O(1)
3
+
4
+ class Solution {
5
+ public int hammingWeight (int n ) {
6
+ int count = 0 ;
7
+
8
+ while (n != 0 ) {
9
+ if ((n &1 ) == 1 ) count ++;
10
+ n = n >> 1 ;
11
+ }
12
+
13
+ return count ;
14
+ }
15
+ }
16
+
Original file line number Diff line number Diff line change
1
+
2
+ class Solution {
3
+ public int [] topKFrequent (int [] nums , int k ) {
4
+ Map <Integer , Integer > map = new HashMap <>();
5
+ for (int num : nums ) {
6
+ map .put (num , map .getOrDefault (num , 0 ) + 1 );
7
+ }
8
+
9
+ PriorityQueue <Map .Entry <Integer , Integer >> queue = new PriorityQueue <>(
10
+ (a , b ) -> Integer .compare (b .getValue (), a .getValue ())
11
+ );
12
+
13
+ for (Map .Entry <Integer , Integer > entry : map .entrySet ()) {
14
+ queue .offer (entry );
15
+ }
16
+
17
+ int [] res = new int [k ];
18
+
19
+ for (int i = 0 ; i < k ; i ++) {
20
+ res [i ] = queue .poll ().getKey ();
21
+ }
22
+
23
+ return res ;
24
+ }
25
+ }
26
+
27
+ // n : nums์ ๊ธธ์ด
28
+ // m : nums์์ ์๋ก ๋ค๋ฅธ ์ซ์์ ๊ฐ์
29
+
30
+ // time : O(n) + O(m*logm) + O(k*logm) = O(n + m*logm + k*logm)
31
+ // ์ต์
์ ๊ฒฝ์ฐ, nums ๊ฐ ๋ค unique ํ๊ธฐ ๋๋ฌธ์ n == m == k ๊ฐ ๋จ
32
+ // ๋ฐ๋ผ์, O(n*logn)
33
+
34
+ // space : O(m) + O(m) + O(k) = O(m + k)
35
+ // ์ต์
์ ๊ฒฝ์ฐ n == m == k ๊ฐ ๋จ
36
+ // ๋ฐ๋ผ์, O(n)
37
+
38
+
39
+
You canโt perform that action at this time.
0 commit comments