Java&C++ problem solution and expansion - leetcode1608. Eigenvalues of special arrays [What's new knowledge]

Make a record of one question per day, refer to the official and Sanye's solution

Topic requirements

Idea 1: enumeration + dichotomy

  • Enumerate all the values ​​in the value range one by one, and then judge whether it is legal or not.

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;
    }
}
复制代码
  • Time complexity: O(n\log n) O ( n log n ), the sorting complexity is O(n\log n) O ( n log n ), the number of enumerations is the range of values ​​C=1000 1 0 0 0 , so the complexity of finding the answer is O(C\log n) O ( C log n )
  • Space complexity: O(\log n) 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;
    }
};
复制代码
  • Time complexity: O(n\log n) O ( n log n ), the sorting complexity is O(n\log n) O ( n log n ), the number of enumerations is the range of values ​​C=1000 1 0 0 0 , so the complexity of finding the answer is O(C\log n) O ( C log n )
  • Space complexity: O(\log n) O ( log n )

Idea 2: Binary enumeration

  • Binary enumeration + binary judgment is legal;
    • For convenience, the legality of the judgment is written separately as a function getRes get R e s .

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;
    }
}
复制代码
  • Time complexity: O(n\log n) O ( n log n ) , the sorting complexity is O(n\log n) O ( n log n ) , the complexity is O(\log C\log n) O ( log C log n )
  • Space complexity: O(\log n) O ( log n )

C++

  • Note that global variables and input variables need to be differentiated...
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;
    }
};
复制代码
  • Time complexity: O(n\log n) O ( n log n ) , the sorting complexity is O(n\log n) O ( n log n ) , the complexity is O(\log C\log n) O ( log C log n )
  • Space complexity: O(\log n) O ( log n )

Idea 3: Enumerate in reverse order

  • Because the value range is relatively small, you can enumerate backwards directly from the end of the value range;
  • Preprocess the number of occurrences of each value, and then record the number of current legal values ​​and compare them with the current values.

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]; // 数量
            if (i == tot)
                return i;
        }
        return -1;
    }
}
复制代码
  • Time complexity: O(n+C) O ( n + C )
  • Space Complexity: O(C) 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;
    }
};
复制代码
  • Time complexity: O(n+C) O ( n + C )
  • Space Complexity: O(C) O( C )

Summarize

There are many solutions to the simple questions and I have written for a long time...

[Rust is lazy...]

Guess you like

Origin blog.csdn.net/Chenhui98/article/details/126829993