File tree 2 files changed +34
-0
lines changed
2 files changed +34
-0
lines changed Original file line number Diff line number Diff line change 502
502
661|[ Image Smoother] ( ./0661-image-smoother.js ) |Easy|
503
503
662|[ Maximum Width of Binary Tree] ( ./0662-maximum-width-of-binary-tree.js ) |Medium|
504
504
664|[ Strange Printer] ( ./0664-strange-printer.js ) |Hard|
505
+ 665|[ Non-decreasing Array] ( ./0665-non-decreasing-array.js ) |Medium|
505
506
667|[ Beautiful Arrangement II] ( ./0667-beautiful-arrangement-ii.js ) |Medium|
506
507
668|[ Kth Smallest Number in Multiplication Table] ( ./0668-kth-smallest-number-in-multiplication-table.js ) |Hard|
507
508
669|[ Trim a Binary Search Tree] ( ./0669-trim-a-binary-search-tree.js ) |Medium|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments