查看: 2308| 回复: 14
跳转到指定楼层
上一主题 下一主题
收起左侧

[CareerCup] [第二轮] 3/4-3/10 CareerCup 3.2

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
How would you design a stack which, in addition to push and pop, also has a function min which returns the minimum element? Push, pop and min should all operate in O(1) time.


发帖规范:
http://www.1point3acres.com/bbs/thread-48094-1-1.html
http://www.1point3acres.com/bbs/thread-32423-1-1.html

上一篇:[第二轮] 3/4-3/10 CareerCup 3.1
下一篇:[第二轮] 3/4-3/10 CareerCup 3.3
🔗
EchoMemory 2013-3-3 20:57:09 | 只看该作者
全局:
my first thought is having a variable cur_min within each node,function min is the top.().cur_min. And compare the min() to maintain the min in the stack before every time push a new node...
回复

使用道具 举报

🔗
grassgigi 2013-3-4 06:28:01 | 只看该作者
全局:
Add min attribute for every stack element, which trace the minimum value for all nodes below itself.
Key point here is the minimum value below one node will not change during its life time.
https://gist.github.com/chrislukkk/5078590
回复

使用道具 举报

🔗
liuzhe1218 2013-3-4 20:20:33 | 只看该作者
全局:
https://gist.github.com/mettaworld/5081901
java language
use linked list to make a stack and and get the least data in this stack with a loop.....
回复

使用道具 举报

🔗
leonsu777 2013-3-5 06:26:03 | 只看该作者
全局:
本帖最后由 leonsu777 于 2013-3-5 06:28 编辑

https://github.com/longwei/Algo/ ... 3/StackWithMin.java

another way to think about it.
The most difficult part is to peek the min ops in constant time.
1. same the above, add a min attribut to stack, so each element in stack remember the min element.2. use additonal stack to tracking the min element.
回复

使用道具 举报

🔗
lazyGoose 2013-3-5 08:40:40 | 只看该作者
全局:
本帖最后由 lazyGoose 于 2013-3-10 04:03 编辑

JAVA
初始化Stack的时候,总是把所有元素都初始化成 “0”。
为了避免排序的时候出错,在min方法里得把Stack里得有效元素抽出来。

https://gist.github.com/lazyGoose/5087021

点评

I think you min() is O(nlogn) time rather than O(1)...... btw, wrong link  发表于 2013-3-10 02:33
回复

使用道具 举报

🔗
weep_willow 2013-3-6 04:16:23 | 只看该作者
全局:
Solution1: for each element, we also store the min value of all the elements in current stack

C++ implementation

https://github.com/StanleyLee/CareerCup/blob/master/3_2.cpp

自己没有想到第二种算法,用另外一个stack来track min值,实现是应该注意push、pop操作 :)
回复

使用道具 举报

🔗
weep_willow 2013-3-6 04:46:58 | 只看该作者
全局:
用两个stack的方法实现了一遍,方法的思想如下:
用valueStack来存储正常的数据,每次push值入valueStack时,比较当前值与另一个minStack的第一个元素比较,如果小于等于minStack的第一个元素,则push入minStack;当pop时,如果pop的元素和minStack的第一元素相等,则minStack也pop出此元素。

C++ implementation

https://github.com/StanleyLee/CareerCup/blob/master/3_2_1.cpp
回复

使用道具 举报

🔗
ThunderXu 2013-3-9 10:49:04 | 只看该作者
全局:
https://gist.github.com/ThunderXu/5122243
Use a LinkedList to realize, each node has attributes "data", "next" and "min", for those nodes who has the minimal element when it is at the top of the stack, its "min" sttribute points to the minimal node when it hasn't been pushed, then if the current mininal node is poped, just set the minimal node to the node it's "min" pointer points to.
回复

使用道具 举报

🔗
Fanyare 2013-3-10 02:29:44 | 只看该作者
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表