Skip to content

Instantly share code, notes, and snippets.

View HDegano's full-sized avatar

Herbert HDegano

  • Amazon.com
  • Austin, TX
View GitHub Profile
@HDegano
HDegano / IsBTBalanced.cs
Last active August 29, 2015 14:19
Check if a BST is balanced
public class TreeNode {
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int x) { val = x; }
}
public bool IsBalanced(TreeNode root) {
if(root == null) return true;
@HDegano
HDegano / MinStack.cs
Last active August 29, 2015 14:19
Stack with Min/Max operation constant time.
public class MinStack {
private Stack<int> _stack = new Stack<int>();
private Stack<int> _min = new Stack<int>();
public void Push(int x) {
_stack.Push(x);
if(_min.Count == 0) _min.Push(x);
else {
@HDegano
HDegano / balanced_parenthesis.cs
Last active August 29, 2015 14:07
Check if the parenthesis/brackets are balanced
public static bool balanced_brackets(string S)
{
var open = new List<char>(){'(', '{', '['};
var stack = new Stack<char>();
foreach (var i in S)
{
if (open.Contains(i)) stack.Push(i);
@HDegano
HDegano / FindKInRotatedSortedArray.cs
Last active August 29, 2015 14:07
Find element in a rotated sorted Array
/*
Assumes no duplicate
O(logN)
*/
public int FindElementInRotatedSortedArray(int[] ar, int k){
int n = ar.Length;
int lo = 0;
int hi = n - 1;
@HDegano
HDegano / FindMinRotatedSortedArray.cs
Last active August 29, 2015 14:07
Find the minimum in a sorted and rotated Array
//Assumes there are no duplicate
//O(log n)
public int FindMinRotatedArray(int[] ar){
int n = ar.Length;
int lo = 0;
int hi = n - 1;
while(lo <= hi){
if(ar[lo] <= ar[hi]) // (sub) array is already sorted, yay!