当前位置:网站首页>Leetcode 1968. Construct an array whose elements are not equal to the average value of two adjacent elements (yes, finally solved)
Leetcode 1968. Construct an array whose elements are not equal to the average value of two adjacent elements (yes, finally solved)
2022-06-11 14:04:00 【I'm not xiaohaiwa~~~~】

To give you one Subscript from 0 Start Array of nums , The array consists of several Different from each other Integer composition . You're going to rearrange the elements in the array to meet : After rearranging , Every element in the array is It's not equal to Of adjacent elements on both sides Average .
A more formulaic statement is , The rearranged array should satisfy this property : For scope 1 <= i < nums.length - 1 Each of the i ,(nums[i-1] + nums[i+1]) / 2 It's not equal to nums[i] All set up .
Return any rearrangement result that meets the meaning of the question .
Example 1:
Input :nums = [1,2,3,4,5]
Output :[1,2,4,5,3]
explain :
i=1, nums[i] = 2, The average value of two adjacent elements is (1+4) / 2 = 2.5
i=2, nums[i] = 4, The average value of two adjacent elements is (2+5) / 2 = 3.5
i=3, nums[i] = 5, The average value of two adjacent elements is (4+3) / 2 = 3.5
Example 2:
Input :nums = [6,2,0,9,7]
Output :[9,7,6,2,0]
explain :
i=1, nums[i] = 7, The average value of two adjacent elements is (9+6) / 2 = 7.5
i=2, nums[i] = 6, The average value of two adjacent elements is (7+2) / 2 = 4.5
i=3, nums[i] = 2, The average value of two adjacent elements is (6+0) / 2 = 3
Tips :
- 3 <= nums.length <= 10^5
- 0 <= nums[i] <= 10^5
Main idea :
What I'm thinking about here is , Two big ones and one small one , The so-called "two big and one small" is to add a big element first , Then add a small element , Add another big element , So you can sort the elements first and add them to the new array according to this idea ( In fact, it is the swing sequence that can be solved )
Code:
class Solution {
public:
vector<int> rearrangeArray(vector<int>& nums) {
sort(nums.begin(),nums.end());
vector<int>res;
int end=nums.size();
int start=0;
for(int i=0;i<nums.size();i++)
{
if((i%2)==0)
{
res.push_back(nums[--end]);
}
else
res.push_back(nums[start++]);
}
return res;
}
};
边栏推荐
- Customize terrain providers (terrain plugin framework) -04
- 风电随机性动态经济调度模型(Matlab代码实现)
- tp6基于whoops的异常接管(漂亮的界面)
- vim二次替换
- SQL must know and know
- [issue 268] accidentally submit the test code to the production environment. I will teach you six ways to solve it in seconds!
- Question bank and answers for 2022 tool fitter (intermediate) operation certificate examination
- Energy storage operation and configuration analysis of high proportion wind power system (realized by Matlab)
- Terraform + Ansible实现基础设施及配置管理
- Kubernetes binary installation (v1.20.15) (VII) plug a work node
猜你喜欢

Distributed file system and enterprise application -- elk enterprise log analysis system

阿里一面,谈谈策略模式在项目中的使用

cadence SPB17.4 - allegro - allegro_ free_ viewer

Can't understand kotlin source code? Starting with the contracts function~
![[pyhton crawler] regular expression](/img/d3/578514b2d19d5f4ed246a33a333d14.jpg)
[pyhton crawler] regular expression
![[Clickhouse] the clckhouse view can be inserted but not queried](/img/72/717d70af49be2b1dc2331fe603d106.jpg)
[Clickhouse] the clckhouse view can be inserted but not queried

Just after the college entrance examination, I was confused and didn't know what to do? Tell me what I think

AGV机器人RFID传感器CK-G06A与西门子1200PLC应用手册

Bs-xx-007 registered residence management system based on JSP

Work summary: it took a long time to write SQL because of Cartesian product problem (Cartesian product summary attached)
随机推荐
Container -- reverse content -- use of explosion, splicing, and inversion functions
SAP Spartacus checkout 流程使用 url 粘贴直接跳转到 delivery mode不能打开页面的原因
应用编排Nomad与Kubernetes对比
tf.data(二) —— 并行化 tf.data.Dataset 生成器
【clickhouse专栏】新建库角色用户初始化
SQL: how to use the data of purchase order and sales order to calculate commodity cost by moving weighted average method
三级分类展示
Interview to build an aircraft carrier, screw on the job, pay
2022工具钳工(中级)操作证考试题库及答案
Leetcode 1962. 移除石子使总数最小(应该是向上取整)
[Clickhouse] the clckhouse view can be inserted but not queried
Question bank and answers for 2022 tool fitter (intermediate) operation certificate examination
Leetcode 1968. 构造元素不等于两相邻元素平均值的数组(可以,终于解决)
Redis configuration and optimization of NoSQL
How to quickly compress the size of video?
HR doesn't want to read such a PDF technical resume at all. How can it be in the hands of the interviewer?
.NET C#基础(6):命名空间 - 有名字的作用域
Distributed file system and enterprise application -- elk enterprise log analysis system
Explanation of waitgroup usage in go language learning
Pki/tls Swiss Army knife cfssl