May 2009 - May 2017 论坛八周年-你的足迹,我的骄傲



查看: 914|回复: 5

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

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


您需要 登录 才可以下载或查看,没有帐号?获取更多干活,快来注册

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,如果觉得比较好,欢迎贴出来分享)

3、每道题目有对应的帖子,除了贴解法,欢迎探讨各种follow up questions,集思广益

A30041839 发表于 2015-4-15 17:44:15 | 显示全部楼层
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.
average time complexity O(n^2), in the best case(all elements are equal) time complexity is O(n)
回复 支持 反对

使用道具 举报

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  >
         compare it with helpStack
               if  pushed data >
                    pop it from helpstak and push it to  mainstak
            until pushed data >=
  2.  if the pushed 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 ==null and ==null;   
O(n) for push and pop
O(n) for helpstack
【gist link]

【test case】 (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)
【gist link]
【test case】
回复 支持 反对

使用道具 举报

alikewmk 发表于 2015-5-3 03:50:09 | 显示全部楼层
【gist link】 ... -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.
【gist link] ... aster/Chap3/3_6.cpp
【test case】(optional,如果觉得比较好,欢迎贴出来分享)
回复 支持 反对

使用道具 举报



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

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

custom counter

GMT+8, 2017-5-27 12:47

Powered by Discuz! X3

© 2001-2013 Comsenz Inc. Design By HUXTeam

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