class MinStack: | |||||
def __init__(self): | |||||
self.stack = [] | |||||
self.minEl = None | |||||
def push(self, x: int) -> None: | |||||
node = Node(x) | |||||
if self.minEl is None or node.value <= self.minEl.value: | |||||
node.lastMin = self.minEl | |||||
self.minEl = node | |||||
self.stack.append(node) | |||||
def pop(self) -> None: | |||||
if len(self.stack) > 0: | |||||
ret = self.stack[len(self.stack) - 1] | |||||
if ret.value == self.minEl.value: | |||||
self.minEl = self.minEl.lastMin | |||||
self.stack = self.stack[0:len(self.stack) - 1] | |||||
return ret.value | |||||
return None | |||||
def top(self) -> int: | |||||
if len(self.stack) > 0: | |||||
return self.stack[len(self.stack) - 1].value | |||||
return None | |||||
def getMin(self) -> int: | |||||
return self.minEl.value | |||||
def __str__(self): | |||||
str_result = "" | |||||
for x in self.stack: | |||||
str_result += "\n" | |||||
str_result += str(x) | |||||
return str_result | |||||
class Node: | |||||
def __init__(self, x, lastMin = None): | |||||
self.value = x | |||||
self.lastMin = lastMin | |||||
def __str__(self): | |||||
return f"Value: {str(self.value)} Last Min: {str(self.lastMin)}" | |||||
# Your MinStack object will be instantiated and called as such: | |||||
values = [] | |||||
obj = MinStack() | |||||
obj.push(-2) | |||||
obj.push(0) | |||||
obj.push(-3) | |||||
values.append(obj.getMin()) | |||||
values.append(obj.pop()) | |||||
values.append(obj.top()) | |||||
values.append(obj.getMin()) | |||||
print("Expected: [-3, -3, 0, -2]") | |||||
print("Got:", values) |
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. | |||||
push(x) -- Push element x onto stack. | |||||
pop() -- Removes the element on top of the stack. | |||||
top() -- Get the top element. | |||||
getMin() -- Retrieve the minimum element in the stack. | |||||
Example: | |||||
MinStack minStack = new MinStack(); | |||||
minStack.push(-2); | |||||
minStack.push(0); | |||||
minStack.push(-3); | |||||
minStack.getMin(); --> Returns -3. | |||||
minStack.pop(); | |||||
minStack.top(); --> Returns 0. | |||||
minStack.getMin(); --> Returns -2. | |||||
#!/bin/bash | |||||
python3 main.py |
#include <stdio.h> | |||||
int removeDuplicates(int*, int); | |||||
int removeDuplicates(int* nums, int numsSize) { | |||||
if (numsSize == 0) { | |||||
return 0; | |||||
} | |||||
int shift = 0; | |||||
int last = nums[0] + 1; // This way the first item will never be a duplicate | |||||
for (int i = 0; i < numsSize; i++) { | |||||
int value = nums[i]; | |||||
nums[i - shift] = value; | |||||
if (value == last) { | |||||
shift++; | |||||
} | |||||
last = value; | |||||
} | |||||
return numsSize - shift; | |||||
} | |||||
int main() { | |||||
printf("Expetected: Length 5, [0, 1, 2, 3, 4]\n"); | |||||
int arr[10] = {0,0,1,1,1,2,2,3,3,4}; | |||||
int newLength = removeDuplicates(&arr[0], 10); // &arr[0] is a hack to stop warning about static sized pointer being passed | |||||
printf("Got: Length: %d,", newLength); | |||||
for (int i = 0; i < newLength; i++) { | |||||
printf(" %d ", arr[i]); | |||||
} | |||||
printf("\n"); | |||||
return 0; | |||||
} |
Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length. | |||||
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. | |||||
Example 1: | |||||
Given nums = [1,1,2], | |||||
Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. | |||||
It doesn't matter what you leave beyond the returned length. | |||||
Example 2: | |||||
Given nums = [0,0,1,1,1,2,2,3,3,4], | |||||
Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively. | |||||
It doesn't matter what values are set beyond the returned length. | |||||
Clarification: | |||||
Confused why the returned value is an integer but your answer is an array? | |||||
Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well. | |||||
Internally you can think of this: | |||||
// nums is passed in by reference. (i.e., without making a copy) | |||||
int len = removeDuplicates(nums); | |||||
// any modification to nums in your function would be known by the caller. | |||||
// using the length returned by your function, it prints the first len elements. | |||||
for (int i = 0; i < len; i++) { | |||||
print(nums[i]); | |||||
} |
#!/bin/bash | |||||
gcc -o main main.c | |||||
./main | |||||
rm main |