一亩三分地论坛

 找回密码
 获取更多干货,去instant注册!

扫码关注一亩三分地公众号
查看: 680|回复: 5
收起左侧

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

[复制链接] |试试Instant~ |关注本帖
pure0909 发表于 2015-4-14 11:00:40 | 显示全部楼层 |阅读模式

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

您需要 登录 才可以下载或查看,没有帐号?获取更多干货,去instant注册!

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、任何未尽之处,欢迎回报名帖提问,我会进一步作出修改

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】
回复 支持 反对

使用道具 举报

alikewmk 发表于 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,如果觉得比较好,欢迎贴出来分享)
回复 支持 反对

使用道具 举报

本版积分规则

请点这里访问我们的新网站:一亩三分地Instant.

Instant搜索更强大,不扣积分,内容组织的更好更整洁!目前仍在beta版本,努力完善中!反馈请点这里

关闭

一亩三分地推荐上一条 /5 下一条

手机版|小黑屋|一亩三分地论坛声明 ( 沪ICP备11015994号 )

custom counter

GMT+8, 2016-12-8 12:35

Powered by Discuz! X3

© 2001-2013 Comsenz Inc. Design By HUXTeam

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