> For the complete documentation index, see [llms.txt](https://syjohnson11.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://syjohnson11.gitbook.io/leetcode/7_dp/yi-wei-dp.md).

# 一维DP

## 划分型：f\[] means XXXX ending with i

word break

longest valid parentheses

decode ways

## Longest Valid Parentheses

* 不能算Hard, 但是转移方程需要考虑坐标edge case, bug free不容易
* 划分型类似Longest Palindromic Substring，word break思路

```
public int longestValidParentheses(String s) {
        if (s == null || s.length() == 0) return 0;
        int n = s.length();
        int[] f = new int[n];
        int max = 0;
        for (int i = 1; i < n; i++) {
            if (s.charAt(i) == ')') {
                if (s.charAt(i - 1) == '(') { //“（）”
                    f[i] = 2 + (i >= 2 ? f[i - 2] : 0);
                } else if (i - f[i-1] - 1 >= 0 && s.charAt(i - f[i-1] - 1) == '(') {
                    f[i] = 2 + f[i-1] + (i - f[i-1] - 2 >= 0 ? f[i - f[i-1] - 2] : 0);
                }
            }
            max = Math.max(max, f[i]);
        }
        return max;
    }
```

## Decode Ways

* 注意corner case, any valid substring must starts with non-zero
* how to handle "0000" ?
* state:f\[i] means num of decode ways for s.substring(i)&#x20;
* function:&#x20;
  * f\[i] = isValid(s.substring(i, i + 1)) && f\[i + 1] + isValid(s.substring(i, i + 2)) && f\[i + 2]&#x20;
* oneDigit != 0&#x20;
* 10 <= twoDigits <= 26&#x20;
* res: f\[0]&#x20;
* initialize:&#x20;
  * f\[n] = 0&#x20;
  * f\[n - 1] = isValid(s.substring(n - 1)) ? 1 : 0&#x20;
  * f\[n - 2] = isValid(s.substring(n - 2)) ? 1 + f\[n - 1] : 0
* **面试还是写从dfs -> memo的思路更make sense一点**
* **除非面试官要求optimal space complexity-> DP with rolling array**

```
class Solution {
    /*    
    brute force: DFS
    decode("226") = decode("26") + isValid("22") && decode("6")
    
    state:f[i] means num of decode ways for s.substring(i)
    function: 
    f[i] = isValid(s.substring(i, i + 1)) && f[i + 1] + isValid(s.substring(i, i + 2)) && f[i + 2]
    oneDigit != 0
    10 <= twoDigits <= 26
    res: f[0]
    initialize: 
    f[n] = 0
    f[n - 1] = isValid(s.substring(n - 1)) ? 1 : 0
    f[n - 2] = isValid(s.substring(n - 2)) ? 1 + f[n - 1] : 0
    
    O(N) Time and O(N) Space
    can use rolling array to optimize space complexity to O(1)
    
    */
    public int numDecodings(String s) {
        if (s == null || s.length() == 0) {
            return 0;
        }
        int n = s.length();
        int[] f = new int[n + 1];
        //initialize
        f[n] = 1;
        f[n - 1] =  s.charAt(n - 1) != '0' ? 1 : 0;
        for (int i = n - 2; i >= 0; i--) {
            char c = s.charAt(i);
            //one digit, need to make sure it's not '0'
            if (c != '0') {
                f[i] += f[i + 1];
            }
            int twoDigits = (s.charAt(i) - '0') * 10 + s.charAt(i + 1) - '0';
            if (twoDigits >= 10 && twoDigits <= 26) {
                f[i] += f[i + 2];
            }
        }
        return f[0];
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://syjohnson11.gitbook.io/leetcode/7_dp/yi-wei-dp.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
