Skip to content

Commit f7c05f3

Browse files
authored
Add files via upload
1 parent cbe6811 commit f7c05f3

25 files changed

+971
-0
lines changed
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
int coins[] = {1, 5, 10, 25, 50};
5+
int dp[7500][5];
6+
7+
int coinChange(int amount, int index) {
8+
if (amount == 0) return 1;
9+
if (dp[amount][index] != -1) return dp[amount][index];
10+
11+
int ways = 0;
12+
for (int i=index; i<5; ++i) {
13+
if (amount >= coins[i])
14+
ways += coinChange(amount - coins[i], i);
15+
}
16+
17+
return dp[amount][index] = ways;
18+
}
19+
20+
int main() {
21+
//freopen("in.txt", "r", stdin);
22+
//freopen("out.txt", "w", stdout);
23+
24+
int amount;
25+
memset(dp, -1, sizeof dp);
26+
while (scanf("%d", &amount) == 1) {
27+
printf("%d\n",coinChange(amount, 0));
28+
}
29+
return 0;
30+
}
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package dpAlgorithms;
2+
3+
import java.util.Arrays;
4+
5+
public class CoinChange {
6+
public static int countWays(int s[], int m, int n) {
7+
int[] arr = new int[n+1];
8+
9+
Arrays.fill(arr, 0);
10+
arr[0] = 1;
11+
for(int i=0; i<m; i++) {
12+
for(int j=s[i]; j<=n; j++) {
13+
arr[j] += arr[j-s[i]];
14+
}
15+
}
16+
17+
return arr[n];
18+
}
19+
20+
public static void main(String[] args) {
21+
int arr[] = {1, 2, 3};
22+
int m = arr.length;
23+
int n = 4;
24+
25+
System.out.println(countWays(arr, m, n));
26+
}
27+
}

Algorithms/dpAlgorithms/CoinChange.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
def count(S, m, n ):
2+
3+
if (n == 0):
4+
return 1
5+
6+
if (n < 0):
7+
return 0;
8+
9+
if (m <=0 and n >= 1):
10+
return 0
11+
12+
return count( S, m - 1, n ) + count( S, m, n-S[m-1] );
13+
14+
arr = [1, 2, 3]
15+
m = len(arr)
16+
print(count(arr, m, 4))
17+
18+

Algorithms/dpAlgorithms/LCS.cpp

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include<stdio.h>
2+
#include<string.h>
3+
4+
int max(int a, int b);
5+
void findLCS(char *X, char *Y, int XLen, int YLen);
6+
7+
int max(int a, int b) {
8+
return (a > b)? a : b;
9+
}
10+
11+
void findLCS(char *X, char *Y, int XLen, int YLen) {
12+
int L[XLen + 1][YLen + 1];
13+
int r, c, i;
14+
15+
/// Find LCS
16+
for(r=0; r<=XLen; r++) {
17+
for(c=0; c<=YLen; c++) {
18+
if(r==0 || c==0) L[r][c] = 0;
19+
else if(X[r-1] == Y[c-1]) L[r][c] = L[r-1][c-1] + 1;
20+
else L[r][c] = max(L[r-1][c], L[r][c-1]);
21+
}
22+
}
23+
24+
/// Print LCS
25+
r = XLen;
26+
c = YLen;
27+
i = L[r][c];
28+
29+
char LCS[i+1];
30+
31+
/*
32+
* setting the NULL character at the end of LCS character array.
33+
* as we know in C programming language, NULL character is used
34+
* to denote the end of a string
35+
*/
36+
LCS[i] = '\0';
37+
38+
while(r>0 && c>0) {
39+
40+
if(X[r-1] == Y[c-1]) {
41+
42+
LCS[i-1] = X[r-1];
43+
44+
i--;
45+
r--;
46+
c--;
47+
48+
}
49+
else if(L[r-1][c] > L[r][c-1])
50+
r--;
51+
else
52+
c--;
53+
}
54+
55+
/// print result
56+
printf("Length of the LCS: %d\n", L[XLen][YLen]);
57+
printf("LCS: %s\n", LCS);
58+
}
59+
60+
int main()
61+
{
62+
//the two sequences
63+
char X[] = "ABCF";
64+
char Y[] = "ACF";
65+
66+
//length of the sequences
67+
int XLen = strlen(X);
68+
int YLen = strlen(Y);
69+
70+
findLCS(X, Y, XLen, YLen);
71+
72+
return 0;
73+
}

