Skip to content

Commit 8312890

Browse files
replace dsa code
1 parent 2fe2315 commit 8312890

File tree

2 files changed

+47
-104
lines changed

2 files changed

+47
-104
lines changed
Lines changed: 24 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,25 @@
1-
/* A Naive Java Program for LIS Implementation */
2-
class LIS
3-
{
4-
static int max_ref; // stores the LIS
5-
6-
/* To make use of recursive calls, this function must return
7-
two things:
8-
1) Length of LIS ending with element arr[n-1]. We use
9-
max_ending_here for this purpose
10-
2) Overall maximum as the LIS may end with an element
11-
before arr[n-1] max_ref is used this purpose.
12-
The value of LIS of full array of size n is stored in
13-
*max_ref which is our final result */
14-
static int _lis(int arr[], int n)
15-
{
16-
// base case
17-
if (n == 1)
18-
return 1;
19-
20-
// 'max_ending_here' is length of LIS ending with arr[n-1]
21-
int res, max_ending_here = 1;
22-
23-
/* Recursively get all LIS ending with arr[0], arr[1] ...
24-
arr[n-2]. If arr[i-1] is smaller than arr[n-1], and
25-
max ending with arr[n-1] needs to be updated, then
26-
update it */
27-
for (int i = 1; i < n; i++)
28-
{
29-
res = _lis(arr, i);
30-
if (arr[i-1] < arr[n-1] && res + 1 > max_ending_here)
31-
max_ending_here = res + 1;
32-
}
33-
34-
// Compare max_ending_here with the overall max. And
35-
// update the overall max if needed
36-
if (max_ref < max_ending_here)
37-
max_ref = max_ending_here;
38-
39-
// Return length of LIS ending with arr[n-1]
40-
return max_ending_here;
41-
}
42-
43-
// The wrapper function for _lis()
44-
static int lis(int arr[], int n)
45-
{
46-
// The max variable holds the result
47-
max_ref = 1;
48-
49-
// The function _lis() stores its result in max
50-
_lis( arr, n);
51-
52-
// returns max
53-
return max_ref;
54-
}
55-
56-
// driver program to test above functions
57-
public static void main(String args[])
58-
{
59-
int arr[] = { 10, 22, 9, 33, 21, 50, 41, 60 };
60-
int n = arr.length;
61-
System.out.println("Length of LIS is "
62-
+ lis(arr, n) + "n");
63-
}
1+
class Solution {
2+
// problem statement and judging: https://leetcode.com/problems/longest-increasing-subsequence
3+
public int lengthOfLIS(int[] nums) {
4+
int len = nums.length;
5+
6+
int[] dp = new int[len];
7+
8+
for(int i=0; i<len; ++i) { dp[i] = 1; }
9+
10+
for(int i=0; i<len; ++i) {
11+
for(int j=0; j<i; ++j) {
12+
if( nums[j] < nums[i] ) {
13+
dp[i] = Math.max(dp[i], dp[j]+1);
14+
}
15+
}
16+
}
17+
18+
int max_dp = 1;
19+
for(int i=1; i<len; ++i) {
20+
max_dp = Math.max(dp[i], max_dp);
21+
}
22+
23+
return max_dp;
24+
}
6425
}
Lines changed: 23 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,27 @@
1-
// A Naive recursive Java solution for Edit Distance
2-
// to find minimum number operations to convert str1 to str2
3-
class EDIST
4-
{
5-
static int min(int x,int y,int z)
6-
{
7-
if (x<=y && x<=z) return x;
8-
if (y<=x && y<=z) return y;
9-
else return z;
10-
}
1+
class Solution {
2+
// problem statement and judging: https://leetcode.com/problems/edit-distance
3+
public int minDistance(String word1, String word2) {
4+
int n1 = word1.length(), n2 = word2.length();
115

12-
static int editDist(String str1 , String str2 , int m ,int n)
13-
{
14-
// If first string is empty, the only option is to
15-
// insert all characters of second string into first
16-
if (m == 0) return n;
17-
18-
// If second string is empty, the only option is to
19-
// remove all characters of first string
20-
if (n == 0) return m;
21-
22-
// If last characters of two strings are same, nothing
23-
// much to do. Ignore last characters and get count for
24-
// remaining strings.
25-
if (str1.charAt(m-1) == str2.charAt(n-1))
26-
return editDist(str1, str2, m-1, n-1);
27-
28-
// If last characters are not same, consider all three
29-
// operations on last character of first string, recursively
30-
// compute minimum cost for all three operations and take
31-
// minimum of three values.
32-
return 1 + min ( editDist(str1, str2, m, n-1), // Insert
33-
editDist(str1, str2, m-1, n), // Remove
34-
editDist(str1, str2, m-1, n-1) // Replace
35-
);
36-
}
6+
if(n1==0)
7+
return n2;
8+
if(n2==0)
9+
return n1;
3710

38-
public static void main(String args[])
39-
{
40-
String str1 = "sunday";
41-
String str2 = "saturday";
11+
int dp[][]=new int[n1+1][n2+1];
4212

43-
System.out.println( editDist( str1 , str2 , str1.length(), str2.length()) );
44-
}
13+
for(int i=0;i<=n1;i++){
14+
for(int j=0;j<=n2;j++){
15+
if( i==0 )
16+
dp[i][j] = j;
17+
else if( j==0 )
18+
dp[i][j]=i;
19+
else if( word1.charAt(i-1) == word2.charAt(j-1) )
20+
dp[i][j] = dp[i-1][j-1];
21+
else
22+
dp[i][j] = Math.min( dp[i-1][j-1], Math.min( dp[i-1][j],dp[i][j-1] ) ) + 1;
23+
}
24+
}
25+
return dp[n1][n2];
26+
}
4527
}

0 commit comments

Comments
 (0)