Skip to content

Instantly share code, notes, and snippets.

# idea: use a 2-D array to mimic set of stacks.
# use a pivot index to indicate current stack that common pop() and push() are applyed to
# time complexity: O(1)
# space complexity: O(n)
class SetOfStack:
def __init__(self):
self.__stack = []
self.__top = -1
@monkerek
monkerek / minStack.py
Created July 2, 2014 00:13
3.2 How would you design a stack which, in addition to push and pop, also has a function min which returns the minimum element? Push, pop and min should all operate in O(1) time.
# idea: use another stack to store current minimum value each time a new element is pushed in.
# pop top values from both 2 stacks when doing pop operation
# time complexity: O(1)
# space complexity: O(n)
class myStack:
def __init__(self):
self.__array = []
self.__min = []
self.__topIndex = -1
@monkerek
monkerek / ArrayStack.py
Created July 1, 2014 11:52
3.1 Describe how you could use a single array to implement three stacks.
# idea: evenly divide an array into 3 parts to implement stacks separately.
# use 3 flags to indicate the top index of each stack. check before operation if current stack is full or empty
# this solution only works for constant length of array
# time complexity: O(1)
# space complexity: O(1) for constant length of array
class myStack:
def __init__(self):
self.__array = [-1 for n in range(30)]
self.__top = [-1, 9, 19]
@monkerek
monkerek / StrCompression.py
Created June 18, 2014 03:10
CC1.5 Implement a method to perform basic string compression using the counts of repeated characters. For example, the string aabcccccaaa would become a2blc5a3. If the "compressed" string would not become smaller than the original string, your method should return the original string.
# idea: traverse the string directly and count #characters. Then check whether the result is smaller than input string.
# time complexity: O(n)
# space complexity: O(n)
class Solution:
def StrCompression(self, string):
if string == '':
return string
ret = ''
@monkerek
monkerek / ReplaceSpace.cpp
Last active August 29, 2015 14:02
1.4 Write a method to replace all spaces in a string with'%20'. You may assume that the string has sufficient space at the end of the string to hold the additional characters, and that you are given the "true" length of the string. (Note: if implementing in Java, please use a character array so that you can perform this operation in place.)
// first find the index where the actual string ends
// then calculate the number of spaces to be converted to assure the length of the result string
// use two pointers, one pointing to the end of the former string, the other pointing to the end of the result string
// then move pointers backwards to convert spaces one by one, and this can be done in-place.
// time complexity: O(n)
// space complexity: O(1)
class Solution
{
public:
@monkerek
monkerek / isPermutation.py
Created June 16, 2014 13:10
1.3 Given two strings, write a method to decide if one is a permutation of the other.
# use a dict(hashmap) to count the appearance of characters of the first string, then minus the number of that in the other string.
# once the hash value is less than zero, return false
# time complexity: O(n)
# space complexity: O(1) for certain set of characters, say ascii
class Solution:
def isPermutation(self, str1, str2):
if len(str1) != len(str2):
return False
@monkerek
monkerek / ReverseString.cpp
Created June 16, 2014 03:20
CC1.2 Implement a function void reverse(char* str) in C or C++ which reverses a null-terminated string.
/* idea: using 2 pointers respectively point to the start and the end of the string,
swap the value they point to, then move the pointers iteratively.
time: complexity: O(n)
space complexity: O(1)
*/
void reverse(char* str)
{
if(!str)
return;
@monkerek
monkerek / UniqueChar.py
Last active August 29, 2015 14:02
CC1.1
# idea: sort the string such that same characters sit together, then traverse the string one by one
# time complexity: O(NlogN)? depends on sorting alg.
# space complexity: O(1)
class Solution:
def UniqueChar(self, string):
if string == '':
return True