This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Assumes no duplicate | |
O(logN) | |
*/ | |
public int FindElementInRotatedSortedArray(int[] ar, int k){ | |
int n = ar.Length; | |
int lo = 0; | |
int hi = n - 1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution{ | |
public ListNode ReverseLinkList(ListNode head){ | |
if(head == null) return null; | |
ListNode current = head; | |
ListNode prev = null; | |
ListNode reverseHead = null; | |
OlderNewer