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].
C语言
bool checkNonDes(int* nums, int numsSize, int* i) {
for (int j = 0; j+1 < numsSize; j++) {
if (nums[j] > nums[j+1]) {
*i = j;
return false;
}
}
return true;
}
bool checkPossibility(int* nums, int numsSize){
int i;
if (numsSize < 3)
return true;
if (checkNonDes(nums,numsSize,&i) == false){
if (i < numsSize-2) {
if (nums[i] < nums[i+2]) {
nums[i+1] = nums[i];
if (checkNonDes(nums,numsSize,&i) == false){
return false;
}
} else {
nums[i] = nums[i+1];
if (checkNonDes(nums,numsSize,&i) == false){
return false;
}
}
}
}
return true;
}
Success
Details
Runtime: 28 ms, faster than 12.56% of C online submissions for Non-decreasing Array.
Memory Usage: 8.3 MB, less than 100.00% of C online submissions for Non-decreasing Array.
python3
class Solution:
def checkPossibility(self, nums: List[int]) -> bool:
count_dec = 0
for i in range(len(nums) - 1):
if nums[i] > nums[i + 1]:
count_dec += 1
if i == 0:
nums[i] = nums[i + 1]
elif nums[i - 1] <= nums[i + 1]:
nums[i] = nums[i - 1]
else:
nums[i + 1] = nums[i]
if count_dec > 1:
return False
return True
Success
Details
Runtime: 48 ms, faster than 99.86% of Python3 online submissions for Non-decreasing Array.
Memory Usage: 14.2 MB, less than 5.95% of Python3 online submissions for Non-decreasing Array.
Longest Continuous Increasing Subsequence
Given an unsorted array of integers, find the length of longest continuous increasing subsequence (subarray).
Example 1:
Input: [1,3,5,4,7]
Output: 3
Explanation: The longest continuous increasing subsequence is [1,3,5], its length is 3.
Even though [1,3,5,7] is also an increasing subsequence, it’s not a continuous one where 5 and 7 are separated by 4.
Example 2:
Input: [2,2,2,2,2]
Output: 1
Explanation: The longest continuous increasing subsequence is [2], its length is 1.
Note: Length of the array will not exceed 10,000.
C语言
int findLengthOfLCIS(int* nums, int numsSize){
int i=0,count=0,res=0;
if(numsSize<1)return 0;
while(i<numsSize-1){if(nums[i]<nums[i+1]){count++;if(count>res)res=count;}else count=0;i++;}
return res+1;
}
Success
Details
Runtime: 8 ms, faster than 42.35% of C online submissions for Longest Continuous Increasing Subsequence.
Memory Usage: 7.5 MB, less than 100.00% of C online submissions for Longest Continuous Increasing Subsequence.
python3
class Solution:
def findLengthOfLCIS(self, nums: List[int]) -> int:
if not nums:
return 0
res = 1
c = 1
for i in range(1, len(nums)):
c = c + 1 if nums[i] > nums[i-1] else 1
res = max(res, c)
return res
Success
Details
Runtime: 48 ms, faster than 48.89% of Python3 online submissions for Longest Continuous Increasing Subsequence.
Memory Usage: 14.1 MB, less than 6.67% of Python3 online submissions for Longest Continuous Increasing Subsequence.
Degree of an Array
Given a non-empty array of non-negative integers nums, the degree of this array is defined as the maximum frequency of any one of its elements.
Your task is to find the smallest possible length of a (contiguous) subarray of nums, that has the same degree as nums.
Example 1:
Input: [1, 2, 2, 3, 1]
Output: 2
Explanation:
The input array has a degree of 2 because both elements 1 and 2 appear twice.
Of the subarrays that have the same degree:
[1, 2, 2, 3, 1], [1, 2, 2, 3], [2, 2, 3, 1], [1, 2, 2], [2, 2, 3], [2, 2]
The shortest length is 2. So return 2.
Example 2:
Input: [1,2,2,3,1,4,2]
Output: 6
Note:
nums.length will be between 1 and 50,000.
nums[i] will be an integer between 0 and 49,999.
C语言
int findShortestSubArray(int* nums, int numsSize){
int count[50000]={0};
int first[50000]={0};
int res=0,degree=0;
for(int i=0;i<numsSize;i++){
if(first[nums[i]]==0)
first[nums[i]]=i+1;
count[nums[i]]++;
if(count[nums[i]]>degree){
degree=count[nums[i]];
res=i-first[nums[i]]+2;
}
else if(count[nums[i]]==degree){
if((i-first[nums[i]]+2)<res)
res=i-first[nums[i]]+2;
}
}
return res;
}
Success
Details
Runtime: 28 ms, faster than 40.54% of C online submissions for Degree of an Array.
Memory Usage: 9.3 MB, less than 100.00% of C online submissions for Degree of an Array.
python3
class Solution:
def findShortestSubArray(self, nums: List[int]) -> int:
dic = {}
for i in nums:
if i in dic:
dic[i] += 1
else:
dic[i] = 1
degree = max(dic.values())
if degree == 1:
return 1
max_vec = []
for i in dic:
if dic[i] == degree:
max_vec.append(i)
shortest_len = float('inf')
for i in range(len(max_vec)):
temp = len(nums) - nums.index(max_vec[i]) - nums[::-1].index(max_vec[i])
shortest_len = min(shortest_len,temp)
return shortest_len
Success
Details
Runtime: 60 ms, faster than 96.88% of Python3 online submissions for Degree of an Array.
Memory Usage: 14.5 MB, less than 14.06% of Python3 online submissions for Degree of an Array.