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 / 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!
@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 / 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 / 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 / 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 / MergeTwoList.cs
Last active August 29, 2015 14:19
Merge Two Sorted List
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode MergeTwoLists(ListNode l1, ListNode l2) {
@HDegano
HDegano / sortedArrayToBalancedBST.cs
Last active August 29, 2015 14:19
Sorted Array to Balanced BST
/**
* Definition for binary tree
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
@HDegano
HDegano / MinEditDistance.cs
Last active August 29, 2015 14:19
Given two strings, return the min edit distance
/*
Requires O(mn) space
*/
public class Solution {
public int MinDistance(string word1, string word2) {
int[,] dp = new int[word2.Length + 1, word1.Length + 1];
for (int i = 0; i <= word1.Length; i++) dp[0, i] = i;
for (int i = 0; i <= word2.Length; i++) dp[i, 0] = i;
@HDegano
HDegano / FindKthNodeFromEnd.cs
Last active August 29, 2015 14:20
Find the Kth to The Last Node of a Singly Link List
public class Solution{
public ListNode FindKthNode(ListNode head, int n){
if(head == null || n <= 0) return null;
ListNode fast = head;
//Verify what is Kth to the last
// 1 -> 2 -> 3 and k = 1, if K = 3 or K = 2. For now assume the former is correct
@HDegano
HDegano / ReverseSLL.cs
Last active August 29, 2015 14:20
Reverse a Link List
public class Solution{
public ListNode ReverseLinkList(ListNode head){
if(head == null) return null;
ListNode current = head;
ListNode prev = null;
ListNode reverseHead = null;