Algorithms/dpAlgorithms/LCS.cs

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
3+
namespace LCS
4+
{
5+
class Program
6+
{
7+
static void Main(string[] args)
8+
{
9+
string word1 = "thisisatest";
10+
string word2 = "testing123testing";
11+
12+
Console.WriteLine(lcsBack(word1, word2));
13+
Console.ReadKey();
14+
}
15+
16+
public static string lcsBack(string a, string b)
17+
{
18+
string aSub = a.Substring(0, (a.Length - 1 < 0) ? 0 : a.Length - 1);
19+
string bSub = b.Substring(0, (b.Length - 1 < 0) ? 0 : b.Length - 1);
20+
21+
if (a.Length == 0 || b.Length == 0)
22+
return "";
23+
else if (a[a.Length - 1] == b[b.Length - 1])
24+
return lcsBack(aSub, bSub) + a[a.Length - 1];
25+
else
26+
{
27+
string x = lcsBack(a, bSub);
28+
string y = lcsBack(aSub, b);
29+
return (x.Length > y.Length) ? x : y;
30+
}
31+
}
32+
}
33+
}

Algorithms/dpAlgorithms/LCS.java

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package dpAlgorithms;
2+
3+
public class LCS {
4+
5+
public static int getLongestCommonSubsequence(String a, String b){
6+
int m = a.length();
7+
int n = b.length();
8+
int[][] dp = new int[m+1][n+1];
9+
10+
for(int i=0; i<=m; i++){
11+
for(int j=0; j<=n; j++){
12+
if(i==0 || j==0){
13+
dp[i][j]=0;
14+
}else if(a.charAt(i-1)==b.charAt(j-1)){
15+
dp[i][j] = 1 + dp[i-1][j-1];
16+
}else{
17+
dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]);
18+
}
19+
}
20+
}
21+
22+
return dp[m][n];
23+
}
24+
25+
public static void main(String[] args) {
26+
String a = "abcdef";
27+
String b = "acf";
28+
29+
int lcs_length = getLongestCommonSubsequence(a, b);
30+
31+
System.out.println("1st string: "+ a + "\n2nd string: " + b);
32+
System.out.println("LCS length is: " + lcs_length);
33+
}
34+
35+
}

Algorithms/dpAlgorithms/LCS.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
def lcs(a, b):
2+
lengths = [[0 for j in range(len(b)+1)] for i in range(len(a)+1)]
3+
# row 0 and column 0 are initialized to 0 already
4+
for i, x in enumerate(a):
5+
for j, y in enumerate(b):
6+
if x == y:
7+
lengths[i+1][j+1] = lengths[i][j] + 1
8+
else:
9+
lengths[i+1][j+1] = max(lengths[i+1][j], lengths[i][j+1])
10+
# read the substring out from the matrix
11+
result = ""
12+
x, y = len(a), len(b)
13+
while x != 0 and y != 0:
14+
if lengths[x][y] == lengths[x-1][y]:
15+
x -= 1
16+
elif lengths[x][y] == lengths[x][y-1]:
17+
y -= 1
18+
else:
19+
assert a[x-1] == b[y-1]
20+
result = a[x-1] + result
21+
x -= 1
22+
y -= 1
23+
return result

