File tree 5 files changed +142
-0
lines changed
encode-and-decode-strings
longest-consecutive-sequence
product-of-array-except-self
5 files changed +142
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * time: O(n^2)
3
+ * space: O(n)
4
+ *
5
+ * - time: becasue of two nested loop and inner loop having a linear time complexity.
6
+ * - space: because of a HashSet to store the triplets.
7
+ */
8
+ class Solution {
9
+
10
+ public List <List <Integer >> threeSum (int [] nums ) {
11
+ Arrays .sort (nums );
12
+ int i = 0 , j , k ;
13
+ int ni = 0 , nj , nk ;
14
+ Set <List <Integer >> res = new HashSet <>();
15
+ while (i < nums .length && ni <= 0 ) {
16
+ ni = nums [i ];
17
+ j = i + 1 ;
18
+ k = nums .length - 1 ;
19
+ while (j < k ) {
20
+ nj = nums [j ];
21
+ nk = nums [k ];
22
+ int sum = ni + nj + nk ;
23
+ if (sum < 0 ) {
24
+ j ++;
25
+ } else if (sum > 0 ) {
26
+ k --;
27
+ } else {
28
+ res .add (List .of (ni , nj , nk ));
29
+ j ++;
30
+ }
31
+ }
32
+ i ++;
33
+ }
34
+ return res .stream ()
35
+ .toList ();
36
+ }
37
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * time: O(N)
3
+ * space: O(N)
4
+ */
5
+ public class Codec {
6
+
7
+ // Encodes a list of strings to a single string.
8
+ public String encode (List <String > strs ) {
9
+ StringBuilder sb = new StringBuilder ();
10
+ for (String str : strs ) {
11
+ sb .append (str .length ())
12
+ .append (':' )
13
+ .append (str );
14
+ }
15
+ return sb .toString ();
16
+ }
17
+
18
+ // Decodes a single string to a list of strings.
19
+ public List <String > decode (String s ) {
20
+ List <String > decoded = new ArrayList <>();
21
+ int i = 0 ;
22
+ while (i < s .length ()) {
23
+ int searchIndex = s .indexOf (':' , i );
24
+ int chunkSize = Integer .parseInt (s .substring (i , searchIndex ));
25
+ i = searchIndex + chunkSize + 1 ;
26
+ decoded .add (s .substring (searchIndex + 1 , i ));
27
+ }
28
+ return decoded ;
29
+ }
30
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * time: O(N)
3
+ * space: O(N)
4
+ */
5
+ class Solution {
6
+
7
+ public int longestConsecutive (int [] nums ) {
8
+ Set <Integer > numSet = new HashSet <>();
9
+ for (int n : nums ) {
10
+ numSet .add (n );
11
+ }
12
+ int longest = 0 ;
13
+ for (int n : nums ) {
14
+ if (numSet .contains (n - 1 )) {
15
+ continue ;
16
+ }
17
+ int seq = 1 ;
18
+ while (numSet .contains (n + seq )) {
19
+ seq ++;
20
+ }
21
+ longest = Math .max (longest , seq );
22
+ }
23
+ return longest ;
24
+ }
25
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * time: O(N)
3
+ * space: O(N)
4
+ */
5
+ class Solution {
6
+ public int [] productExceptSelf (int [] nums ) {
7
+ int len = nums .length ;
8
+ int [] res = new int [len ];
9
+ int [] left = Arrays .copyOf (nums , len );
10
+ int [] right = Arrays .copyOf (nums , len );
11
+ for (int i = 1 ; i < len ; i ++) {
12
+ left [i ] *= left [i - 1 ];
13
+ }
14
+ for (int i = len - 2 ; i >= 0 ; i --) {
15
+ right [i ] *= right [i + 1 ];
16
+ }
17
+ for (int i = 1 ; i < len - 1 ; i ++) {
18
+ res [i ] = left [i - 1 ] * right [i + 1 ];
19
+ }
20
+ res [0 ] = right [1 ];
21
+ res [len - 1 ] = left [len - 2 ];
22
+ return res ;
23
+ }
24
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * time: O(N log k)
3
+ * space: O(N + k)
4
+ *
5
+ * - time: N ๊ฐ๋ฅผ ์ํํ๋ฉฐ heap ์ ์์ ์ถ๊ฐ, ์ต๋ k ๊ฐ๋งํผ ์์๋ฅผ ์ถ๊ฐํ์ฌ log k ๋งํผ ์๊ฐ์ด ๊ณฑํด์ง.
6
+ * - space: ๋น๋ ์๋ฅผ ์ ์ฅํ๊ธฐ ์ํด N, ํ์ ์์๋ฅผ ์ ์ฅํ๊ธฐ ์ํด K ๋งํผ ์ ์ฅ ๊ณต๊ฐ์ ์ฌ์ฉํจ.
7
+ */
8
+ class Solution {
9
+
10
+ public int [] topKFrequent (int [] nums , int k ) {
11
+ Map <Integer , Integer > freqMap = new HashMap <>();
12
+ for (int n : nums ) {
13
+ freqMap .put (n , freqMap .getOrDefault (n , 0 ) + 1 );
14
+ }
15
+ Queue <Integer > pq = new PriorityQueue <>(Comparator .comparingInt (freqMap ::get ));
16
+ for (Integer n : freqMap .keySet ()) {
17
+ pq .add (n );
18
+ if (pq .size () > k ) {
19
+ pq .poll ();
20
+ }
21
+ }
22
+ return IntStream .range (0 , k )
23
+ .map (i -> pq .poll ())
24
+ .toArray ();
25
+ }
26
+ }
You canโt perform that action at this time.
0 commit comments