📣 黑五价提前开抢|VIP一年通行证5折,蓝莓、Offer多多同步特惠
查看: 2519| 回复: 5
跳转到指定楼层
上一主题 下一主题
收起左侧

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

全局:

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

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

x
3.6  Write a program to sort a stack in ascending order (with biggest items on top).
You may use at most one additional stack to hold items, but you may not copy
the elements into any other data structure (such as an array). The stack supports
the following operations: push, pop, peek, and isEmpty.

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

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


上一篇:【第四轮】4.13 - 4.19 Career Cup 3.5
下一篇:【第四轮】4.13 - 4.19 Career Cup 3.7
🔗
A30041839 2015-4-15 17:44:15 | 只看该作者
全局:
[solution]
suppose we have stack s1 (which is to sort) and an additional stack s2. In each round, we move elements from s1 to s2, and record the maximum element(curmax) in s1. then we move elements back to s1 using the following rule:
1)if current top of s2 is equal to curmax, increase a counter by 1
2)if the current top of s2 is not equal to curmax, push it into s1
after iteration of s2, push curmax counter times to s2

when s1 is empty, we move all elements from s2 to s1, and the maximum element is at the top of s1.
[time]
average time complexity O(n^2), in the best case(all elements are equal) time complexity is O(n)
[space]
O(n)
[gist]
https://gist.github.com/A30041839/d27757b0df432b0161c5
回复

使用道具 举报

🔗
laonong15 2015-4-17 00:29:52 | 只看该作者
全局:
【解题思路】
set  two stacks  mainStack and  helpStack
  the helpStack  store the  data  that bigger then pushed data;
  push  function :
     
   1. if the pushed data  > mainStack.top.data
         compare it with helpStack
             repeat
               if  pushed data > helpstack.top
                    pop it from helpstak and push it to  mainstak
            until pushed data >=   helpstack.top
  2.  if the pushed data < manistack.top.data
     repeat pop mainstack top and push it to  helpstack
     
  3.   finally push the data to the  mainstack
  
pop function
         1.if helpstack  top is not  null repeat to   
            pop the data from helpstack  one by one to main stackl until
         2.pop the top;
peak function  
    similar to pop
isEmpty  rturn  main.top ==null and help.top ==null;   
【时间复杂度】
O(n) for push and pop
【空间复杂度】
O(n) for helpstack
【gist link]
https://gist.github.com/michaelniu/d1145e1d5fe2ea3644a6

【test case】
https://gist.github.com/michaelniu/d1145e1d5fe2ea3644a6 (main)
code is runnalbe
回复

使用道具 举报

🔗
JamesJi 2015-4-18 22:50:13 | 只看该作者
全局:
【解题思路】
because limit of one stack, but we can still use a temp to store element which is poped from one of the stack. Then can compare the temp with s2.pop(), if s2.pop() greater than temp, just push s2.pop() into s1, else s2.push(temp)
【时间复杂度】
O(n^2)
【空间复杂度】
O(n)
【gist link]
https://gist.github.com/JamesJi9277/6160fac2a7f36b6bf548
【test case】
回复

使用道具 举报

地里匿名用户
🔗
匿名用户-0OAZR  2015-5-3 03:50:09
【解题思路】
选择排序
【时间复杂度】
O(n^2)
【空间复杂度】
O(2n+1)
【gist link】
https://gist.github.com/alikewmk ... -3-6-sortstack-java
回复

使用道具 举报

🔗
Godbless 2015-6-7 10:13:15 | 只看该作者
全局:
【解题思路】Use the temp stack to store the sorted result, and use a temp variable to
        stor the top element poped from original stack. Each time pop a element x1 from the
        original stack, compare it with the top elemtn x2 of the temp stack, if x1>x2, directly
        push x1 into temp stack, else if x1<x2, pop x2 from temp stack to original stack, and then
        compare x1 to the next top element of temp stack. Repeat this untill x1 find its right place
        in the temp stack.
【时间复杂度】O(n^2)
【空间复杂度】O(n)
【gist link] https://github.com/StephenWeiXu/ ... aster/Chap3/3_6.cpp
【test case】(optional,如果觉得比较好,欢迎贴出来分享)
回复

使用道具 举报

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

本版积分规则

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