Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 9d627ac

Browse files
committedMar 21, 2025
Add solution #665
1 parent 69a123b commit 9d627ac

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,7 @@
502502
661|[Image Smoother](./0661-image-smoother.js)|Easy|
503503
662|[Maximum Width of Binary Tree](./0662-maximum-width-of-binary-tree.js)|Medium|
504504
664|[Strange Printer](./0664-strange-printer.js)|Hard|
505+
665|[Non-decreasing Array](./0665-non-decreasing-array.js)|Medium|
505506
667|[Beautiful Arrangement II](./0667-beautiful-arrangement-ii.js)|Medium|
506507
668|[Kth Smallest Number in Multiplication Table](./0668-kth-smallest-number-in-multiplication-table.js)|Hard|
507508
669|[Trim a Binary Search Tree](./0669-trim-a-binary-search-tree.js)|Medium|
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* 665. Non-decreasing Array
3+
* https://leetcode.com/problems/non-decreasing-array/
4+
* Difficulty: Medium
5+
*
6+
* Given an array nums with n integers, your task is to check if it could become non-decreasing
7+
* by modifying at most one element.
8+
*
9+
* We define an array is non-decreasing if nums[i] <= nums[i + 1] holds for every i (0-based)
10+
* such that (0 <= i <= n - 2).
11+
*/
12+
13+
/**
14+
* @param {number[]} nums
15+
* @return {boolean}
16+
*/
17+
var checkPossibility = function(nums) {
18+
let count = 0;
19+
20+
for (let i = 0; i < nums.length - 1; i++) {
21+
if (nums[i] > nums[i + 1]) {
22+
count++;
23+
if (count > 1) return false;
24+
if (i > 0 && nums[i - 1] > nums[i + 1]) {
25+
nums[i + 1] = nums[i];
26+
} else {
27+
nums[i] = nums[i + 1];
28+
}
29+
}
30+
}
31+
32+
return true;
33+
};

0 commit comments

Comments
 (0)
Please sign in to comment.