GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode-1/java/665_Non-decreasing_Array.java at master · FullstackSensei/leetcode-1

Expand file tree

/

665_Non-decreasing_Array.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
29 lines (28 loc) · 1002 Bytes
/

665_Non-decreasing_Array.java

Copy path

File metadata and controls

29 lines (28 loc) · 1002 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution {
/* public boolean checkPossibility(int[] nums) {
int pos = -1;
for (int i = 0; i < nums.length - 1; i++) {
if (nums[i] > nums[i + 1]) {
// More than two broken points
if (pos != -1) return false;
pos = i;
}
}
if (pos == -1 || pos == 0 || pos == nums.length - 2) return true;
// Remove pos or pos + 1
return (nums[pos - 1] <= nums[pos + 1] || nums[pos] <= nums[pos + 2]);
} */
public boolean checkPossibility(int[] nums) {
int brokenPoint = 0;
for (int i = 0; i < nums.length - 1; i++) {
if (nums[i] > nums[i + 1]) {
brokenPoint++;
if (brokenPoint >= 2) return false;
// Remove i or remove i + 1
if (i - 1 < 0 || nums[i - 1] <= nums[i + 1]) nums[i] = nums[i + 1];
else nums[i + 1] = nums[i];
}
}
return true;
}
}
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.