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

Palindrome Partitioning Memoization #3

Open
ChetasShree opened this issue Feb 6, 2022 · 0 comments
Open

Palindrome Partitioning Memoization #3

ChetasShree opened this issue Feb 6, 2022 · 0 comments

Comments

@ChetasShree
Copy link

ChetasShree commented Feb 6, 2022

Hey, this repo you have made has helped me a lot, because I get the logic but when it comes to coding it, it sometimes feels difficult, so thank you for making this amazing repo 🙌🙌.

In this -> https://github.com/shubhamchemate003/Dynamic-Programming-Questions-by-Aditya-Verma/blob/main/pallindrome_partitioning_memoization.cpp

and in this too -> https://github.com/shubhamchemate003/Dynamic-Programming-Questions-by-Aditya-Verma/blob/main/pallindrome_partitioning_memoized_optimization.cpp

just pass the string with reference(&) in both the function ( isPallindrome and Solve) and it will not give TLE .
Just wanted to help, because I think there would be a lot of people like me who are getting help through this repo.

code :

bool isPallindrome(string &X, int i, int j) {
	while (i <= j) {
		if (X[i] != X[j])
			return false;
		i++, j--;
	}

	return true;
}

int Solve(string &X, int i, int j) {
	if (i >= j || isPallindrome(X, i, j)) {
		t[i][j] = 0;
		return 0;
	}

	if (t[i][j] != -1)
		return t[i][j];

	int ans = INT_MAX;
	for (int k = i; k < j; k++) {
		int temp_ans = Solve(X, i, k) + Solve(X, k + 1, j) + 1;
		ans = min(ans, temp_ans);
	}

	return t[i][j] = ans;
} 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant