File tree Expand file tree Collapse file tree 5 files changed +119
-0
lines changed
longest-substring-without-repeating-characters Expand file tree Collapse file tree 5 files changed +119
-0
lines changed Original file line number Diff line number Diff line change 1+ // TC: O(n^2)
2+ // -> all elements can be retrived multiple times in the worst case
3+ // SC: O(1)
4+ // -> since declare, no more increase or decrease
5+ class Solution {
6+ public int lengthOfLongestSubstring (String s ) {
7+ int max = 0 ;
8+ int count = 0 ;
9+ boolean [] checkList = new boolean [128 ];
10+
11+ for (int i = 0 ; i < s .length (); i ++) {
12+ int idx = s .charAt (i );
13+ if (checkList [idx ]) {
14+ max = Math .max (max , count );
15+ i -= count ;
16+ count = 0 ;
17+ checkList = new boolean [128 ];
18+ } else {
19+ count += 1 ;
20+ checkList [idx ] = true ;
21+ }
22+ }
23+ return max = Math .max (max , count );
24+ }
25+ }
Original file line number Diff line number Diff line change 1+ // TC: O(n * m)
2+ // retrieve all elemetns, grid.length * grid[0].length
3+ // SC: O(n * m(
4+ // need to change all elements from 1 to 0 in the worst case
5+ class Solution {
6+ int output = 0 ;
7+ public int numIslands (char [][] grid ) {
8+ for (int i = 0 ; i < grid .length ; i ++) {
9+ for (int j = 0 ; j < grid [0 ].length ; j ++) {
10+ if (grid [i ][j ] == '1' ) {
11+ output += 1 ;
12+ countIslands (i , j , grid );
13+ }
14+ }
15+ }
16+ return output ;
17+ }
18+
19+ private void countIslands (int i , int j , char [][] grid ) {
20+ if (i < 0 || i >= grid .length || j < 0 || j >= grid [0 ].length ) return ;
21+ if (grid [i ][j ] == '0' ) return ;
22+ grid [i ][j ] = '0' ;
23+ countIslands (i +1 , j , grid );
24+ countIslands (i -1 , j , grid );
25+ countIslands (i , j +1 , grid );
26+ countIslands (i , j -1 , grid );
27+ }
28+ }
Original file line number Diff line number Diff line change 1+ // TC: O(n)
2+ // -> visit all elements of head
3+ // SC: O(1)
4+ // -> constant space complexity
5+ class Solution {
6+ public ListNode reverseList (ListNode head ) {
7+ ListNode node = null ;
8+ while (head != null ) {
9+ ListNode temp = head .next ;
10+ head .next = node ;
11+ node = head ;
12+ head = temp ;
13+ }
14+ return node ;
15+ }
16+ }
Original file line number Diff line number Diff line change 1+ // TC: O(n^2)
2+ // SC: O(1)
3+ class Solution {
4+ public void setZeroes (int [][] matrix ) {
5+ boolean firstRow = false , firstCol = false ;
6+
7+ for (int i = 0 ; i < matrix .length ; i ++) {
8+ for (int j = 0 ; j < matrix [0 ].length ; j ++) {
9+ if (matrix [i ][j ] == 0 ) {
10+ if (i == 0 ) firstRow = true ;
11+ if (j == 0 ) firstCol = true ;
12+ matrix [0 ][j ] = 0 ;
13+ matrix [i ][0 ] = 0 ;
14+ }
15+ }
16+ }
17+
18+ for (int i = 1 ; i < matrix .length ; i ++) {
19+ for (int j = 1 ; j < matrix [0 ].length ; j ++) {
20+ if (matrix [i ][0 ] == 0 || matrix [0 ][j ] == 0 ) matrix [i ][j ] = 0 ;
21+ }
22+ }
23+
24+ if (firstRow ) {
25+ for (int j = 0 ; j < matrix [0 ].length ; j ++) matrix [0 ][j ] = 0 ;
26+ }
27+
28+ if (firstCol ) {
29+ for (int i = 0 ; i < matrix .length ; i ++) matrix [i ][0 ] = 0 ;
30+ }
31+ }
32+ }
Original file line number Diff line number Diff line change 1+ // TC: O (m * n)
2+ // SC: O (m * n)
3+ // -> need to retrieve all elements
4+ class Solution {
5+ public int uniquePaths (int m , int n ) {
6+ int [][] dp = new int [m ][n ];
7+
8+ for (int i = 0 ; i < m ; i ++) dp [i ][0 ] = 1 ;
9+ for (int j = 0 ; j < n ; j ++) dp [0 ][j ] = 1 ;
10+
11+ for (int i = 1 ; i < m ; i ++) {
12+ for (int j = 1 ; j < n ; j ++) {
13+ dp [i ][j ] = dp [i - 1 ][j ] + dp [i ][j - 1 ];
14+ }
15+ }
16+ return dp [m -1 ][n -1 ];
17+ }
18+ }
You can’t perform that action at this time.
0 commit comments