Longest Palindromic Subsequence - Leetcode 516 - Python

Longest Palindromic Subsequence - Leetcode 516 - Python

NeetCodeIO

2 года назад

38,583 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@juda550
@juda550 - 14.04.2023 04:45

I love you neetcode

Ответить
@jswlprtk
@jswlprtk - 14.04.2023 05:17

Your consistency is inspirational ❤

Ответить
@jamesmandla1192
@jamesmandla1192 - 14.04.2023 05:37

You don't have to run the top down dp function n times on all characters and expand outwards which is O(n^3). You can just run the dp func once with the two pointers at both ends of the string and expand in-wards instead which is O(n^2). So just the same process but going in the opposite direction and you only have to run it once so you reduce it to O(n^2) which is accepted.

Ответить
@viktoreidrien7110
@viktoreidrien7110 - 14.04.2023 07:12

Man your work is saving lives, thank you

Ответить
@jaymehta8235
@jaymehta8235 - 14.04.2023 12:22

can you please teach in c++ too

Ответить
@eobardthawne6903
@eobardthawne6903 - 14.04.2023 12:42

It's good that you're making videos on daily challenges 💪

Ответить
@hoyinli7462
@hoyinli7462 - 14.04.2023 15:14

i coded in java and got pass using top down dp

Ответить
@krateskim4169
@krateskim4169 - 14.04.2023 21:06

Thank you so much

Ответить
@yang5843
@yang5843 - 14.04.2023 22:10

This video has been super helpful in understanding the problem.

The recursive solution doesn't work in Java either. I have attached the dp Java solution below.

class Solution {
public int longestPalindromeSubseq(String s) {
int n = s.length();
int[][] dp = new int[n+1][n+1];
int res = 0;

for (int i=0;i<n;i++)
for (int j=n-1;j>i-1;j--)
{
if ( s.charAt(i) == s.charAt(j) ) {
if ( i == j )
dp[i][j] = 1;
else
dp[i][j] = 2;

if ( i - 1 >=0 )
dp[i][j] += dp[i-1][j+1];
}
else {
dp[i][j] = dp[i][j+1];
if ( i - 1 >= 0 )
dp[i][j] = Math.max(dp[i][j], dp[i-1][j]);
}
res = Math.max(res,dp[i][j]);
}
return res;
}
}

Ответить
@wentaowang8622
@wentaowang8622 - 15.04.2023 00:22

lets write some neetcode today!

Ответить
@akalizaakaliza7049
@akalizaakaliza7049 - 15.04.2023 00:49

when using 2 dimension array instead of a hashmap , all may tests case are passing with the same code (recursion + memo)- Integer [][] map = new Integer [1001][1001] , the constraints are too small

Ответить
@slayers2966
@slayers2966 - 27.10.2023 08:37

wow, 18 mins of explaining everything except for the required solution.

Ответить
@moisascholar
@moisascholar - 02.11.2023 05:09

Excellent explanation. Very helpful!

Ответить
@homyakMilashka
@homyakMilashka - 23.11.2023 20:06

Not passing in JavaScript either =(

Ответить
@rafael84
@rafael84 - 21.01.2024 13:03

DFS version in JS:

var longestPalindromeSubseq = function (s) {
const cache = new Map();

function dfs(L, R) {
if (L > R) return 0;
if (L === R) return 1;

const key = `${L},${R}`;
if (cache.has(key))
return cache.get(key);

let length = 0;
if (s[L] === s[R]) {
length = 2 + dfs(L + 1, R - 1);
} else {
length = Math.max(dfs(L + 1, R), dfs(L, R - 1));
}

cache.set(key, length);
return length;
}

return dfs(0, s.length - 1);
};

Ответить
@sancho7198
@sancho7198 - 08.02.2024 22:02

possible solution


from functools import lru_cache

class Solution:
def longestPalindromeSubseq(self, s: str) -> int:
@lru_cache(None)
def dp(l, r):
if l==r:
return 1
if l>r:
return 0
if s[l]==s[r]:
return 2 + dp(l+1,r-1)
else:
return max(dp(l+1,r), dp(l,r-1))


return dp(0,len(s)-1)

Ответить
@tranminhquang4541
@tranminhquang4541 - 14.06.2024 13:11

I did DP for this !

Ответить
@mohamed44324
@mohamed44324 - 15.09.2024 02:39

Yeah I think you made a mistake here. You're recursive algorithm in n^3 not n^2. Starting with 2 pointers one at the beginning and one at the end will give you n^2. Same idea but go inwards instead of outwards.

Ответить
@OwenWu-f9t
@OwenWu-f9t - 19.09.2024 07:00

these questions are nothing but memorization and anyone who says otherwise is a complete liar.

Ответить
@berfubuyukoz353
@berfubuyukoz353 - 04.12.2024 11:44

This was an amazing walk through. Just watching this is pleasure. Thank you so much for your hard work with these videos 🙏

Ответить
@ukaszczyz2962
@ukaszczyz2962 - 13.01.2025 01:53

What you said at the end of the video made me feel better. I arrived at pretty much exactly the same solution as you and got memory exceeded result even though all cases passed.

Ответить
@mateovlz
@mateovlz - 22.01.2025 18:07

My god thanks @NeetCodeIO your explanations are awesome, even doing DSA courses they can't cover what you have done with your content, thanks!

Ответить
@prithvialva4099
@prithvialva4099 - 29.01.2025 17:10

Tried brute force method using c++ : time limit exceeded. My experience has been that the language doesn't really help with the time limit exceed scenarios, if it exceeds in python, there's mostly no way it'll pass in c++ (for leetcode stuff).

Serious competitive programming scenarios must be different though, I've heard that's very language sensitive.

Ответить
@MikeShauneu
@MikeShauneu - 08.05.2025 20:02

class Solution:
def longestPalindromeSubseq(self, s: str) -> int:
n = len(s)
memo = [[0] * n for _ in range(n)]

def lps(left: int, right: int) -> int:
if left > right:
return 0
if left == right:
return 1
if memo[left][right] != 0:
return memo[left][right]

if s[left] == s[right]:
memo[left][right] = 2 + lps(left + 1, right - 1)
else:
memo[left][right] = max(lps(left + 1, right), lps(left, right - 1))

return memo[left][right]

return lps(0, n - 1)

Ответить
@Adam-tz6gk
@Adam-tz6gk - 13.06.2025 14:02

Fantastic explanation

Ответить