欢迎访问
讨论版列表 - 面试求职 - 主题数: 42 | 文章数: 46 | 管理员: (无)

面试求职

版面 | 文摘区 | 马克区

文章数: 1 | 分页: << 1 >>
admin
[回复] [修改] [删除] [返回版面] 1  
作者: admin, 讨论版: 面试求职, 发表时间: 2015-03-18 15:23:25 PST
标题: Wiggle Sort
关键字:

From: http://www.mitbbs.com/article_t/JobHunting/32911043.html

Given an array Of integers build a new array of integers such that every 2nd element of the array is greater than its left and right element.

eg:
[1,4,5,2,3]

o/p:
[1,4,2,5,3]

so, 3 > 2 > 1,  and 5 > 4
有没有o(n) 解法

---

Solution:

Do a quick_select(n/2), O(n) time. Then all the elements in the first half are smaller than the second half. Then construct new array where odd positions are from first half, and even positions are from second half.


--

最后修改: admin on 2015-03-18 15:23:59 PST
※ 来源: homecox.com  [来自: 128.]


Reply

Please log in first.