#Leetcode:665,605
665:Non-decreasing Array
Given an array with n integers, your task is to check if it could become non-decreasing by modifying at most 1 element.
We define an array is non-decreasing if array[i] <= array[i + 1] holds for every i (1 <= i < n).
Example 1:
Input: [4,2,3]
Output: True
Explanation: You could modify the first 4 to 1 to get a non-decreasing array.
Example 2:
Input: [4,2,1]
Output: False
Explanation: You can’t get a non-decreasing array by modify at most one element.
Note: The n belongs to [1, 10,000].
class Solution { public: bool checkPossibility(vector<int>& nums) { int index = -1; int countnum = 0; bool flag1; bool flag2; for(int i = 0; i<nums.size()-1; i++) { if(nums[i+1]<nums[i]) { if(countnum == 0) { index = i + 1; } countnum ++; } } if(index == -1) return true; else if(countnum>1) return false; else { if(index+1 > nums.size()-1) { flag1 = true; } else if(nums[index+1]>=nums[index-1]) { flag1 = true; } else { flag1 = false; } if(index-2<0) { flag2 = true; } else if(nums[index]>nums[index-2]) { flag2 = true; } else { flag2 = false; } } return flag1||flag2; } };
605: Can Place Flowers
Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, flowers cannot be planted in adjacent plots - they would compete for water and both would die.
Given a flowerbed (represented as an array containing 0 and 1, where 0 means empty and 1 means not empty), and a number n, return if n new flowers can be planted in it without violating the no-adjacent-flowers rule.
Example 1:
Input: flowerbed = [1,0,0,0,1], n = 1
Output: True
Example 2:
Input: flowerbed = [1,0,0,0,1], n = 2
Output: False
Note:
The input array won’t violate no-adjacent-flowers rule.
The input array size is in the range of [1, 20000].
n is a non-negative integer which won’t exceed the input array size.
首先需要找到關鍵點:v[i]==0的點,再根據i點的左右值把陣列分為以下四種討論:
1.[0] i=0
2.[0,0,…] i=0
3.[…0,0] i=size()-1
4.[…0,0,0…] 普通情況
可得出如下程式碼:
# 605: Can Place Flowers
class Solution {
public:
bool canPlaceFlowers(vector<int>& flowerbed, int n) {
int countnum = 0;
if(n == 0)
return true;
for(int i = 0; i<flowerbed.size(); i++)
{
if(flowerbed[i] == 0)
{
if(i-1 < 0 && i+1 >flowerbed.size()-1) //[0]
{
flowerbed[i] = 1;
countnum++;
}
if(i==flowerbed.size()-1 && i-1 >=0 && flowerbed[i-1] == 0) //i-1存在且v(i-1)==0 [...0,0]
{
flowerbed[i] = 1;
countnum++;
}
if(i==0 && i+1<=flowerbed.size()-1 && flowerbed[i+1] == 0) // [0,0....]
{
flowerbed[i] = 1;
countnum++;
}
if(i-1 >= 0 && i+1 <=flowerbed.size()-1 && flowerbed[i-1] == 0 && flowerbed[i+1] == 0) // [...0,0,0...]
{
flowerbed[i] = 1;
countnum++;
}
}
}
if(countnum>=n)
{
return true;
}
else
{
return false;
}
}
};
實際上,上述1,2,3,三種情況可以通過一些技巧轉換未普通情況:當i索引(v[i]==0)位於陣列的最左邊,最右邊或者陣列中只有一個元素時,通過人工新增i索引處左值(v[i-1])或者右值(v[i+1])來將其轉換為普通情況:
if(flowerbed[i] == 0)
int next = (i == flowerbed.length - 1) ? 0 : flowerbed[i + 1];
int prev = (i == 0) ? 0 : flowerbed[i - 1];
同時在for迴圈中加上count<n節省時間:
public class Solution {
public boolean canPlaceFlowers(int[] flowerbed, int n) {
int count = 0;
for(int i = 0; i < flowerbed.length && count < n; i++) {
if(flowerbed[i] == 0) {
//get next and prev flower bed slot values. If i lies at the ends the next and prev are considered as 0.
int next = (i == flowerbed.length - 1) ? 0 : flowerbed[i + 1];
int prev = (i == 0) ? 0 : flowerbed[i - 1];
if(next == 0 && prev == 0) {
flowerbed[i] = 1;
count++;
}
}
}
return count == n;
}
}
tip:
①:找關鍵點,根據關鍵點來對可能出現的情況進行討論,比如665中的i與i-1的情況。
②:關鍵點轉換,比如605中,對於陣列頭與陣列尾的情況,通過對pre與next賦值來轉換為一般情況