Skip to content

Instantly share code, notes, and snippets.

@gabhi
gabhi / gist:a21e9594330f52b3039f
Created June 10, 2014 06:18
Find all subsets of length k in an array
private static void getSubsets(List<Integer> superSet, int k, int idx, Set<Integer> current,List<Set<Integer>> solution) {
//successful stop clause
if (current.size() == k) {
solution.add(new HashSet<>(current));
return;
}
//unseccessful stop clause
if (idx == superSet.size()) return;
Integer x = superSet.get(idx);
current.add(x);
@gabhi
gabhi / gist:99914454da82938b0671
Created June 11, 2014 22:55
Majority Element
findCandidate(a[], size)
1. Initialize index and count of majority element
maj_index = 0, count = 1
2. Loop for i = 1 to size – 1
(a)If a[maj_index] == a[i]
count++
(b)Else
count--;
(c)If count == 0
maj_index = i;
@gabhi
gabhi / gist:0e95c67cb90f9a1c8d72
Created June 12, 2014 06:55
implement @patch for jersey
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.ws.rs.HttpMethod;
import javax.ws.rs.NameBinding;
@gabhi
gabhi / gist:c5bce693357d640b73be
Created June 12, 2014 19:28
Booting Spring Boot into Heroku
Booting Spring Boot into Heroku
FRIDAY, JANUARY 17, 2014
Spring Boot is a new approach from Spring to build web applications. The quote from Spring is that it "takes an opinionated view of building production-ready Spring applications. Spring Boot favors convention over configuration and is designed to get you up and running as quickly as possible." One of the things I like doing when evaluating a new framework is to ensure that I have an environment to deploy my work. So this post will show you how to get a sample Spring Boot application running on heroku.
To get started, you need a few prerequistes.
JDK 1.7
Maven 3
A Herkou account
Heroku command line tools
@gabhi
gabhi / gist:cea78255772ebf108140
Created June 12, 2014 21:55
best spring mongodb rest hypermedia example
https://spring.io/guides/gs/accessing-mongodb-data-rest/
@gabhi
gabhi / app.js
Created June 17, 2014 08:44 — forked from arvis/app.js
/**
* Module dependencies.
*/
var express = require('express')
, routes = require('./routes')
, http = require('http')
, mongoose = require('mongoose')
, path = require('path');
@gabhi
gabhi / gist:a42e12480c064735af91
Created June 17, 2014 23:33
Reset git repository
git reset --hard # removes staged and working directory changes
git clean -f -d # remove untracked files
git clean -f -x -d # CAUTION: as above but removes ignored files like config.
@gabhi
gabhi / gist:394af078d6e6345445db
Created June 18, 2014 21:34
Word break memorization +dynamic programming
Map<String, String> memoized;
String SegmentString(String input, Set<String> dict) {
if (dict.contains(input)) return input;
if (memoized.containsKey(input) {
return memoized.get(input);
}
int len = input.length();
for (int i = 1; i < len; i++) {
String prefix = input.substring(0, i);
@gabhi
gabhi / gist:22c889cff6ae1358c38f
Created June 19, 2014 05:10
MAximum sub matrix sum in array
import java.util.Arrays;
public class MaxSumInRectangle {
public int start = 0, finish = 0;
public int maxSubArraySum(int arr[]) {
// initialize sum, maxSum and start
int sum = 0, maxSum = Integer.MIN_VALUE, i;
@gabhi
gabhi / gist:fcc369e0a11292a17729
Created June 19, 2014 23:56
Maximum rectangle in matrix
public class MaximumRectangle {
public static void main(String[] args)
{
int nRows = 5, nCols = 6;
int[][] boolMatrix = {
{1,0,0,1,1,0},
{0,1,1,1,1,0},
{0,1,1,1,1,1},
{1,0,1,1,1,0},
{0,1,1,0,1,1}