当前位置:网站首页>Leetcode 2293. Minimax game (yes. One pass)
Leetcode 2293. Minimax game (yes. One pass)
2022-06-10 14:01:00 【I'm not xiaohaiwa~~~~】

I'll give you a subscript from 0 The starting array of integers nums , Its length is 2 The power of .
Yes nums Execute the following algorithm :
- set up n be equal to nums The length of , If n == 1 , End The algorithm process . otherwise , establish A new array of integers newNums , The length of the new array is n
/ 2 , Subscript from 0 Start . - For satisfying 0 <= i < n / 2 Each even numbers Subscript i , take newNums[i] assignment by min(nums[2 * i],
nums[2 * i + 1]) . - For satisfying 0 <= i < n / 2 Each Odd number Subscript i , take newNums[i] assignment by max(nums[2 * i],
nums[2 * i + 1]) . - use newNums Replace nums .
- From step 1 Start repeat The whole process .
After executing the algorithm , return nums The remaining number in the .
Example 1:

Input :nums = [1,3,5,2,4,8,2,2]
Output :1
explain : Repeat the algorithm to get the following array .
The first round :nums = [1,5,4,2]
The second round :nums = [1,4]
The third round :nums = [1]
1 It's the last number left , return 1 .
Example 2:
Input :nums = [3]
Output :3
explain :3 That's the last number left , return 3 .
Tips :
- 1 <= nums.length <= 1024
- 1 <= nums[i] <= 10^9
- nums.length yes 2 The power of
Code:
class Solution {
public:
int minMaxGame(vector<int>& nums) {
do
{
int n=nums.size();
if(n==1)
return nums[0];
vector<int>newnums;
newnums.resize(nums.size()/2);
copy(nums.begin(),nums.begin()+nums.size()/2,newnums.begin());
for(int i=0;i<newnums.size();i++)
{
if((i%2)==0)
{
int num=min(nums[2*i],nums[2*i+1]);
newnums[i]=num;
}
else
{
int num=max(nums[2*i],nums[2*i+1]);
newnums[i]=num;
}
}
nums=newnums;
}while(1);
return 0;
}
};
边栏推荐
猜你喜欢

C multithreading learning note 2

二叉树和图2

基于FPGA的VGA协议实现

「大模型」之所短,「知识图谱」之所长

How to locate the hot problem of the game

新功能|Mail GPU Counter模块新增GPU图元处理和GPU Shader Cycles
![[note] the environment for setting up get injectedthread script supplemented by shellcode in Windows Security III and its use](/img/b4/f7838a7e12379190e2bc9b869839f0.png)
[note] the environment for setting up get injectedthread script supplemented by shellcode in Windows Security III and its use

2022危险化学品经营单位主要负责人考试题库及在线模拟考试

40 necessary methodologies for large factories

Application analysis of key recording and playing of wt2003h4-16s voice chip
随机推荐
Flutter drawer学习总结6
IIC总线的主要特点/通信过程/读写过程
【技术分析】探讨大世界游戏的制作流程及技术——前期流程篇
Flutter Wrap Button bottomNavigationBar学习总结4
传奇登录器提示连接服务器失败是怎么回事?怎么解决?
2022 examination question bank and online simulation examination for main principals of hazardous chemical business units
How to build Haojing technology when the computing power network is brought into reality?
22.6.7成功使用doc2vec模型生成嵌入向量
Recyclerview multi layout writing method, "my" and "personal center" page classic writing method demonstration
How to deal with the interview for the fresh senior graduates who want to self-study software testing?
算力网络照进现实,浩鲸科技如何构建?
Gin blog 总结1
Application analysis of key recording and playing of wt2003h4-16s voice chip
How to locate the hot problem of the game
Kotlin 冒泡算法 ,高德地图 过滤掉两点之间距离小于50的数据,不重复显示
还在说大学排名是笑话?最新规定:世界top50大学可以直接落户上海!
格力手机叫板苹果手机?除了嘴硬之外,恐怕再无其他
在启牛开户安全么
智慧校园安全通道及视频监控解决方案
2022广东省安全员A证第三批(主要负责人)考试练习题及在线模拟考试