Algorithms/dpAlgorithms/LIS.cpp

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
void printoutput(int *arr, int *out, int curi);
5+
6+
int main()
7+
{
8+
int arr[] = {0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15};
9+
int *res;
10+
int *out;
11+
12+
int len = sizeof(arr)/sizeof(arr[0]);
13+
int max = len > 0 ? 1 : 0;
14+
int n = 1;
15+
int maxi = 0;
16+
int i,j;
17+
18+
res = (int*)malloc(sizeof(int) * len);
19+
out = (int*)malloc(sizeof(int) * len);
20+
for (i=0; i < len; i++) {
21+
res[i] = 1;
22+
out[i] = -1;
23+
}
24+
25+
for (i = 1; i < len; i++) {
26+
n = res[i];
27+
for (j = 0; j < i; j++) {
28+
if ((arr[i] > arr[j]) && (res[j] >= n)) {
29+
n = res[j] + 1;
30+
out[i] = j;
31+
}
32+
}
33+
res[i] = n;
34+
if (n > max) {
35+
maxi = i;
36+
max = n;
37+
}
38+
}
39+
40+
printf("The LIS length is: %d\n", max);
41+
printf("LIS value is:\n");
42+
if (len != 0)
43+
printoutput(arr, out, maxi);
44+
printf("\n");
45+
46+
return 0;
47+
}
48+
49+
void printoutput(int *arr, int *out, int curi) {
50+
if (curi < 0)
51+
return;
52+
53+
if (out[curi] != -1) {
54+
printoutput(arr, out, out[curi]);
55+
}
56+
printf("%d ", arr[curi]);
57+
}

Algorithms/dpAlgorithms/LIS.java

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package dpAlgorithms;
2+
3+
import java.util.Arrays;
4+
5+
public class LIS {
6+
7+
public static int recursiveLIS(int arr[], int i, int n, int prev) {
8+
/*
9+
Complexity: O(2^n)
10+
*/
11+
if (i == n)
12+
return 0;
13+
14+
int excl = recursiveLIS(arr, i+1, n, prev);
15+
16+
int incl = 0;
17+
if (arr[i] > prev) {
18+
incl = 1 + recursiveLIS(arr, i+1, n, arr[i]);
19+
}
20+
21+
return Integer.max(incl, excl);
22+
}
23+
24+
public static int dpLIS(int arr[]) {
25+
/*
26+
Complexity: O(n log n)
27+
*/
28+
int[] L = new int[arr.length];
29+
30+
L[0] = 1;
31+
for(int i=1; i<arr.length; i++) {
32+
for(int j=0; j<i; j++) {
33+
if (arr[j]<arr[i] && L[j]>L[i]) {
34+
L[i] = L[j];
35+
}
36+
}
37+
L[i]++;
38+
}
39+
40+
return Arrays.stream(L).max().getAsInt();
41+
}
42+
43+
public static void main(String[] args) {
44+
int[] arr = {0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15};
45+
46+
///int len = recursiveLIS(arr, 0, arr.length, Integer.MIN_VALUE);
47+
int lis_len = dpLIS(arr);
48+
System.out.println("Length os LIS is: " + lis_len);
49+
}
50+
}
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
int lps(char *);
5+
6+
int main()
7+
{
8+
char str[] = "abcdeRTONUedcba";
9+
int lps_len = lps(str);
10+
printf ("Length of Longest palindromic subsequence is: %d\n", lps_len);
11+
12+
return 0;
13+
}
14+
15+
int lps(char *str) {
16+
int n = strlen(str);
17+
int i, j, cl;
18+
int L[n][n];
19+
20+
21+
// Strings of length 1 are palindrome of length 1
22+
for(i = 0; i < n; i++)
23+
L[i][i] = 1;
24+
25+
for(cl=2; cl<=n; cl++) {
26+
for(i=0; i<n-cl+1; i++) {
27+
j = i+cl-1;
28+
29+
if (str[i] == str[j] && cl == 2) L[i][j] = 2;
30+
else if (str[i] == str[j]) L[i][j] = L[i+1][j-1] + 2;
31+
else L[i][j] = max(L[i][j-1], L[i+1][j]);
32+
}
33+
}
34+
35+
return L[0][n-1];
36+
}
37+

0 commit comments

Comments
 (0)