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

[CareerCup] 【第三轮】6.30-7.6 CareerCup 3.5

全局:

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

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

x
3.5 Implement a MyQueue class which implements a queue using two stacks.


回复解法可以按照以下格式来
【解题思路】
【时间复杂度】
【空间复杂度】
【gist link】
---------------Optional,如果觉得test case比较好,欢迎写出来分享----------------------
【test case】


Notice:
1、记得在程序注释中表明自己算法的时间、空间复杂度
2、代码难懂之处加注释
3、每道题目有对应的帖子,除了贴解法,欢迎讨论,集思广益
4、任何未尽之处,欢迎回报名帖提问,我会进一步作出修改。





上一篇:【第三轮】6.30-7.6 CareerCup 3.4
下一篇:【第三轮】6.30-7.6 CareerCup 3.6
全局:
【解题思路】
Implement class API in a similar way as the c++ queue class.
use stackIn to store incoming data, stackOut to pop data. Move all data from stackIn to stackOut in a FILO manner when stackOut runs empty. Update first and last element each time we push or pop (which is the only tricky part in this problem).
【时间复杂度】
O(1) for push
O(N) for pop when data moving is necessary
【空间复杂度】
O(N)
【gist link】
formatting...占坑……
【test case】
(0) -> push(1) -> pop(0) -> push(1) -> push(2) -> pop(1) -> push(2) -> push(3) -> pop(2) -> pop(1) -> pop(0) -> pop(0, error)
回复

使用道具 举报

全局:
【解题思路】
本来是比较简单的问题,我为了记录first and last element额外纠结了很多东西…各种debug完以后什么也不想再写了……
一切尽在comments中,最新版已经把first last那些去掉了,可以在revision history里看到……
(其实我只是饿了噗嗤而且之前发的一篇贴莫名其妙消失了TT)
【时间复杂度】
O(1) for push
O(N) for pop
【空间复杂度】
O(N)
【gist link】
https://gist.github.com/40b53ce17164e2126777
【test case】
(0)->push(1)->pop(0)->push(1)->push(2)->pop(1)->push(2)->push(3)->pop(2)->pop(1)->pop(0)->pop(0,error)
回复

使用道具 举报

全局:
【解题思路】
using two stacks stackIn and stackOut to push/pop elements
push: stackIn.push
pop: if stackOut is empty, move all elements of stackIn into stackOut
size: stackIn.size + stackOut.size
back: stackIn.peek, if stackIn is empty, move all elements of stackOut into stackIn
front: stack.Out.peek, if stackOut is empty, move all elements of stackIn into stackOut
【时间复杂度】O(1) or O(n) (if the stack is empty)
【空间复杂度】O(N)
https://gist.github.com/weazord/f231350faa96658a1016
回复

使用道具 举报

🔗
chouclee 2014-7-2 00:02:40 | 只看该作者
全局:
【解题思路】
用两个stack,一个专门供enqueue (记做pushStack),一个专门用来dequeue (popStack),每次enqueue,直接把元素push进pushStack,每次dequeue,优先从popStack中pop元素,如果没有元素,就将pushStack中的元素全部pop进popStack,再pop元素。
【时间复杂度】
enqueue: O(1)
dequeue:average O(1)
【空间复杂度】
O(n)
【gist link】
https://gist.github.com/chouclee/9fb80a96222d8b8f9d53
回复

使用道具 举报

🔗
qianhuang 2014-7-2 11:15:38 | 只看该作者
全局:
本帖最后由 qianhuang 于 2014-7-2 16:27 编辑

【解题思路】
use a stack to push data, use a stack to pop data.
【时间复杂度】
push: O(1)
pop: O(n)
【空间复杂度】
O(n)
【gist link】
https://gist.github.com/qianhuang/061661593396c1ee9602


回复

使用道具 举报

🔗
ryancooper 2014-7-2 12:08:17 | 只看该作者
全局:
qianhuang 发表于 2014-7-2 11:15
【解题思路】
use a stack to push data, use a stack to pop data.
when we push(), if all data are i ...

I don't understand why you push the data in 'out' stack back in 'in' stack when pushing new data in 'in' stack. These works are redundant.
回复

使用道具 举报

🔗
qianhuang 2014-7-2 16:25:49 | 只看该作者
全局:
ryancooper 发表于 2014-7-2 12:08
I don't understand why you push the data in 'out' stack back in 'in' stack when pushing new data i ...

you are right. Thank you for suggestion.
回复

使用道具 举报

全局:
qianhuang 发表于 2014-7-1 22:15
【解题思路】
use a stack to push data, use a stack to pop data.
【时间复杂度】

I assume your pop() function doesn't have return value, then how would you get the top element value since you don't have a top() function to keep track of it? It seems to me that you're only popping things out while losing information about them, and that's not the way queue works....

点评

为什么我自己的贴不见了嘤嘤嘤……先粘个gist来这里好了…… https://gist.github.com/40b53ce17164e2126777  发表于 2014-7-3 05:25
回复

使用道具 举报

🔗
grassgigi 2014-7-3 08:45:03 | 只看该作者
全局:
【解题思路】
Divide queue into two stacks, whose top element is head and tail of the queue

【时间复杂度】
O(1)

【空间复杂度】
O(1) amortized cost

【gist link】
https://gist.github.com/chrislukkk/6a9f88001b172ba629ed
回复

使用道具 举报

🔗
qianhuang 2014-7-3 10:23:47 | 只看该作者
全局:
ivycheung1208 发表于 2014-7-3 05:19
I assume your pop() function doesn't have return value, then how would you get the top element val ...

thank you for review.
PS: 怎么点评别人的帖子?我找不到这功能?
回复

使用道具 举报

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

本版积分规则

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