File tree Expand file tree Collapse file tree 5 files changed +123
-0
lines changed
longest-common-subsequence
longest-increasing-subsequence Expand file tree Collapse file tree 5 files changed +123
-0
lines changed Original file line number Diff line number Diff line change 1+ // time: O(N)
2+ // space: O(1)
3+ class Solution {
4+
5+ public boolean canJump (int [] nums ) {
6+ int lastPosition = nums .length - 1 ;
7+
8+ for (int i = nums .length - 1 ; i >= 0 ; i --) {
9+ if (i + nums [i ] >= lastPosition ) {
10+ lastPosition = i ;
11+ }
12+ }
13+ return lastPosition == 0 ;
14+ }
15+ }
Original file line number Diff line number Diff line change 1+ // time: O(m*n)
2+ // space: O(m*n)
3+ class Solution {
4+
5+ public int longestCommonSubsequence (String text1 , String text2 ) {
6+ int [][] dp = new int [text1 .length () + 1 ][text2 .length () + 1 ];
7+
8+ for (int col = text2 .length () - 1 ; col >= 0 ; col --) {
9+ for (int row = text1 .length () - 1 ; row >= 0 ; row --) {
10+ if (text1 .charAt (row ) == text2 .charAt (col )) {
11+ dp [row ][col ] = 1 + dp [row + 1 ][col + 1 ];
12+ } else {
13+ dp [row ][col ] = Math .max (dp [row + 1 ][col ], dp [row ][col + 1 ]);
14+ }
15+ }
16+ }
17+
18+ return dp [0 ][0 ];
19+ }
20+ }
Original file line number Diff line number Diff line change 1+ // time: O(N^2)
2+ // space: O(N)
3+ class Solution {
4+
5+ public int lengthOfLIS (int [] nums ) {
6+ int [] dp = new int [nums .length ];
7+ Arrays .fill (dp , 1 );
8+
9+ for (int i = 1 ; i < nums .length ; i ++) {
10+ for (int j = 0 ; j < i ; j ++) {
11+ if (nums [i ] > nums [j ]) {
12+ dp [i ] = Math .max (dp [i ], dp [j ] + 1 );
13+ }
14+ }
15+ }
16+
17+ int longest = 0 ;
18+ for (int count : dp ) {
19+ longest = Math .max (longest , count );
20+ }
21+
22+ return longest ;
23+ }
24+ }
Original file line number Diff line number Diff line change 1+ // time: O(N)
2+ // space: O(1)
3+ class Solution {
4+
5+ public int maxSubArray (int [] nums ) {
6+ int currentSum = nums [0 ];
7+ int maxSum = nums [0 ];
8+
9+ for (int i = 1 ; i < nums .length ; i ++) {
10+ int k = nums [i ];
11+ currentSum = Math .max (k , currentSum + k );
12+ maxSum = Math .max (maxSum , currentSum );
13+ }
14+
15+ return maxSum ;
16+ }
17+ }
Original file line number Diff line number Diff line change 1+ /*
2+ [Approch 1]
3+ - time: O(m*n)
4+ - space: O(m*n)
5+ */
6+ class Solution {
7+
8+ public int uniquePaths (int m , int n ) {
9+ int [][] dp = new int [m ][n ];
10+
11+ for (int [] row : dp ) {
12+ Arrays .fill (row , 1 );
13+ }
14+
15+ for (int row = 1 ; row < m ; row ++) {
16+ for (int col = 1 ; col < n ; col ++) {
17+ dp [row ][col ] = dp [row - 1 ][col ] + dp [row ][col - 1 ];
18+ }
19+ }
20+
21+ return dp [m - 1 ][n - 1 ];
22+ }
23+ }
24+
25+ /*
26+ [Approch 2]
27+ - time: O(m*n)
28+ - space: O(n)
29+ */
30+ class Solution {
31+
32+ public int uniquePaths (int m , int n ) {
33+ int [] upper = new int [n ];
34+
35+ Arrays .fill (upper , 1 );
36+
37+ for (int i = 1 ; i < m ; i ++) {
38+ int left = 1 ;
39+ for (int j = 1 ; j < n ; j ++) {
40+ left += upper [j ];
41+ upper [j ] = left ;
42+ }
43+ }
44+
45+ return upper [n - 1 ];
46+ }
47+ }
You can’t perform that action at this time.
0 commit comments