-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 105ef6b
Showing
24 changed files
with
1,117 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
name: Codeforces solutions using MkDocs | ||
|
||
# Controls when the action will run. Triggers the workflow on push or pull request | ||
# events but only for the main branch | ||
on: | ||
push: | ||
branches: [main] | ||
pull_request: | ||
branches: [main] | ||
|
||
jobs: | ||
build: | ||
name: Codeforces Solutions | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout Main | ||
uses: actions/checkout@v2 | ||
|
||
- name: Set up Python 3.12 | ||
uses: actions/setup-python@v2 | ||
with: | ||
python-version: "3.12" | ||
architecture: x64 | ||
|
||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
python -m pip install -r requirements.txt | ||
- name: Deploy | ||
run: | | ||
git pull | ||
cd codeforces-solutions | ||
mkdocs gh-deploy --force |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
venv/ | ||
codeforces-solutions/site/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"files.associations": { | ||
"iostream": "cpp", | ||
"ostream": "cpp", | ||
"vector": "cpp" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
**Solutions** |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
**Approach 1 : Recursion :sweat_smile:** | ||
|
||
=== "Java" | ||
|
||
```java | ||
``` | ||
|
||
- [x] **Time Complexity :** O(2^(n\*W)) where n is number of items, and W is the capacity of knapsack | ||
|
||
- [x] **Space Complexity :** O(max(n,W)) where n is number of items, and W is the capacity of knapsack | ||
|
||
<hr> | ||
|
||
**Approach 2 : Memoization :smile:** | ||
|
||
=== "Java" | ||
|
||
```java | ||
``` | ||
|
||
- [x] **Time Complexity :** O(n\*W) where n is number of items, and W is the capacity of knapsack | ||
|
||
- [x] **Space Complexity :** O(n\*W) where n is number of items, and W is the capacity of knapsack | ||
|
||
<hr> | ||
|
||
**Approach 3 : Tabulation :smile:** | ||
|
||
=== "Java" | ||
|
||
```java | ||
class Solution { | ||
public static void main(String[] args) { | ||
int W = 50; | ||
int n = 3; | ||
int[] p = {60, 100, 120}; | ||
int[] wt = {10, 20, 30}; | ||
|
||
int[][] dp = new int[n + 1][W + 1]; | ||
|
||
for(int i = 0; i <= n; i++){ | ||
for(int j = 0; j <= W; j++){ | ||
if(i == 0 || j == 0) | ||
continue; | ||
else if(wt[i-1] > j) | ||
dp[i][j] = dp[i-1][j]; | ||
else | ||
dp[i][j] = Math.max(dp[i-1][j], (dp[i-1][j-wt[i-1]]+p[i-1])); | ||
} | ||
} | ||
System.out.println("Maximum Profit = " + dp[n][W]); | ||
} | ||
} | ||
``` | ||
|
||
- [x] **Time Complexity :** O(n\*W) where n is number of items, and W is the capacity of knapsack | ||
|
||
- [x] **Space Complexity :** O(n\*W) where n is number of items, and W is the capacity of knapsack |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
**Approach 1 : Recursion :sweat_smile:** | ||
|
||
=== "Java" | ||
|
||
```java | ||
class Solution { | ||
public static void main(String[] args) { | ||
String s1 = "AGGTAB", s2 = "GXTXAYB"; | ||
|
||
int m = s1.length(), n = s2.length(); | ||
int lcs = solve(m-1, n-1, s1, s2); | ||
System.out.println("Length of LCS = " + lcs); | ||
} | ||
|
||
public static int solve(int i, int j, String s1, String s2){ | ||
// base case | ||
if(i < 0 || j < 0) | ||
return 0; | ||
// if the current characters of both strings match, | ||
// then lcs length = 1 + lcs length for remaining part of these strings | ||
if(s1.charAt(i) == s2.charAt(j)) | ||
return 1 + solve(i-1, j-1, s1, s2); | ||
return Math.max(solve(i-1, j, s1, s2), solve(i, j-1, s1, s2)); | ||
} | ||
} | ||
``` | ||
|
||
- [x] **Time Complexity :** O(2^m\*n^) where m and n are the length of the two strings | ||
|
||
- [x] **Space Complexity :** O(max(m,n)) where m and n are the length of the two strings | ||
|
||
<hr> | ||
|
||
**Approach 2 : Memoization :smile:** | ||
|
||
=== "Java" | ||
|
||
```java | ||
``` | ||
|
||
- [x] **Time Complexity :** O(m\*n) where m and n are the length of the two strings | ||
|
||
- [x] **Space Complexity :** O(m\*n) where m and n are the length of the two strings | ||
|
||
<hr> | ||
|
||
**Approach 3 : Tabulation :smile:** | ||
|
||
=== "Java" | ||
|
||
```java | ||
class Solution { | ||
public static void main(String[] args) { | ||
String s1 = "ABCDGH", s2 = "AEDFHR"; | ||
|
||
int m = s1.length(), n = s2.length(); | ||
int[][] dp = new int[m + 1][n + 1]; | ||
|
||
for(int i = 0; i < m + 1; i++){ | ||
for(int j = 0; j < n + 1; j++){ | ||
if(i == 0 || j == 0) | ||
dp[i][j] = 0; | ||
else if(s1.charAt(i-1) == s2.charAt(j-1)) | ||
dp[i][j] = 1 + dp[i-1][j-1]; | ||
else | ||
dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]); | ||
} | ||
} | ||
int lcs = dp[m][n]; | ||
System.out.println("Length of LCS : " + dp[m][n]); | ||
|
||
char[] str = new char[lcs]; | ||
int i = m, j = n; | ||
while(i > 0 && j > 0){ | ||
if(dp[i-1][j] == dp[i][j]) | ||
i--; | ||
else if(dp[i][j-1] == dp[i][j]) | ||
j--; | ||
else{ | ||
str[--lcs] = s1.charAt(i-1); | ||
i--; | ||
j--; | ||
} | ||
} | ||
System.out.println("LCS is : " + String.valueOf(str)); | ||
} | ||
} | ||
``` | ||
|
||
- [x] **Time Complexity :** O(m\*n) where m and n are the length of the two strings | ||
|
||
- [x] **Space Complexity :** O(m\*n) where m and n are the length of the two strings |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
**Approach : Dynamic Programming - Tabulation :smile:** | ||
|
||
=== "Java" | ||
|
||
```java | ||
class Solution { | ||
public static void main(String[] args) { | ||
String s1 = "abcdaf", s2 = "zbcdf"; | ||
|
||
int m = s1.length(), n = s2.length(); | ||
int[][] dp = new int[m + 1][n + 1]; | ||
int r = -1, c = -1, len = 0; | ||
for(int i = 1; i <= m; i++){ | ||
for(int j = 1; j <= n; j++){ | ||
if(s1.charAt(i-1) == s2.charAt(j-1)){ | ||
dp[i][j] = 1 + dp[i-1][j-1]; | ||
if(len < dp[i][j]){ | ||
r = i; | ||
c = j; | ||
len = dp[i][j]; | ||
} | ||
} | ||
} | ||
} | ||
System.out.println("Length of longest common substring : " + len); | ||
char[] str = new char[len]; | ||
if(r == -1 && c == -1) | ||
System.out.println("Longest Common Substring does not exist"); | ||
else{ | ||
while(dp[r][c] > 0){ | ||
str[--len] = s1.charAt(r-1); | ||
r--; | ||
c--; | ||
} | ||
System.out.println("Longest Common Substring is : " + String.valueOf(str)); | ||
} | ||
} | ||
} | ||
``` | ||
|
||
- [x] **Time Complexity :** O(m\*n) where m and n are the length of the two strings | ||
|
||
- [x] **Space Complexity :** O(m\*n) where m and n are the length of the two strings |
27 changes: 27 additions & 0 deletions
27
codeforces-solutions/docs/dp/longestincreasingsubsequence.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
**Approach : Dynamic Programming - Tabulation :smile:** | ||
|
||
=== "Java" | ||
|
||
```java | ||
class Solution { | ||
public static void main(String[] args) { | ||
int[] nums = {10,9,2,5,3,7,101,18}; | ||
int n = nums.length; | ||
int[] dp = new int[n]; | ||
Arrays.fill(dp, 1); | ||
int len = 1; | ||
for(int i = 1; i < n; i++){ | ||
for(int j = 0; j < i; j++){ | ||
if(nums[j] < nums[i] && dp[i] <= dp[j]) | ||
dp[i] = 1 + dp[j]; | ||
} | ||
len = Math.max(len, dp[i]); | ||
} | ||
System.out.println("Length of LIS = " + len); | ||
} | ||
} | ||
``` | ||
|
||
- [x] **Time Complexity :** O(n^2^) where n is the length of the array | ||
|
||
- [x] **Space Complexity :** O(n) where n is the length of the array |
78 changes: 78 additions & 0 deletions
78
codeforces-solutions/docs/dp/longestpalindromicsubsequence.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
- Basically, the idea is to find length of LCS of a string and it's reverse | ||
|
||
**Approach 1 : Recursion :sweat_smile:** | ||
|
||
=== "Java" | ||
|
||
```java | ||
class Solution { | ||
public static void main(String[] args) { | ||
String s1 = ""; | ||
String s2 = new StringBuilder(s1).reverse().toString(); | ||
|
||
int n = s1.length(); | ||
int lps = solve(n-1, n-1, s1, s2); | ||
System.out.println("Length of LPS = " + lps); | ||
} | ||
|
||
public static int solve(int i, int j, String s1, String s2){ | ||
// base case | ||
if(i < 0 || j < 0) | ||
return 0; | ||
// if the current characters of both strings match, | ||
// then lcs length = 1 + lcs length for remaining part of these strings | ||
if(s1.charAt(i) == s2.charAt(j)) | ||
return 1 + solve(i-1, j-1, s1, s2); | ||
return Math.max(solve(i-1, j, s1, s2), solve(i, j-1, s1, s2)); | ||
} | ||
} | ||
``` | ||
|
||
- [x] **Time Complexity :** O(2^n\*n^) where n is the length of the string | ||
|
||
- [x] **Space Complexity :** O(n) where n is the length of the string | ||
|
||
<hr> | ||
|
||
**Approach 2 : Memoization :smile:** | ||
|
||
=== "Java" | ||
|
||
```java | ||
``` | ||
|
||
- [x] **Time Complexity :** O(n^2^) where n is the length of the string | ||
|
||
- [x] **Space Complexity :** O(n^2^) where n is the length of the string | ||
|
||
<hr> | ||
|
||
**Approach 3 : Tabulation :smile:** | ||
|
||
=== "Java" | ||
|
||
```java | ||
class Solution { | ||
public static void main(String[] args) { | ||
String s1 = ""; | ||
String s2 = new StringBuilder(s1).reverse().toString(); | ||
|
||
int n = s1.length(); | ||
int[][] dp = new int[n + 1][n + 1]; | ||
|
||
for(int i = 1; i <= n; i++){ | ||
for(int j = 1; j <= n; j++){ | ||
if(s1.charAt(i-1) == s2.charAt(j-1)) | ||
dp[i][j] = 1 + dp[i-1][j-1]; | ||
else | ||
dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]); | ||
} | ||
} | ||
System.out.println("Length of LPS = " + dp[n][n]); | ||
} | ||
} | ||
``` | ||
|
||
- [x] **Time Complexity :** O(n^2^) where n is the length of the string | ||
|
||
- [x] **Space Complexity :** O(n^2^) where n is the length of the string |
Oops, something went wrong.