Skip to content

Instantly share code, notes, and snippets.

@lawliet89
Created February 16, 2015 13:07
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lawliet89/8bccec514e38a981bcda to your computer and use it in GitHub Desktop.
Save lawliet89/8bccec514e38a981bcda to your computer and use it in GitHub Desktop.
HackerRank Super Stack Solution
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace Solution
{
class Solution
{
static void Main(string[] args)
{
var noCommands = Convert.ToInt32(System.Console.ReadLine());
var stack = new LinkedList<int>();
for (var i = 0; i < noCommands; ++i)
{
var command = Console.ReadLine();
var split = command.Split(' ');
if (split[0] == "push")
{
var number = Convert.ToInt32(split[1]);
stack.AddFirst(number);
}
else if (split[0] == "pop" && stack.Count > 0)
{
stack.RemoveFirst();
}
else if (split[0] == "inc")
{
var count = Convert.ToInt32(split[1]);
var increment = Convert.ToInt32(split[2]);
count = Math.Min(stack.Count, count);
var node = stack.Last;
for (var j = 0; j < count; ++j)
{
node.Value += increment;
node = node.Previous;
}
}
PrintTop(stack);
}
}
static void PrintTop(LinkedList<int> stack)
{
if (stack.Count == 0)
{
System.Console.WriteLine("EMPTY");
}
else
{
System.Console.WriteLine("{0}", stack.First.Value);
}
}
}
}
@lawliet89
Copy link
Author

Fails for some edge cases that I might not have spotted.

@VishalTheAries
Copy link

Did you managed to find why it fails for certain test cases? Mine failed for few too.... similar approach

@neerajkumar
Copy link

Can you paste the full question?

@songliao-branch
Copy link

it fails because in the "inc" operation if k is large, time will exceed limit,one way to solve this is to use a hashmap to remember the values that should add to the bottom. java code here:

static void superStack(String[] operations) {
        List<Integer> stack = new ArrayList<>();
        HashMap<Integer, Integer> map = new HashMap<>();
        for (String s : operations) {
            if (s.startsWith("push")) {
                int value = Integer.parseInt(s.split(" ")[1]);
                stack.add(value);
            } else if (s.startsWith("pop")) {
                stack.remove(stack.size() - 1);
                //modify the hashmap
                for(Integer i: map.keySet()) {
                    if (stack.size() < i) {
                        map.put(i-1, map.get(i));
                        map.remove(i);
                    }
                }
            } else if (s.startsWith("inc")) {
                String[] splits = s.split(" ");
                int e = Integer.parseInt(splits[1]);
                int value = Integer.parseInt(splits[2]);
                map.put(e, map.getOrDefault(e, 0) + value);
            }

            if (stack.size() == 0) {
                System.out.println("EMPTY");
            } else {
                int value = stack.get(stack.size()-1);
                for(Integer i: map.keySet()) {
                    if (stack.size() <= i) {
                        value+=map.get(i);
                    }
                }
                System.out.println(value);
            }
        }
    }

@TheRayTracer
Copy link

TheRayTracer commented Feb 18, 2019

Hi Liaoxsong, using your exact Java8 code fragment only 5/14 test cases pass. And the O(n*n) test cases still timeout. Sorry.

@Abhiramianbu
Copy link

if we use treemap to save the inc operations
we can just process the tailmap for the pop operation instead whole hashmap.
will this work?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment