leetcode 2293. Min Max Game(python)
持續創作,加速成長!這是我參與「掘金日新計劃 · 6 月更文挑戰」的第15天,點選檢視活動詳情
描述
You are given a 0-indexed integer array nums whose length is a power of 2.
Apply the following algorithm on nums:
- Let n be the length of nums. If n == 1, end the process. Otherwise, create a new 0-indexed integer array newNums of length n / 2.
- For every even index i where 0 <= i < n / 2, assign the value of newNums[i] as min(nums[2 * i], nums[2 * i + 1]).
- For every odd index i where 0 <= i < n / 2, assign the value of newNums[i] as max(nums[2 * i], nums[2 * i + 1]).
- Replace the array nums with newNums.
- Repeat the entire process starting from step 1.
Return the last number that remains in nums after applying the algorithm.
Example 1:
Input: nums = [1,3,5,2,4,8,2,2]
Output: 1
Explanation: The following arrays are the results of applying the algorithm repeatedly.
First: nums = [1,5,4,2]
Second: nums = [1,4]
Third: nums = [1]
1 is the last remaining number, so we return 1.
Example 2:
Input: nums = [3]
Output: 3
Explanation: 3 is already the last remaining number, so we return 3.
Note:
1 <= nums.length <= 1024
1 <= nums[i] <= 10^9
nums.length is a power of 2.
解析
根據題意,給定一個 0 開始索引的陣列 nums ,長度為 2 的倍數,讓我們按照特定的演算法來改變 nums :
- 假設 n 為 nums 的長度。 如果 n == 1 ,則結束該過程。 否則,建立一個長度為 n / 2 的新的 0 索引整數陣列 newNums。
- 對於每個 0 <= i < n / 2 的偶數索引 i ,將 newNums[i] 的值設定為 min(nums[2 * i], nums[2 * i + 1]) 。
- 對於 0 <= i < n / 2 的每個奇數索引 i ,將 newNums[i] 的值設定為 max(nums[2 * i], nums[2 * i + 1])。
- 將陣列 nums 替換為 newNums。
- 從步驟 1 開始重複整個過程。
返回執行演算法後保留在 nums 中的最後一個數字。
我們直接按照題意,進行陣列的修改即可。時間複雜度為 O(logN) ,空間複雜度為 O(N) 。
解答
class Solution(object):
def minMaxGame(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if len(nums) == 1: return nums[0]
new = []
while len(new) != 1:
new = [0] * (len(nums) // 2)
for i in range(0, len(nums), 2):
if (i // 2) % 2 == 0:
tmp = nums[i:i + 2]
new[i//2] = min(tmp)
else:
tmp = nums[i:i + 2]
new[i//2] = max(tmp)
nums = new
return new[0]
執行結果
96 / 96 test cases passed.
Status: Accepted
Runtime: 41 ms
Memory Usage: 13.7 MB
原題連結
https://leetcode.com/contest/weekly-contest-296/problems/min-max-game/
您的支援是我最大的動力
「其他文章」
- linux 安裝 ES 8.0 和 kibana 8.0 爬米共坑
- ES 8.0 及其生態配置過程一條龍服務
- 2022 年中紀
- leetcode 1048. Longest String Chain(python)
- leetcode 1354. Construct Target Array With Multiple Sums(python)
- leetcode 2311. Longest Binary Subsequence Less Than or Equal to K(python)
- leetcode 2304. Minimum Path Cost in a Grid(python)
- leetcode 2303. Calculate Amount Paid in Taxes(python)
- 帶你輕鬆入門 Bert
- leetcode 1332. Remove Palindromic Subsequences(python)
- leetcode 3. Longest Substring Without Repeating Characters (python)
- leetcode 2296. Design a Text Editor (python)
- leetcode 2293. Min Max Game(python)
- leetcode 1584. Min Cost to Connect All Points(python)
- leetcode 318. Maximum Product of Word Lengths(python)
- 沙灘 美女才是夏天的正確開啟方式
- leetcode 2275. Largest Combination With Bitwise AND Greater Than Zero (python)
- leetcode 2258. Escape the Spreading Fire(python)
- leetcode 2257. Count Unguarded Cells in the Grid(python)
- leetcode 2273. Find Resultant Array After Removing Anagrams(python)