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 / KnapSack.cs
Created June 18, 2015 01:18
Knapsack 0/1 and Unbounded
public static partial class DynamicProgramming
{
public static int KnapSack_01(Tuple<int, int>[] valueWeightTuple, int maxWeight)
{
int[,] dp = new int[valueWeightTuple.Length + 1, maxWeight + 1];
for (int i = 0; i <= maxWeight; ++i) dp[0, i] = 0;
for (int i = 1; i <= valueWeightTuple.Length; i++)
{
@HDegano
HDegano / RotateArray.java
Last active August 29, 2015 14:23
Rotate Array with O(n) complexity and O(1) space
public class RotateArray {
public void rotate(int[] nums, int k) {
if(k > nums.length) k = k % nums.length;
reverse(nums, 0, nums.length);
reverse(nums, 0, k);
reverse(nums, k, nums.length);
}
@HDegano
HDegano / StringToInt.java
Last active August 29, 2015 14:22
string to int
//From OJ handbook
public class StringToint{
public int atio(String str){
int i = 0;
int n = str.length();
while(i < n && Character.isWhiteSpace(str.charAt(i))) i++;
int sign = 1;
@HDegano
HDegano / PrintSpiralMatrix.java
Created May 29, 2015 04:39
Print a matrix on spiral order. Clockwise
public class PrintSpiralMatrix {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> elements = new ArrayList<>();
if (matrix.length == 0) return elements;
int m = matrix.length; //rows
int n = matrix[0].length; //col
@HDegano
HDegano / MergeIntervals.java
Last active August 29, 2015 14:22
Merge Intervals
public class MergeIntervals {
public List<Interval> merge(List<Interval> intervals) {
List<Interval> mergedIntervals = new ArrayList<>();
if(intervals == null || intervals.size() == 0)
return mergedIntervals;
int n = intervals.size();
@HDegano
HDegano / Pow.java
Created May 27, 2015 16:51
Implement pow(a, b) logN
public class Pow {
public double myPow(double x, int n) {
boolean isNegative = n < 0;
if(isNegative)
n = n * -1;
double result = innerPow(x, n);
@HDegano
HDegano / Search2DMatrix.java
Created May 27, 2015 15:19
Search 2D Matrix where row and columns are sorted. Last item in row is less than the first item for the next row
public class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int rows = matrix.length;
if(rows <= 0) return false;
int cols = matrix[0].length;
if(cols <= 0) return false;
@HDegano
HDegano / KthLargest.java
Last active August 29, 2015 14:21
Kth Largest Element in unsorted Array.
public class KthLargest{
public int findKthLargestPQ(int[] nums, int k) {
PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
int n = nums.length;
for(int i = 0; i < n; ++i){
@HDegano
HDegano / FindKInSortedMatrix.java
Created May 24, 2015 00:19
Find item in sorted matrix where last number of each row is not greater than the first item of the next row.
public class Search2DMatrix{
public boolean searchMatrix(int[][] matrix, int target) {
int rows = matrix.length;
if(rows <= 0) return false;
int cols = matrix[0].length;
@HDegano
HDegano / SymmetricTree.java
Created May 10, 2015 17:32
Check if Tree is symmetrical
public class SymmetricTree{
public boolean isSymmetric(TreeNode root) {
return isSymmetric(root, root);
}
private boolean isSymmetric(TreeNode p, TreeNode q){
if(p == null && q == null) return true;