【Likou】822. Flip card game

The following is the official solution of Lituo, and my code

topic

Title meaning

have nn on the tablen cards, each with a positive number written on the front and back (the numbers on the front and back may be different).
We can turn over any card first, and then choose one of the cards. If the number xx
on the back of the selected cardx 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 none of the numbers meet the requirements, output0 00 .
Among them,fronts [ i ] fronts[i]fronts[i] b a c k s [ i ] backs[i] b c k s [ i ] respectively represent theiiNumbers on the front and back of i cards.
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 1

输入 f r o n t s = [ 1 , 2 , 4 , 4 , 7 ] , b a c k s = [ 1 , 3 , 4 , 1 , 3 ] fronts = [1,2,4,4,7], backs = [1,3,4,1,3] fronts=[1,2,4,4,7],backs=[1,3,4,1,3 ]
Output:2 22
Explanation: Suppose we turn over the second card, then the number on the front becomes[ 1 , 3 , 4 , 4 , 7 ] [1,3,4,4,7][1,3,4,4,7 ] , the number on the back becomes[ 1 , 2 , 4 , 1 , 3 ] [1,2,4,1,3][1,2,4,1,3 ] .
Then we choose the second card, because now the number on the back of the card is2 22 2 2 2 is different from the number on the face of any card, so2 22 is the number we want.

Example 2

输入 f r o n t s = [ 1 ] , b a c k s = [ 1 ] fronts = [1], backs = [1] fronts=[1],backs=[ 1 ]
output:0 00
Explanation: No matter how flipped, you can’t get the desired number, so return0 00

hint

  • n = = f r o n t s . l e n g t h = = b a c k s . l e n g t h n == fronts.length == backs.length n==fronts.length==backs.length
  • 1 < = n < = 1000 1 <= n <= 1000 1<=n<=1000
  • 1 < = f r o n t s [ i ] , b a c k s [ i ] < = 2000 1 <= fronts[i], backs[i] <= 2000 1<=fronts[i],backs[i]<=2000

official solution

hash set

Algorithm summary

If a card has the same number on both sides, no matter how the card is turned over, the number on the front will always be the same number, which cannot be the last selected number xxx .
According to this idea, we first traverse all the cards, if the two numbers on the cards are the same, then join the hash setsame sameIn s am e , all numbers except this set can be selected asxxx , we just need to go through all the numbers again and find the minimum value. Finally, we return the minimum value found, or0 00

the complexity

  • Time complexity: O ( n ) O(n)O ( n ) , wherennn is the number of cards.
  • Space Complexity: O ( n ) O(n)O ( n ) , wherennn is the number of cards.

My code

Java

class Solution {
    
    
    public int flipgame(int[] fronts, int[] backs) {
    
    
        Set<Integer> same = new HashSet<>();
        for (int i = 0; i < fronts.length; i++) {
    
    
            if (fronts[i] == backs[i]) {
    
    
                same.add(fronts[i]);
            }
        }
        
        //牌正反面的数字最大为 2000,所以可以将答案初始化为 3000
        //如果找不到符合的牌面,res%3000 即是返回 0
        int res = 3000;
        for (int i : fronts) {
    
    
            if (i < res && !same.contains(i)) {
    
    
                res = i;
            }
        }
        for (int i : backs) {
    
    
            if (i < res && !same.contains(i)) {
    
    
                res = i;
            }
        }
        
        return res % 3000;
    }
}

Submission result: passed

  • Execution time: 3 ms 3ms3m s _
  • Memory Consumption: 42.6MB 42.6MB42.6MB

Guess you like

Origin blog.csdn.net/qq_45256357/article/details/132069808