324. Wiggle Sort II

SGLang HiCache实战:手把手教你配置KV Cache Offload,让大模型推理成本直降84% 本文详细介绍了SGLang HiCache在KV Cache Offload配置与调优中的实战应用,通过多级存储架构和智能卸载策略,显著降低大模型推理成本达84%。文章涵盖HiCache架构优势、环境配置、高级调优策略及生产环境最佳实践,帮助开发者优化GPU显存使用,提升并发处理能力。 阅读详情

324. Wiggle Sort II

1. 题目描述
题目链接

Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]…
Example 1:
Input: nums = [1, 5, 1, 1, 6, 4]
Output: One possible answer is [1, 4, 1, 5, 1, 6].
Example 2:
Input: nums = [1, 3, 2, 2, 3, 1]
Output: One possible answer is [2, 3, 1, 3, 1, 2].
Note:
You may assume all input has valid answer.
Follow Up:
Can you do it in O(n) time and/or in-place with O(1) extra space?

2. 题目分析
题目就一句话,给数组重排序,使得数组规律是奇数下标的关键字大于左右两边(偶数下标)的关键字。

3. 解决思路
一开始我想,先排序,然后将前半部分放到偶数下标,后半部分放到奇数下标不就OK了,为什么一开始就能知道先使用排序?因为我刷题是按标签分类刷题的,这题属于排序标签中,哈哈哈,我不确定如果没有标签提示,会不会立马想到先排序在移动关键字。
commit后,发现,只能通过部分事例,但有部分事例无法通过,如[4,5,5,6],使用我的思路,最后数组仍是[4,5,5,6],难道哪里不对?看看前辈的解决方案,发现换个思路,将前半段倒序填入偶数下标,将后半段倒序填入奇数下标。好气啊啊啊啊啊啊啊,换个顺序就能完美解决中间重复的问题。
即,整个算法步骤:
对数组排序,然后将前半部分倒序放到偶数下标,后半部分倒序放到奇数。时间复杂度O(nlogn),即用在排序算法中了,排序算法最优时间复杂度为O(nlogn),空间复杂度O(n)。网上看了下时间复杂度为O(n)的,有点复杂,太烧脑子了,后面有兴趣再看。

*4. 代码实现(java)

package com.algorithm.leetcode.sort;

import java.util.Arrays;

/**
 * Created by 凌 on 2019/1/6.
 * 描述:324. Wiggle Sort II
 */
public class WiggleSort {
    public static void main(String[] args) {
//        int[] nums = {1, 5, 1, 1, 6, 4};
        int[] nums = {4, 5, 5, 6};
//        int[] nums = {1, 3, 2, 2, 3, 1};
        wiggleSort(nums);
        for (int i = 0; i < nums.length; i++) {
            System.out.printf(nums[i] + "\t");
        }
        System.out.println();
    }
    /**
     * 对数组进行排序,然后将前半段倒序填入偶数下标,将后半段倒序填入奇数下标,
     * 其原理是如果题目有解,则对于排好序的数组,间隔超过n/2的两个元素必不相等。
     * 时间复杂度O(nlogn),空间复杂度O(n)。
     * @param nums
     */
    public static void wiggleSort(int[] nums) {
        int[] sort = nums.clone();
        Arrays.sort(sort);
        int index = 0;
        int count = (nums.length-1) / 2;
        //将前半段倒序填入偶数下标
        while (index < nums.length) {
            nums[index] = sort[count];
            index += 2;
            count--;
        }

        count = nums.length-1;
        index = 1;
        //将后半段倒序填入奇数下标
        while (index < nums.length) {
            nums[index] = sort[count];
            index += 2;
            count--;
        }
    }
}

手把手教你用STM32H743+FreeRTOS+LWIP搭建物联网网关(附完整工程) 本文详细介绍了如何使用STM32H743微控制器结合FreeRTOS实时操作系统和LWIP协议栈构建高性能物联网网关。内容涵盖硬件配置、FreeRTOS任务设计、LWIP协议栈优化及物联网协议集成,提供完整工程示例,助力开发者快速实现稳定可靠的网络通信枢纽。 阅读详情

相关推荐

Nordic BLE芯片RF测试

