g0001_0100.s0032_longest_valid_parentheses.solution.js Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-all Show documentation
Show all versions of leetcode-in-all Show documentation
104 LeetCode algorithm problem solutions
The newest version!
// #Hard #Top_100_Liked_Questions #String #Dynamic_Programming #Stack #Big_O_Time_O(n)_Space_O(1)
// #2024_12_05_Time_1_ms_(96.21%)_Space_52.5_MB_(15.11%)
/**
* @param {string} s
* @return {number}
*/
var longestValidParentheses = function (s) {
const n = s.length
const f = new Array(n + 1).fill(0)
for (let i = 2; i <= n; ++i) {
if (s[i - 1] === ')') {
if (s[i - 2] === '(') {
f[i] = f[i - 2] + 2
} else {
const j = i - f[i - 1] - 1
if (j && s[j - 1] === '(') {
f[i] = f[i - 1] + 2 + f[j - 1]
}
}
}
}
return Math.max(...f)
};
export { longestValidParentheses }
© 2015 - 2025 Weber Informatics LLC | Privacy Policy