2021-12-27 swipe questions and punch in every day

Posted by Pavlos1316 on Tue, 04 Jan 2022 16:27:19 +0100

2021-12-27 swipe questions and punch in every day

Force buckle - sort

75. Color classification

Given an array of n elements including red, white and blue, sort them in place so that the elements of the same color are adjacent and arranged in the order of red, white and blue.

In this problem, we use integers 0, 1 and 2 to represent red, white and blue respectively.

Example 1:

Input: num = [2,0,2,1,1,0]
Output: [0,0,1,1,2,2]

Simply sort the array in ascending order.

class Solution {
public:
    void sortColors(vector<int>& nums) {
        for(int i=0;i<nums.size();i++)
        {
            for(int j=i+1;j<nums.size();j++)
            {
                if(nums[j]<nums[i])
                    swap(nums[j],nums[i]);
            }
        }
    }
};

747. A maximum of at least twice the other figures

Give you an integer array nums, in which there is always a unique maximum integer.

Please find the largest element in the array and check whether it is at least twice as many as every other number in the array. If yes, the subscript of the largest element is returned, otherwise - 1 is returned.

Example 1:

Input: num = [3,6,1,0]
Output: 1
Explanation: 6 is the largest integer. For other integers in the array, 6 is greater than twice the other elements in the array. The subscript of 6 is 1, so 1 is returned.

First save the corresponding position of each number in the hash table, and then sort the array, Then compare the last number with the penultimate number (that is, the largest number and the second largest number). If the largest number is twice or more than the second largest number, return the original subscript of the largest number through the hash table, otherwise return - 1. Pay attention to judge the number of elements of the array. If there is only one element, return 0.

class Solution {
public:
    int dominantIndex(vector<int>& nums) {
        if(nums.size()==1)
            return 0;
        unordered_map<int,int>mymap;
        int n=nums.size();
        for(int i=0;i<n;i++)
            mymap[nums[i]]=i;
        for(int i=0;i<n;i++)
        {
            for(int j=i+1;j<n;j++)
            {
                if(nums[j]<nums[i])
                    swap(nums[j],nums[i]);
            }
        }
        return nums[n-1]/2>=nums[n-2]?mymap[nums[n-1]]:-1;
    }
};

1051. Height checker

The school plans to take an annual commemorative photo for all the students. Students are required to line up in non decreasing height order.

The sorted height is represented by the integer array expected, where expected[i] is the height of the student who is expected to rank I in this row (the subscript starts from 0).

Give you an integer array heights, which represents the height of the current student station. heights[i] is the height of the ith student in this line (subscript starts from 0).

Return to meet heights [i]= Number of subscripts for expected [i].

Example:

Input: heights = [1,1,4,2,1,3]
Output: 3
Explanation:
Height: [1,1,4,2,1,3]
Expected: [1,1,1,2,3,4]
The student heights at subscripts 2, 4 and 5 do not match.

First take an array to save the original array, then sort the heights, and then compare it with the elements in the same position of the original array. If it is not the same counter + +, finally return the counted number.

class Solution {
public:
    int heightChecker(vector<int>& heights) {
        vector<int>v;
        v=heights;
        int n=heights.size(),ans=0,res=0;
        for(int i=0;i<n;i++)
        {
            for(int j=i+1;j<n;j++)
            {
                if(heights[i]>heights[j])
                    swap(heights[i],heights[j]);
            }
            if(heights[i]!=v[i])
                res++;
        }
        return res;
    }
};

1502. Judge whether an equal difference sequence can be formed

Give you an array of numbers arr.

If the difference between any two adjacent terms in a sequence is always equal to the same constant, then this sequence is called equal difference sequence.

If the array can be rearranged to form an arithmetic sequence, please return true; Otherwise, false is returned.

Example 1:

Input: arr = [3,5,1]
Output: true
Explanation: reorder the array to get [1,3,5] or [5,3,1]. The difference between any two adjacent terms is 2 or - 2 respectively, which can form an equal difference sequence.

Sort the array, and then judge whether the difference between two adjacent numbers is the same. If one is different, it will return false. If it has not returned false, it will return true.

class Solution {
public:
    bool canMakeArithmeticProgression(vector<int>& arr) {
        int n=arr.size(),ans=0;
        for(int i=0;i<n;i++)
        {
            for(int j=i+1;j<n;j++)
            {
                if(arr[i]>arr[j])swap(arr[i],arr[j]);
            }
            if(i==1)ans=arr[i]-arr[i-1];
            else if(i>1&&arr[i]-arr[i-1]!=ans)return false;
        }
        return true;
    }
};

Topics: Algorithm leetcode