|
| 1 | +--- |
| 2 | +title: One question daily 2293. Great mini game |
| 3 | +date: '2024.01.01 0:00' |
| 4 | +tags: |
| 5 | + - - Python |
| 6 | + - - solved |
| 7 | + - answer |
| 8 | +abbrlink: 9df6242c |
| 9 | +category: |
| 10 | +--- |
| 11 | +Although it is a simple question,But there are recursive ideas。 |
| 12 | + |
| 13 | +Constantly convert the one -dimensional list into a two -dimensional list for operation(This can be avoidedindexChaos caused by changes) |
| 14 | + |
| 15 | +Then useflagCount,Comparison of maximum and minimum values。 |
| 16 | + |
| 17 | +Just return to the end,Although it must be returned to a number,Still‘nums[0]’To avoid warning。 |
| 18 | + |
| 19 | + |
| 20 | +```html |
| 21 | +2293. Great mini game |
| 22 | +Simple |
| 23 | +39 |
| 24 | +company |
| 25 | +Google Google |
| 26 | +Give you a bidding 0 Started integer array nums ,Its length is 2 Power。 |
| 27 | + |
| 28 | +right nums Execute the following algorithm: |
| 29 | + |
| 30 | +set up n equal nums length,if n == 1 ,termination Algorithm。otherwise,create A new integer array newNums ,The length of the new array is n / 2 ,Bidding from 0 start。 |
| 31 | +right于满足 0 <= i < n / 2 Every even Bidding i ,Will newNums[i] Assignment for min(nums[2 * i], nums[2 * i + 1]) 。 |
| 32 | +right于满足 0 <= i < n / 2 Every odd number Bidding i ,Will newNums[i] Assignment for max(nums[2 * i], nums[2 * i + 1]) 。 |
| 33 | +use newNums replace nums 。 |
| 34 | +From steps 1 start repeat the whole process。 |
| 35 | +After executing the algorithm,return nums The remaining numbers。 |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | +Exemplary example 1: |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | +enter:nums = [1,3,5,2,4,8,2,2] |
| 44 | +Output:1 |
| 45 | +explain:repeat执行算法会得到下述数组。 |
| 46 | +first round:nums = [1,5,4,2] |
| 47 | +second round:nums = [1,4] |
| 48 | +Third round:nums = [1] |
| 49 | +1 Is the last number left,return 1 。 |
| 50 | +Exemplary example 2: |
| 51 | + |
| 52 | +enter:nums = [3] |
| 53 | +Output:3 |
| 54 | +explain:3 Is the last number left,return 3 。 |
| 55 | + |
| 56 | + |
| 57 | +hint: |
| 58 | + |
| 59 | +1 <= nums.length <= 1024 |
| 60 | +1 <= nums[i] <= 109 |
| 61 | +nums.length yes 2 Power |
| 62 | + |
| 63 | +``` |
| 64 | + |
| 65 | +```python |
| 66 | +class Solution: |
| 67 | + def minMaxGame(self, nums: List[int]) -> int: |
| 68 | + flag = 0 |
| 69 | + while len(nums) > 1: |
| 70 | + nums = [nums[i:i + 2] for i in range(0, len(nums), 2)] |
| 71 | + # Divide2dimension |
| 72 | + for i in range(len(nums)): |
| 73 | + if flag % 2 == 0: |
| 74 | + nums[i] = min(nums[i]) |
| 75 | + flag += 1 |
| 76 | + else: |
| 77 | + nums[i] = max(nums[i]) |
| 78 | + flag += 1 |
| 79 | + return nums[0] |
| 80 | + |
| 81 | +``` |
0 commit comments