Skip to content

Instantly share code, notes, and snippets.

View madhur's full-sized avatar
🎯
Focusing

Madhur Ahuja madhur

🎯
Focusing
View GitHub Profile
@madhur
madhur / makechange2.java
Created July 27, 2019 14:20
make change2
public static int makeChange(int[] coins, int amount) {
if (coins != null && coins.length > 0 && amount >= 0)
return makeChange(coins,amount,0);
return 0;
}
public static int makeChange(int[] coins, int amount, int current_coin_index) {
int next_coin_index;
if (current_coin_index < coins.length - 1){
//If the coin index is less than the last index, increment the index.
@madhur
madhur / makechange.java
Created July 27, 2019 14:14
Make change
// java.util.* and java.util.streams.* have been imported for this problem.
// You don't need any other imports.
public static int makeChange(int[] coins, int amount) {
int counter = 0;
int currentCoinIndex = 0;
Integer[] answer = new Integer[1];
answer[0] = 0;
@madhur
madhur / ipmatching.java
Last active July 13, 2019 17:10
IP Matching
// java.util.* and java.util.streams.* have been imported for this problem.
// You don't need any other imports.
public static ArrayList<String> generateIPAddrs(String s) {
Stack<IpLevelNode> st = new Stack<>();
ArrayList<String> answer = new ArrayList<>();
Character c = s.charAt(0);
Integer numValue = Character.getNumericValue(c);
@madhur
madhur / mergeintervals.java
Last active June 9, 2019 15:52
Merge intervals
public static ArrayList<Interval> insertRange(ArrayList<Interval> intervalsList, Interval insert) {
ArrayList<Interval> out = new ArrayList<>();
for(Interval i: intervalsList){
if(i.end < insert.start) {
out.add(i);
}
else if(i.start > insert.end) {
out.add(insert);
insert = i;
}
@madhur
madhur / maximummatrixsumdfs.java
Created June 5, 2019 05:46
maximum matrix sum dfs
// java.util.* and java.util.streams.* have been imported for this problem.
// You don't need any other imports.
public static int matrixMaxSumDfs(int[][] grid) {
int[] sum = new int[1];
int answer = helper(grid, 0, 0, sum);
return answer;
}
@madhur
madhur / deletebstnode.java
Created May 29, 2019 18:28
Delete node from bst
public TreeNode delete(TreeNode root, int data) {
if(root == null) {
return null;
} else if(data < root.data) {
root.left = delete(root.left, data);
} else if(data > root.data) {
root.right = delete(root.right, data);
} else { //element found
if(root.left != null && root.right != null) { //full node case
root.data = findMin(root.right).data;
@madhur
madhur / longestnrsubstring.java
Created May 27, 2019 17:34
Longest non repeating substring
public static int longestNRSubstringLen(String input) {
if(input==null)
return 0;
char[] array = input.toCharArray();
int prev = 0;
HashMap<Character, Integer> characterMap = new HashMap<Character, Integer>();
for (int i = 0; i < array.length; i++) {
if (!characterMap.containsKey(array[i])) {
@madhur
madhur / addlinkedlist.java
Created May 26, 2019 18:04
Add two linkedlist
public static ListNode sumTwoLinkedLists(ListNode input1, ListNode input2) {
int carryComponent =0;
ListNode returnHead = new ListNode(0);
ListNode n1 = input1, n2 = input2, n3=returnHead;
while(n1 != null || n2 != null){
if(n1 != null){
carryComponent += n1.data;
n1 = n1.next;
@madhur
madhur / mindepthtree.java
Created May 26, 2019 08:10
Minmum depth of a tree
// java.util.* and java.util.streams.* have been imported for this problem.
// You don't need any other imports.
public int minTreeDepth(TreeNode root) {
if (root == null) {
return 0;
}
Queue<TreeNode> currQ = new LinkedList<TreeNode>();
@madhur
madhur / reverselist.java
Created May 25, 2019 20:18
Reverse linked list
public ListNode reverseList(ListNode head) {
// Add your code below this line. Do not modify any other code.
ListNode currentNode = head;
ListNode previousNode= null;
while(currentNode != null)
{
ListNode nextNode = currentNode.next;
currentNode.next = previousNode;