File tree Expand file tree Collapse file tree 5 files changed +107
-0
lines changed
longest-common-subsequence
longest-repeating-character-replacement Expand file tree Collapse file tree 5 files changed +107
-0
lines changed Original file line number Diff line number Diff line change 1+ // TC: O(n)
2+ // -> visit all elements once for each to clone them
3+ // SC: O(n)
4+ // -> all elements are stored in HashMap and values are limited maximum 2
5+ class Solution {
6+ private Map <Integer , Node > map = new HashMap <>();
7+ public Node cloneGraph (Node node ) {
8+ if (node == null ) return null ;
9+ if (map .containsKey (node .val )) return map .get (node .val );
10+
11+ Node clone = new Node (node .val );
12+ map .put (clone .val , clone );
13+
14+ for (Node neighbor : node .neighbors ) {
15+ clone .neighbors .add (cloneGraph (neighbor ));
16+ }
17+ return clone ;
18+ }
19+ }
Original file line number Diff line number Diff line change 1+ // TC: O(n * m)
2+ // the length of text1 by the length of text2
3+ // SC: O(n)
4+ // both size of text1 and text2 can be the size of dp
5+ class Solution {
6+ public int longestCommonSubsequence (String text1 , String text2 ) {
7+ int [] dp = new int [text2 .length ()];
8+ int output = 0 ;
9+
10+ for (char c : text1 .toCharArray ()) {
11+ int curLength = 0 ;
12+
13+ for (int i = 0 ; i < dp .length ; i ++) {
14+ if (curLength < dp [i ]) curLength = dp [i ];
15+ else if (c == text2 .charAt (i )) {
16+ dp [i ] = curLength + 1 ;
17+ output = Math .max (output , dp [i ]);
18+ }
19+ }
20+ }
21+ return output ;
22+ }
23+ }
Original file line number Diff line number Diff line change 1+ // TC: O(26 * n) => O(n)
2+ // iterates 26 times at the first for-loop, while loop O(n)
3+ // SC: O(1)
4+ class Solution {
5+ public int characterReplacement (String s , int k ) {
6+ int ans = 0 ;
7+ int n = s .length ();
8+ for (char c = 'A' ; c <= 'Z' ; c ++) {
9+ int i = 0 , j = 0 , replaced = 0 ;
10+ while (j < n ) {
11+ if (s .charAt (j ) == c ) {
12+ j += 1 ;
13+ } else if (replaced < k ) {
14+ j += 1 ;
15+ replaced ++;
16+ } else if (s .charAt (i ) == c ) {
17+ i += 1 ;
18+ } else {
19+ i += 1 ;
20+ replaced -= 1 ;
21+ }
22+ ans = Math .max (ans , j - i );
23+ }
24+ }
25+ return ans ;
26+ }
27+ }
Original file line number Diff line number Diff line change 1+ // TC: O(n)
2+ // n = length sum of list1 and list2
3+ // SC: O(n)
4+ // n = node 0 ~ length sum of list1 and list2
5+ class Solution {
6+ public ListNode mergeTwoLists (ListNode list1 , ListNode list2 ) {
7+ ListNode node = new ListNode (0 );
8+ ListNode output = node ;
9+
10+ while (list1 != null && list2 != null ) {
11+ if (list1 .val > list2 .val ) {
12+ node .next = list2 ;
13+ list2 = list2 .next ;
14+ } else {
15+ node .next = list1 ;
16+ list1 = list1 .next ;
17+ }
18+ node = node .next ;
19+ }
20+
21+ if (list1 == null ) node .next = list2 ;
22+ if (list2 == null ) node .next = list1 ;
23+
24+ return output .next ;
25+ }
26+ }
Original file line number Diff line number Diff line change 1+ // TC: O(1)
2+ // SC: O(1)
3+ class Solution {
4+ public int getSum (int a , int b ) {
5+ while (b != 0 ) {
6+ int temp = (a & b ) << 1 ;
7+ a = a ^b ;
8+ b = temp ;
9+ }
10+ return a ;
11+ }
12+ }
You can’t perform that action at this time.
0 commit comments