Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added solution for 228. Summary Ranges #3748

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 40 additions & 11 deletions java/1220-count-vowels-permutation.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/* Bottom-Up Approach
-----------------------------------------
Time Complexity : O(n)
Space Complexity : O(n)
----------------------------------------*/

class Solution {
int MOD = (int) 1e9+7;

Expand Down Expand Up @@ -39,19 +45,42 @@ class Solution {
int MOD = (int) 1e9+7;

public int countVowelPermutation(int n) {
long[] counts = getBaseCounts();
if(n == 1) {
return getSum(counts);
}
long ans = 0;
ans = (ans + dfs('a', n, 1)) % MOD;
ans = (ans + dfs('e', n, 1)) % MOD;
ans = (ans + dfs('i', n, 1)) % MOD;
ans = (ans + dfs('o', n, 1)) % MOD;
ans = (ans + dfs('u', n, 1)) % MOD;
return (int)ans;
}

private int dfs(char c, int n, int l){
if(l == n)
return 1;

Map<Integer, List<Integer>> mapNextCounting;
mapNextCounting = getNextCountMapping();

for(int i=1; i<n; i++) {
counts = getNextCounts(counts, mapNextCounting);
String key = c + "_" + l;
if (memo.containsKey(key)) return memo.get(key);

long res = 0;
if(c == 'a') {
res = dfs('e', n, l+1);
} else if(c == 'e') {
res = (res + dfs('a', n, l+1)) % MOD;
res = (res + dfs('i', n, l+1)) % MOD;
} else if(c == 'i') {
res = (res + dfs('a', n, l+1)) % MOD;
res = (res + dfs('e', n, l+1)) % MOD;
res = (res + dfs('o', n, l+1)) % MOD;
res = (res + dfs('u', n, l+1)) % MOD;
} else if(c == 'o') {
res = (res + dfs('i', n, l+1)) % MOD;
res = (res + dfs('u', n, l+1)) % MOD;
} else {
res = dfs('a', n, l+1);
}

return getSum(counts);

memo.put(key, (int)(res % MOD));
return (int)(res % MOD);
}
}
/* Bottom-Up Approach
Expand Down
77 changes: 1 addition & 76 deletions kotlin/1143-longest-common-subsequence.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,79 +20,4 @@ class Solution {
}
return dp[M][N]
}
}

/*
* Different solutions
*/

// Recursion + Memoization, Time Complexity of O(n * m) and space complexity of O(n * m)
class Solution {
fun longestCommonSubsequence(t1: String, t2: String): Int {
val n = t1.length
val m = t2.length
val dp = Array (n) { IntArray (m) { -1 } }

fun dfs(i: Int, j: Int): Int {
if (i == n || j == m) return 0
if (dp[i][j] != -1) return dp[i][j]

if (t1[i] == t2[j])
dp[i][j] = 1 + dfs(i + 1, j + 1)
else
dp[i][j] = maxOf(dfs(i + 1, j), dfs(i, j + 1))

return dp[i][j]
}

return dfs(0, 0)
}
}

// Top down DP, Time Complexity of O(n * m) and space complexity of O(n * m)
class Solution {
fun longestCommonSubsequence(t1: String, t2: String): Int {
val n = t1.length
val m = t2.length
val dp = Array (n + 1) { IntArray (m + 1) }

for (i in n - 1 downTo 0) {
for (j in m - 1 downTo 0) {
if (t1[i] == t2[j])
dp[i][j] = 1 + dp[i + 1][j + 1]
else
dp[i][j] = maxOf(dp[i + 1][j], dp[i][j + 1])
}
}

return dp[0][0]
}
}

// Optimized DP (Works both for both Top-down and Bottom-up, but here we use bottom-up approach)
// Time Complexity of O(n * m) and space complexity of O(maxOf(n, m))
class Solution {
fun longestCommonSubsequence(t1: String, t2: String): Int {
val m = t1.length
val n = t2.length
if (m < n) return longestCommonSubsequence(t2, t1)

var dp = IntArray (n + 1)

for (i in m downTo 0) {
var newDp = IntArray (n + 1)
for (j in n downTo 0) {
if (i == m || j == n) {
newDp[j] = 0
} else if (t1[i] == t2[j]) {
newDp[j] = 1 + dp[j + 1]
} else {
newDp[j] = maxOf(dp[j], newDp[j + 1])
}
}
dp = newDp
}

return dp[0]
}
}
}
23 changes: 23 additions & 0 deletions python/0228-Summary-Ranges.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution:
def summaryRanges(self, nums: List[int]) -> List[str]:
result = []
n = len(nums)
if n == 0:
return result

# Pointer to the start of the current range
start = 0

for i in range(1, n + 1): # Iterate until the end of the array
# If we're at the end of the array or the range breaks
if i == n or nums[i] != nums[i - 1] + 1:
# Single number range
if start == i - 1:
result.append(str(nums[start]))
# Continuous range
else:
result.append(f"{nums[start]}->{nums[i - 1]}")
# Update start for the next range
start = i

return result