> For the complete documentation index, see [llms.txt](https://851958789.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://851958789.gitbook.io/notes/0665_non-decreasing_array/slt.md).

# 思路1 贪心

* 时间复杂度 $O(n)$
* 空间复杂度 $O(1)$

对于每个 i，需要让 nums\[i] <= nums\[i + 1]，如果有 nums\[i] > nums\[i + 1]，则要 modify

但是本题唯一的易错点就在这，

* 如果将 nums\[i] 缩小，可能会导致其无法融入前面已经遍历过的非递减子数列；

  &#x20; 3 4 2 3

  &#x20; 3 2 2 3
* 如果将 nums\[i + 1] 放大，可能会导致其后续的继续出现递减；

  &#x20; 3 4 2 3

  &#x20; 3 4 4 3

所以要采取贪心的策略，在遍历时，每次需要看连续的三个元素，也就是瞻前顾后，遵循以下两个原则：

* 需要尽可能不放大 nums\[i + 1]，这样会让后续非递减更困难；
* 如果缩小 nums\[i]，但不破坏前面的子序列的非递减性；

在遍历时，每次需要看连续的三个元素

遍历数组，如果遇到递减：

* 还能修改：
  * 修改方案1：将nums\[i]缩小至nums\[i + 1]；
  * 修改方案2：将nums\[i + 1]放大至nums\[i]；
* 不能修改了：直接返回false；

缩小时，必保证 nums\[i-1] <= nums\[i+1]。这样 nums\[i] 变小后，也比前一个大

否则，则将后面的放大


---

# 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://851958789.gitbook.io/notes/0665_non-decreasing_array/slt.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.
