[July algorithm] day07 hash table

970. Strong integers

Title description:

Given three integers x , y , and bound , return a list of all strong integers with values ​​less than or equal to bound .

We consider an integer to be a strong integer if it can be expressed as xi + yj for integers i >= 0 and j >= 0.

You can return answers in any order. In your answer, each value appears at most once.

Ideas:

First calculate the nth power of x and y respectively;

Then add the results of the nth power of x and y and put them into the map, which can be deduplicated;

Push the key value of the map to the result array;

Among them, pay attention to the situation of 1;

class Solution {
public:
    vector<int> powerfulIntegers(int x, int y, int bound) {
        unordered_map<int, int> ans;
        vector<int> xval;
        vector<int> yval;
        vector<int> ret;
        
        for (int i = 0; pow(x,i) < bound; ++i) {
            xval.push_back(pow(x,i));
            if (x == 1) {
                break;
            }
        }
        
        for (int i = 0; pow(y,i) < bound; ++i) {
            yval.push_back(pow(y,i));
            if (y == 1) {
                break;
            }
        }
        
        for (int i = 0; i < xval.size(); ++i) {
            for (int j = 0; j < yval.size(); ++j) {
                int val = xval[i] + yval[j];
                if (val > bound) {
                    break;
                } else {
                    ans[val] = 1;
                }
            }
        }
        for (auto it = ans.begin(); it != ans.end(); ++it) {
            ret.push_back(it->first);
        }
        return ret;

    }
};

914. Card Grouping

Title description:

Given a deck of cards, each card has an integer written on it.

At this point, you need to choose a number X, so that we can divide the whole deck into 1 or more groups according to the following rules:

Each set has X cards.
All cards in the set have the same integer number written on them.
Returns true only if your optional X >= 2.

Ideas:

First count the number of occurrences of each number to find the smallest number of occurrences;

Because each group has the same card value, and the number of cards in each group starts from 2 to mingroup, each time the number of occurrences of each letter is calculated, if there is a remainder, it means that the current number of cards is unreasonable;

This question can also find a common divisor for the number of occurrences of all numbers. If the common divisor is greater than or equal to 2, it means that it can be allocated according to the requirements of the question, otherwise it returns false;

class Solution {
public:
    bool hasGroupsSizeX(vector<int>& deck) {
        unordered_map<int, int> ump;
        int mingroup = 10010;
        sort(deck.begin(),deck.end());
        for (int i = 0; i < deck.size(); ++i) {
            ump[deck[i]] ++;
        }
        for (auto it = ump.begin(); it != ump.end(); ++it) {
            mingroup = min(mingroup, it->second);
        }
        
        
        for (int i = 2; i <= mingroup; ++ i) {
            bool flag = true;
            for (auto it = ump.begin(); it != ump.end(); ++it) {
                if (it->second % i) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                return true;
            }

        }
        return false;   

    }
};

Interview question 17.05. Letters and numbers

Title description:

Given an array of letters and numbers, find the longest subarray that contains the same number of letters and numbers.

Return the subarray. If there are multiple longest subarrays, return the subarray with the smallest subscript value of the left endpoint. If no such array exists, returns an empty array.

Ideas:

prefix sum + hash table

Use a prefix and array sum[i] to represent the sum of the previous i items, if it is a number, add 1 to the weight, otherwise subtract 1; for any
interval, sum[r]-sum[l-1] represents [r,l] When the sum is 0, it means that the number of numbers and letters is equal;
therefore, the equation can be transformed into sum[r] == sum[l-1], so that sum[l -1] Insert the hash table, find sum[r] in the hash table every time you traverse, find a set of solutions that meet the conditions, and calculate the length to update the maximum length;

class Solution {
public:
    vector<string> findLongestSubarray(vector<string>& array) {
        int sum[100010]; //存前缀和
        unordered_map<int,int> hash;
        vector<string> ans;
        int pre = 0;
        for (int i = 0; i < array.size(); ++i) {
            sum[i] = pre;
            if (array[i][0] >= '0' && array[i][0] <= '9') {
                sum[i] ++;
            } else {
                sum[i] --;
            }
            pre = sum[i];
        }

        hash[0] = -1;
        int l = 0, r = -1, maxlen = 0; //记录最大长度,最大长度的左边界和右边界
        for (int i = 0; i < array.size(); ++i) {
            if (hash.find(sum[i]) != hash.end()) {
                int len = i - hash[sum[i]];
                if (len > maxlen) {
                    maxlen = len;
                    l = hash[sum[i]] + 1;
                    r = i;
                }
            } else {
                hash[sum[i]] = i;
            }
        }

        for (int i = l; i <= r; ++i) {
            ans.push_back(array[i]);
        }
        return ans;

    }
};
//重做

1497. Check if Pair of Arrays is Divisible by k

Title description:

You are given an integer array arr and an integer k where the array length is even and the value is n.

Now we need to divide the array into exactly n / 2 pairs, so that the sum of each pair of numbers is divisible by k.

Return True if such a division exists; otherwise, return False.

Ideas:

First perform a remainder operation on all numbers; (note the case of negative numbers)

When the number of remainder 0 is an odd number, it is impossible to divide into n/2 teams, and return false directly;

Compare whether the number of values ​​i and ki are the same, if not, return false;

Otherwise return true;

class Solution {
public:
    bool canArrange(vector<int>& arr, int k) {
        int n = arr.size();
        int hash[1000010]; //arr取余k之后的数字
        memset(hash, 0, sizeof(hash));
        for (int i = 0; i < n; ++i) {
            int mod = (arr[i]%k + k) % k;
            hash[mod] ++;
        }
        if (hash[0] & 1) {
            return false;
        }
        for (int i = 1; i < k; ++i) {
            if (hash[i] != hash[k-i]) {
                return false;
            }
        }
        return true;
        
    }
};

おすすめ

転載: blog.csdn.net/ilovejujube/article/details/125658528