📣 独立日限时特惠: VIP通行证立减$68
查看: 3252| 回复: 20
跳转到指定楼层
上一主题 下一主题
收起左侧

[CareerCup] [第二轮] 3/4-3/10 CareerCup 3.6

全局:

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

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

x
Write a program to sort a stack in ascending order (with biggest items on top). You may use additional stacks 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.

发帖规范:
http://www.1point3acres.com/bbs/thread-48094-1-1.html
http://www.1point3acres.com/bbs/thread-32423-1-1.html

上一篇:[第二轮] 3/4-3/10 CareerCup 3.5
下一篇:关于Ruby
🔗
EchoMemory 2013-3-3 21:23:09 | 只看该作者
全局:
No limitations to the number of the stack? Push each value to a single stack, then sort?
回复

使用道具 举报

🔗
lty900301 2013-3-4 12:26:09 | 只看该作者
全局:
EchoMemory 发表于 2013-3-3 21:23
No limitations to the number of the stack? Push each value to a single stack, then sort?

you are given only one additional stack.
回复

使用道具 举报

🔗
EchoMemory 2013-3-4 12:34:26 | 只看该作者
全局:
lty900301 发表于 2013-3-4 12:26
you are given only one additional stack.

ok..my solution is based in the description of the problem, where "You may use additional stacks" used Nouns in Plural Form...
回复

使用道具 举报

🔗
EchoMemory 2013-3-4 12:53:25 | 只看该作者
全局:
lty900301 发表于 2013-3-4 12:26
you are given only one additional stack.

could I use additional variable?
回复

使用道具 举报

🔗
ryancooper 2013-3-4 13:12:27 | 只看该作者
全局:
lty900301 发表于 2013-3-4 12:26
you are given only one additional stack.

I don't think they specify the number of stacks we can use. So, theoretically, EchoMemory's algorithm is OK.
回复

使用道具 举报

🔗
ryancooper 2013-3-4 13:13:28 | 只看该作者
全局:
EchoMemory 发表于 2013-3-4 12:53
could I use additional variable?

I think you could, but I guess they prefer not to
回复

使用道具 举报

🔗
ryancooper 2013-3-4 13:23:22 | 只看该作者
全局:
I have a not very good solution, because I think the algorithm involves too many pop and push. I use two additional stacks.
The algorithm is somewhat similar to insertion sort: we maintain a sorted stack s3(the top element of this stack is the smallest one), then we compare the top of the original stack s1 with every element in s3, using stack s2 as temporal holder. After all elements are in the right position, then we pop every elements in s3 back into s1.
https://gist.github.com/ryancooper/9e58b62b43d7373aaa9f
回复

使用道具 举报

🔗
EchoMemory 2013-3-4 14:00:28 | 只看该作者
全局:
本帖最后由 EchoMemory 于 2013-3-4 15:20 编辑
ryancooper 发表于 2013-3-4 13:13
I think you could, but I guess they prefer not to

if I could use additional variable(not array) to record the min_val and pop_size in my sub stack, I could use two stacks to sort. Every time push pop_size times(initial stack.size()) from first stack to the second stack(), and record the min_element(should update every time). When push_back, firstly push the min_val to the first stack; pop_size--; and push the value in the second stack to first stack. if the value pushing_back is the min_val, just delete.
回复

使用道具 举报

🔗
ryancooper 2013-3-4 14:21:30 | 只看该作者
全局:
本帖最后由 ryancooper 于 2013-3-4 14:26 编辑
EchoMemory 发表于 2013-3-4 14:00
if I could use additional variable(not array) to record the max_val and pop_size in my sub stack,  ...

If I can be allowed to use one additional variable, then I can reduce two additional stacks to just one too.And it seems you have a slightly mistake. Because at last, we have the biggest element at top of the stack. If I comprehend your algorithm correctly, then you seem to put the largest one at the bottom of the stack
回复

使用道具 举报

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

本版积分规则

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