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
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 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
/* | |
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
//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! |
NewerOlder