当前位置:网站首页>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;
}
};
边栏推荐
- /usr/bin/gzip: 1: ELF : not found /usr/bin/gzip: 3: : not found /usr/bin/gzip: 4: Syntax erro
- tampermonkey这玩意如何替换flash播放器为h5播放器?
- vim二次替换
- Which exchange is PTA futures on? How can PTA futures be safe?
- JSTL 自定义标签
- SQL数据查询之单表查询
- SQL must know and know
- JSTL custom label
- 自定义Terraform-Providers(Terraform Plugin Framework)-04
- Checkout design in SAP Spartacus
猜你喜欢

无延时/无延迟视频直播实例效果案例
Explanation of waitgroup usage in go language learning

Unsealing easy QF PDA helps enterprises improve ERP management
![[Clickhouse] the clckhouse view can be inserted but not queried](/img/72/717d70af49be2b1dc2331fe603d106.jpg)
[Clickhouse] the clckhouse view can be inserted but not queried

小米9线刷ROM

Introduction to reverse learning - excellent assembly debugging tool OllyDbg

The application of machine learning in database cardinality estimation

airtest自动化测试

2022年全国最新消防设施操作员(初级消防设施操作员)题库及答案

LNMP deployment
随机推荐
Application choreography nomad vs. kubernetes
Hamad application scheduling scheme 06 of hashicopy (configure task)
cadence SPB17.4 - group operation(add to group, view group list, delete group)
2022.2.26 library management system 2 - module 2: reader management system
The global mobile phone market is declining, and even apple does not expect too much of the iphone14
My struggle: my years in foreign enterprises (1)
[pyhton crawler] regular expression
Sqlmap detection SQL lab range
Nomad application layout scheme 07 of hashicopy (submit job)
Single table query of SQL data query
2022年全国最新消防设施操作员(初级消防设施操作员)题库及答案
使用华为HECS云服务器打造Telegraf+Influxdb+Grafana 监控系统【华为云至简致远】
[signal de-noising] chromatographic baseline estimation and de-noising based on sparsity (beads) with matlab code and papers
How to manage the server to make the website stable and smooth
Hashicopy之nomad应用编排方案06(配置task)
Implementation of VGA protocol based on FPGA
[signal processing] digital signal processing matlab design with GUI interface and report
Ali talked about the use of strategic mode in the project
C # set the cursor shape of forms and systems
SQL:如何用采购单销售单的数据 通过移动加权平均法 计算商品成本