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

[CareerCup] 【第四轮】4.13 - 4.19 Career Cup 3.5

全局:

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

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

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

【解题思路】
【时间复杂度】
【空间复杂度】
【gist link]
【test case】(optional,如果觉得比较好,欢迎贴出来分享)

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



上一篇:【第四轮】4.13 - 4.19 Career Cup 3.4
下一篇:【第四轮】4.13 - 4.19 Career Cup 3.6
🔗
A30041839 2015-4-15 17:09:39 | 只看该作者
全局:
[solution]
when pushing number x to the queue:
1)push x to the first stack
when popping element from the queue:
1)if the second stack is not empty, pop an element from the second stack,return
2)if the first stack is not empty, move its elements to the second stack, and pop an element from the second stack, return
3)raise an "empty queue" exception
[time]
push,back operates in O(1), front and pop operates in O(n)
[space]
O(n)
[gist]
https://gist.github.com/A30041839/1b30ac21cefde12e2804
回复

使用道具 举报

🔗
iker01 2015-4-16 22:00:59 | 只看该作者
全局:
【解题思路】
# solution
# enqueue -- same as the original operation
# dequeue -- use the second stack to store the old element, and pop the oldest one
【时间复杂度】
# for enqueue and isEmpty O(1) in time
# for dequeue O(n) in time
【空间复杂度】
O(n)
【gist link】
https://gist.github.com/zhangjiang2013/7ca3d2df19679845585c
回复

使用道具 举报

🔗
laonong15 2015-4-16 23:21:57 | 只看该作者
全局:
【解题思路】
use one Stack for  Enqueue -----EnQueueStack
* use another Stack for Dequeue----DeQueueStack.
* if DeQueueStack is  empty  then  pop  from EnqueueStack and  push to  DequeueStac one by one.
【时间复杂度】
Enqueue O(1) , Dequeue  O(n)
【空间复杂度】
O(n)
【gist link]
https://gist.github.com/michaelniu/f1cc4577a700db8cc4dd
【test case】
https://gist.github.com/michaelniu/f1cc4577a700db8cc4dd(main)
Code is runnable
回复

使用道具 举报

🔗
JamesJi 2015-4-18 22:03:25 | 只看该作者
全局:
【解题思路】
1. naive: using two stack,because queue is FIFO but stack is FILO, when need to pop element, just push every element from si into s2 and then do s2.pop(). The peek() is the same method, after operation, just push every element from s2 back to s1.
2. instead doing reverse operation each time, we can hold two stacks, one is record newStack, the other hold oldStack, when we need to push(), just push newElem onto newStack, when we need to pop,just do pop operation on oldStack,if oldStack is empty, then push elements from newStack to oldStack in reverse order.
【时间复杂度】
Enqueue O(1) , Dequeue  O(n)
【空间复杂度】
O(n)
【gist link]
https://gist.github.com/JamesJi9277/425d9a0b8ae477ab5aaa
【test case】
回复

使用道具 举报

🔗
daphne_ying 2015-4-28 14:56:59 | 只看该作者
全局:
[solution]
when adding x to the queue, just push it to the first stack.
when removing from the queue:
1)if the second stack is not empty, directly pop from the second stack
2)else if the first stack is not empty, shift all elements to the second stack, and pop from the second stack
3)else throw exception
[time]
add O(1), remove and peek operates in O(n)
[space]
O(n)
[gist]
https://gist.github.com/kelly-us/ca1854a0b3d928a2db46
回复

使用道具 举报

地里匿名用户
🔗
匿名用户-MYPGC  2015-5-2 04:01:13
【解题思路】
建立两个栈,一个做输入栈,一个做输出栈,并使用延迟加载(lazy loading)的思路,即仅当输出栈没有可以取的元素时,才将输入栈的元素压入输出栈
【时间复杂度】
push O(1)
pop 输出栈为空时O(m), m是输入栈内的元素个数,其他时候O(1)
【空间复杂度】
O(m)
【gist link】
https://gist.github.com/alikewmk ... le-3-5-myqueue-java
回复

使用道具 举报

🔗
Godbless 2015-6-7 09:36:00 | 只看该作者
全局:
【解题思路】Use one stack1 as the temporary stack to store the newly pushed elements, use the
        other stack2 to store the oldest elements. Once stack2 is empty, move all elements from
        stack1 to stack 2. pop and peek from stack2, while push new element to stack1.
【时间复杂度】O(1) for push, worst case O(n) for peek and pop
【空间复杂度】O(n)
【gist link] https://github.com/StephenWeiXu/ ... aster/Chap3/3_5.cpp
【test case】(optional,如果觉得比较好,欢迎贴出来分享)
回复

使用道具 举报

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

本版积分规则

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