public int longestValidParentheses(String s) {
		if (s == null || s.length() <= 1) {
			return 0;
		}

		int longest = 0;

		int[] longests = new int[s.length()];
		longests[s.length() - 1] = 0;

		for (int i = s.length() - 2; i >= 0; i--) {
			longests[i] = 0;
			if (s.charAt(i) == ')') {
				longests[i] = 0;
			} else {
				int k = i + longests[i + 1] + 1;
				if (k < s.length() && s.charAt(k) == ')') {
					longests[i] = longests[i + 1] + 2;
					if (k + 1 < s.length()) {
						longests[i] += longests[k + 1];
					}
				}
			}

			if (longests[i] > longest) {
				longest = longests[i];
			}
		}

		return longest;
	}