Java C++ 算法題解leetcode1608特殊數(shù)組特征值
更新時間:2022年09月14日 09:07:25 作者:AnjaVon
這篇文章主要為大家介紹了Java C++ 算法題解拓展leetcode1608特殊數(shù)組特征值實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
題目要求


思路一:枚舉 + 二分
- 逐一枚舉值域內(nèi)的所有值,然后二分判斷是否合法。
Java
class Solution {
public int specialArray(int[] nums) {
Arrays.sort(nums);
int n = nums.length;
for (int x = 0; x <= nums[n - 1]; x++) { // 枚舉
int l = 0, r = n -1 ;
while (l < r) { // 二分
int m = l + r >> 1;
if (nums[m] >= x)
r = m;
else
l = m + 1;
}
if (nums[r] >= x && x == n - r)
return x;
}
return -1;
}
}
- 時間復(fù)雜度:O(n log? n),排序復(fù)雜度為O(n log? n),枚舉次數(shù)為值域范圍C=1000,所以找答案的復(fù)雜度為O(C log n)
- 空間復(fù)雜度:O(log? n))
C++
class Solution {
public:
int specialArray(vector<int>& nums) {
sort(nums.begin(), nums.end());
int n = nums.size();
for (int x = 0; x <= nums[n - 1]; x++) { // 枚舉
int l = 0, r = n -1 ;
while (l < r) { // 二分
int m = (l + r) >> 1;
if (nums[m] >= x)
r = m;
else
l = m + 1;
}
if (nums[r] >= x && x == n - r)
return x;
}
return -1;
}
};
- 時間復(fù)雜度:O(n log ?n),排序復(fù)雜度為O(n log? n),枚舉次數(shù)為值域范圍C=1000,所以找答案的復(fù)雜度為O(C log? n)
- 空間復(fù)雜度:O(log? n)
思路二:二分枚舉
二分枚舉+二分判定是否合法;
為了方便把判斷合法單獨寫成函數(shù)getResgetResgetRes。
Java
class Solution {
int[] nums;
public int specialArray(int[] num) {
this.nums = num;
Arrays.sort(nums);
int l = 0, r = nums[nums.length - 1];
while (l < r) {
int m = l + r >> 1;
if (getRes(m) <= m)
r = m;
else
l = m + 1;
}
return getRes(r) == r ? r : -1;
}
int getRes(int x) {
int n = nums.length, l = 0, r = n - 1;
while (l < r) {
int m = l + r >> 1;
if (nums[m] >= x)
r = m;
else
l = m + 1;
}
return nums[r] >= x ? n - r : 0;
}
}
- 時間復(fù)雜度:O(n log? n),排序復(fù)雜度為O(n log ?n),二分找答案所以復(fù)雜度為O(log ?C log ?n)
- 空間復(fù)雜度:O(log ?n)
C++
- 注意全局變量和輸入變量需要有差別……
class Solution {
public:
vector<int> nums;
int specialArray(vector<int>& num) {
this->nums = num;
sort(nums.begin(), nums.end());
int l = 0, r = nums[nums.size() - 1];
while (l < r) {
int m = (l + r) >> 1;
if (getRes(m) <= m)
r = m;
else
l = m + 1;
}
return getRes(r) == r ? r : -1;
}
int getRes(int x) {
int n = nums.size(), l = 0, r = n - 1;
while (l < r) {
int m = (l + r) >> 1;
if (nums[m] >= x)
r = m;
else
l = m + 1;
}
return nums[r] >= x ? n - r : 0;
}
};
- 時間復(fù)雜度:O(n log? n),排序復(fù)雜度為O(n log ?n),二分找答案所以復(fù)雜度為O(log? C log? n)
- 空間復(fù)雜度:O(log? n)
思路三:倒序枚舉
- 因為值域比較小,所以可以直接從值域最后開始倒著枚舉;
- 預(yù)處理出每個值出現(xiàn)的次數(shù),然后記錄當前合法合法數(shù)值的數(shù)量與當前數(shù)值進行比較。
Java
class Solution {
public int specialArray(int[] nums) {
int[] cnt = new int[1001];
for (int x : nums)
cnt[x]++;
for (int i = 1000, tot = 0; i >= 0; i--) {
tot += cnt[i]; // 數(shù)量
if (i == tot)
return i;
}
return -1;
}
}
- 時間復(fù)雜度:O(n+C)
- 空間復(fù)雜度:O(C)
C++
class Solution {
public:
int specialArray(vector<int>& nums) {
int cnt[1001];
memset(cnt, 0, sizeof(cnt));
for (int x : nums)
cnt[x]++;
for (int i = 1000, tot = 0; i >= 0; i--) {
tot += cnt[i];
if (i == tot)
return i;
}
return -1;
}
};
- 時間復(fù)雜度:O(n+C)
- 空間復(fù)雜度:O(C)
以上就是Java C++ 算法題解leetcode1608特殊數(shù)組特征值的詳細內(nèi)容,更多關(guān)于Java C++ 算法特殊數(shù)組特征值的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C++ Boost Serialization庫超詳細獎金額
Boost是為C++語言標準庫提供擴展的一些C++程序庫的總稱。Boost庫是一個可移植、提供源代碼的C++庫,作為標準庫的后備,是C++標準化進程的開發(fā)引擎之一,是為C++語言標準庫提供擴展的一些C++程序庫的總稱2022-12-12

