LeetCode 822. Flip Card Game

There are N cards on the table. Each card has a positive number written on the front and back (the numbers on the front and back may be different).

We can first flip any of the cards and then select one of them.

If the number on the back of the selected card X is different from the number on the front of any card, then this number is the number we want.

Which number is the smallest of these desired numbers (find the smallest of these numbers)? If no number matches the requirements, output 0.

Among them, fronts[i] and backs[i] represent the numbers on the front and back of the i card respectively.

If we swap the numbers on the front and back by flipping the card over, the number on the front becomes the number on the back, and the number on the back becomes the number on the front.

Example:

Input:fronts = [1,2,4,4,7], backs = [1,3,4,1,3]
Output: 2
Explanation: Suppose we flip the second card, then the number on the front becomes 
Then we select the second card, because now the number on the back of that card is 2, and 2 is different from the number on the front of any card, so 2 is the number we want. [1,3,4,4,7], the number on the back becomes[1,2,4,1,3]。

hint:

  1. 1 <= fronts.length == backs.length <= 1000
  2. 1 <= fronts[i] <= 2000
  3. 1 <= backs[i] <= 2000

 

Idea:

        Given two arrays fronts and backs, we need to find the minimum value that meets the requirements of the question. Among them, we can use the hash set to store cards with the same positive and negative numbers. First, iterate through the arrays fronts and backs. If fronts[i] == backs[i], then the number is a number that meets the requirements, and we add it to the hash set same.

        If there are no cards with the same positive and negative numbers, we can simply return the minimum value in the fronts and backs arrays.

        If there are cards with the same number, we need to traverse all the numbers and find the minimum value that meets the conditions. We iterate through the fronts array and update the value of res if the number is less than the minimum value of the current record, res, and is not in the hash set same. Then iterate through the backs array and update res similarly.

        Finally, if the value of res is still the initialized maximum integer value (that is, the minimum value that satisfies the condition is not found), then 0 is returned, otherwise res is returned.

The code comments are as follows:

Here are two ways to find the minimum:

vector in C++:

 int b_min = *std::min_element(begin(backs),end(backs));
 int f_min = *std::min_element(begin(fronts),end(fronts));

java one-dimensional array: 

     int b_min = Arrays.stream(backs).min().getAsInt();
     int f_min = Arrays.stream(fronts).min().getAsInt();

C++:

class Solution {
public:
    int flipgame(vector<int>& fronts, vector<int>& backs) {
        //哈希集存储正反数字相同卡片
        unordered_set<int> same;
        int n = fronts.size();
        //遍历存储
        for (int i = 0; i < n; ++i) {
            if (fronts[i] == backs[i]) {
                same.insert(fronts[i]);
            }
        }
        //无相同正反数字卡片
        //取最小值
        if(!same.size()){
            int b_min = *std::min_element(begin(backs),end(backs));
            int f_min = *std::min_element(begin(fronts),end(fronts));
            return min(b_min,f_min);
        }
        //有相同数字卡片
        //遍历所有数字,找到最小值
        int res = INT_MAX;
        for (int &x : fronts) {
            if (x < res && same.count(x) == 0) {
                res = x;
            }
        }
        for (int &x : backs) {
            if (x < res && same.count(x) == 0) {
                res = x;
            }
        }
        return res == INT_MAX ? 0 : res;
    }
};

JAVA:

class Solution {
    public int flipgame(int[] fronts, int[] backs) {
        //哈希集存储正反数字相同卡片
        Set<Integer> same = new HashSet();
        int n = fronts.length;
        //遍历存储
        for (int i = 0; i < n; ++i) {
            if (fronts[i] == backs[i]) {
                same.add(fronts[i]);
            }
        }
        //无相同正反数字卡片
        //取最小值
        if(same.size()==0){
            int b_min = Arrays.stream(backs).min().getAsInt();
            int f_min = Arrays.stream(fronts).min().getAsInt();
            return Math.min(b_min,f_min);
        }
        //有相同数字卡片
        //遍历所有数字,找到最小值
        int res = Integer.MAX_VALUE;
        for (int x : fronts) {
            if (x < res && !same.contains(x)) {
                res = x;
            }
        }
        for (int x : backs) {
            if (x < res && !same.contains(x)) {
                res = x;
            }
        }
        return res == Integer.MAX_VALUE ? 0 : res;
    }
}

Guess you like

Origin blog.csdn.net/bigBbug/article/details/132055771