BLE设备在过蓝牙BQB,FCC,ETSI等各种认证时实验室一般都需要做RF各项指标的测试。Nordic公司的SDK包中提供了两种RF测试程序,一种是DTM,另一种是Radio test。这两种都可以做RF指标测试但各自又有区别。DTM是按照蓝牙规范中Direct test mode所要求数据格式测试(数据格式定义参看:Bluetooth Core Specification v5.0-&gt;...

5653

LeetCode:324. Wiggle Sort II

LeetCode:324. Wiggle Sort II 题目描述 Given an unsorted array nums, reorder it such that nums[0] &lt; nums[1] &gt; nums[2] &lt; nums[3]… Example 1: Input: nums = [1, 5, 1, 1, 6, 4] Output: One possible an...

杨领well的专栏 248

告别手动处理:用 Bruker NSMatlabUtilities 工具箱批量分析 AFM 力曲线数据的实战案例

本文介绍了如何使用 Bruker NSMatlabUtilities 工具箱实现 AFM 力曲线数据的批量分析,解决传统手动处理耗时且易出错的问题。通过实战案例展示工具箱的核心功能,包括数据读取、力曲线分析和可视化,帮助科研人员提升数据处理效率和准确性。

weixin_28362173的博客 219

【LeetCode】324.Wiggle Sort II(Medium)解题报告

【LeetCode】324.Wiggle Sort II(Medium)解题报告 题目地址:https://leetcode.com/problems/wiggle-sort-ii/description/ 题目描述:   Given an unsorted array nums, reorder it such that nums[0] &amp;lt; nums[1] &amp;gt; nums[...

郝春雨的博客 269

在Ansys Simplorer中设计三相逆变器,并与Maxwell FEA耦合,实现160kW PMSM

在这篇博文中,我们将探讨如何使用设计三相逆变器,并将其与Ansys Maxwell中的电磁有限元分析模型耦合,以获得准确的系统级结果。Ansys Electronics为多域仿真提供了强大的生态系统,可实现跨各种物理领域的无缝集成。Maxwell用于电磁有限元分析 (FEA),Simplorer(Twin Builder) 用于系统级逆变器和控制器设计,Icepak用于电子热仿真。例如,我们将使用 Simplorer 介绍160 kW、8 极永磁同步电机 (PMSM)的逆变器设计过程。让我们开始吧!

jkh920184196的博客 1442

6.6kW的车载充电器(On-Board Charger, OBC)电路分析

图片展示了6.6kW的车载充电器(On-Board Charger, OBC)电路,其中包含双向和单向两种拓扑结构。接下来我们将深入分析每个模块的设计、原理及其优势,以帮助理解不同结构在实际应用中的选型和应用。

weixin_43199439的博客 5732

LeetCode 324. Wiggle Sort II

324. Wiggle Sort II Given an unsorted array nums, reorder it such that nums[0] nums[2] Example: (1) Given nums = [1, 5, 1, 1, 6, 4], one possible answer is [1, 4, 1, 5, 1, 6].  (2) Given nums

柳婼 の blog 825

python爬取银行年报2

python爬取公司年报(巨潮资讯网)

bboysteed的博客 4738

leetcode 324. Wiggle Sort II

https://leetcode.com/problems/wiggle-sort-ii/ 给定一个数组,将其排序成nums[0] &lt; nums[1] &gt; nums[2] &lt; nums[3] ...的次序(摆动排序) 一、问题分析 测试用例: Example 1: Input: nums = [1, 5, 1, 1, 6, 4] Output: One possi...

小景的博客 285

动量因子在A股市场的有效性分析:量化实证研究

验证动量因子在A股市场的有效性及时间周期性分析不同时间窗口(短期/中期/长期)对动量效应的影响量化交易成本、市场流动性对动量策略收益的侵蚀程度比较A股与成熟市场(如美股)动量效应的差异特征理论框架:行为金融学解释动量效应的产生机制技术实现:Python全流程代码实现动量因子计算与回测实证分析:统计检验、风险收益指标、敏感性分析应用拓展:结合市场微观结构的策略优化方案动量因子(Momentum Factor)

2501_92132293的博客 2506

《备考系统架构设计师》 - 专栏介绍和目录

本专栏包括所有历年真题及详解、章节知识核心总结、精选高分论文、补充知识、专题总结等。欢迎订阅专栏。

数据知道的博客 4万+

leetcode题解:324. Wiggle Sort II

https://leetcode.com/problems/wiggle-sort-ii/ 1. 解题思路 参考自https://leetcode.com/problems/wiggle-sort-ii/discuss/77682/Step-by-step-explanation-of-index-mapping-in-Java 先找出数组的中位数,然后for循环遍历数组,大于中位数的放...

scut_salmon的博客 306

计算机组成原理(第三版)唐朔飞-第五章输入输出系统-课后习题

计算机组成原理(第三版)唐朔飞-第五章输入输出系统-课后习题

weixin_45735391的博客 6万+

LeetCode324. Wiggle Sort II

Given an unsorted arraynums, reorder it such thatnums[0] &lt; nums[1] &gt; nums[2] &lt; nums[3].... Example 1: Input: nums = [1, 5, 1, 1, 6, 4] Output: One possible answer is [1, 4, 1, 5, 1, 6]. ...

李歇特冯-兹拜因巴哈的博客 228

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain&#39;s apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = &quot;R. B. Palm&quot;, title = &quot;Prediction as a candidate for learning deep hierarchical models of data&quot;, year = &quot;2012&quot;,}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath(&#39;DeepLearnToolbox&#39;));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand(&#39;state&#39;,0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W&#39;); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand(&#39;state&#39;,0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = &#39;sigm&#39;;%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er &lt; 0.10, &#39;Too big error&#39;);```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand(&#39;state&#39;,0)sae = saesetup([784 100]);sae.ae{1}.activation_function = &#39;sigm&#39;;sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)&#39;)% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = &#39;sigm&#39;;nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er &lt; 0.16, &#39;Too big error&#39;);```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x&#39;,28,28,60000))/255;test_x = double(reshape(test_x&#39;,28,28,10000))/255;train_y = double(train_y&#39;);test_y = double(test_y&#39;);%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you&#39;ll get around 1.2% errorrand(&#39;state&#39;,0)cnn.layers = { struct(&#39;type&#39;, &#39;i&#39;) %input layer struct(&#39;type&#39;, &#39;c&#39;, &#39;outputmaps&#39;, 6, &#39;kernelsize&#39;, 5) %convolution layer struct(&#39;type&#39;, &#39;s&#39;, &#39;scale&#39;, 2) %sub sampling layer struct(&#39;type&#39;, &#39;c&#39;, &#39;outputmaps&#39;, 12, &#39;kernelsize&#39;, 5) %convolution layer struct(&#39;type&#39;, &#39;s&#39;, &#39;scale&#39;, 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er&lt;0.12, &#39;Too big error&#39;);```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er &lt; 0.08, &#39;Too big error&#39;);%% ex2 neural net with L2 weight decayrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er &lt; 0.1, &#39;Too big error&#39;);%% ex3 neural net with dropoutrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er &lt; 0.1, &#39;Too big error&#39;);%% ex4 neural net with sigmoid activation functionrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.activation_function = &#39;sigm&#39;; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er &lt; 0.1, &#39;Too big error&#39;);%% ex5 plotting functionalityrand(&#39;state&#39;,0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = &#39;softmax&#39;; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er &lt; 0.1, &#39;Too big error&#39;);%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand(&#39;state&#39;,0)nn = nnsetup([784 20 10]); nn.output = &#39;softmax&#39;; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er &lt; 0.1, &#39;Too big error&#39;);```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free &quot;Bitdeli Badge&quot;)

[Leetcode] 324. Wiggle Sort II 解题报告

题目: Given an unsorted array nums, reorder it such that nums[0] nums[2] . Example: (1) Given nums = [1, 5, 1, 1, 6, 4], one possible answer is [1, 4, 1, 5, 1, 6].  (2) Given nums = [1, 3

魔豆(Magicbean)的博客 955

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

Leetcode 324. Wiggle Sort II(容易理解的解法)

Leetcode 324. Wiggle Sort II 题目链接: Wiggle Sort II 难度:Medium 题目大意: 对数组nums进行排序,使得nums[0] &lt; nums[1] &gt; nums[2] &lt; nums[3]… 思路: 先把nums按升序排列,分成两半,将较大的数放在奇数下标的位置,较小的数放在偶数下标位置。 代码 class Solution { public void wiggleSort(int[] nums) { int len=

xue1236的博客 639

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

324. Wiggle Sort II 摆动排序 II

Given an unsorted array nums, reorder it such that nums[0] &amp;lt; nums[1] &amp;gt; nums[2] &amp;lt; nums[3].... Example 1: Input: nums = [1, 5, 1, 1, 6, 4] Output: One possible answer is [1, 4, 1, ...

别说话写代码的博客 387

[LeetCode 324] Wiggle Sort II

Given an unsorted array nums, reorder it such that nums[0] nums[2] . Example: (1) Given nums = [1, 5, 1, 1, 6, 4], one possible answer is [1, 4, 1, 5, 1, 6].  (2) Given nums = [1, 3, 2, 2, 3

coder 进阶的专栏 3009

排序LeetCode 324 Wiggle Sort II

LeetCode 324 Wiggle Sort II Problem Description: 将一个数组进行摆动排序,即满足num[0]&amp;lt;num[1]&amp;gt;num[2]&amp;lt;num[3].... 具体的题目信息: https://leetcode.com/problems/wiggle-sort-ii/description/ Example: Solution: ...

萤火虫啊飞呀飞 397
上一篇: 179. Largest Number
下一篇: cvte面试题收集
凌凌小博客
博客等级 码龄10年 160粉丝 157原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值