Skip to content

Instantly share code, notes, and snippets.

@sdpatil
sdpatil / GenerateDaysBetween2Dates.scala
Created July 8, 2018 19:35
Generate days between 2 dates in scala
import java.time.LocalDate
import java.time.temporal.ChronoUnit
def generateDaysBetween(startDateStr: String, endDateStr:String)={
val start = LocalDate.parse(startDateStr)
val end = LocalDate.parse(endDateStr)
val between = ChronoUnit.DAYS.between(start,end).toInt
for(daysPlus <- 0 to between)
@sdpatil
sdpatil / SetZeros.java
Created August 22, 2017 00:38
LeetCode 73. Set Matrix Zeroes
/**
* Problem :- Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column is set to 0.
*/
public class SetZeros {
/*
Solution :- First create a boolean matrix for number of rows and another for number of columns
Then iterate through the matrix if you find zero mark that row and column to zero in boolean matrix
At the end of first iteration we have marked all the rows or columns that should be marked to zero.
Now go throug the matrix one more time checking if either the current row or column is marked zero in boolean
@sdpatil
sdpatil / ArrayPartition1.java
Created August 22, 2017 00:36
LeetCode 561: Array Partition I
import java.util.Arrays;
/*
Problem: Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), ..., (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large as possible.
Example 1:
Input: [1,4,3,2]
Output: 4
Explanation: n is 2, and the maximum sum of pairs is 4 = min(1, 2) + min(3, 4).
*/
@sdpatil
sdpatil / SortArrayWithRepeatedEntries.java
Created August 14, 2017 15:58
Partitioning and sorting array with many repeated entries
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Problem: You are given an array of student objects. Each student has an integer-valued age field that is
* treated as a key. Rearrange the elements of the array so that students of equal age appear together.
* The order in which different ages appear is not important
*/
@sdpatil
sdpatil / FindMinMaxSimul.java
Created August 14, 2017 15:43
Find the min and max simultaneously
/**
* Problem is how do you find out minimum and maximum element in unsorted array without 2(n-1) comparison's
* Ex. {3,2,5,1,2,4} should return 1 min and 5 max
*/
public class FindMinMaxSimul {
public static class MinMax {
public Integer min;
public Integer max;
public MinMax(Integer min, Integer max) {
@sdpatil
sdpatil / MatrixSearch
Created August 14, 2017 15:41
Search in a 2D sorted array
/**
* Problem: Search a number in 2D matrix
*/
public class MatrixSearch {
/*
Solution: - Start by comparing with last column in first row, if the value matches return it
if not if target is smaller than current column go to next row if target is more than current
value go one column inward
*/
public boolean matrixSearch(int[][] A, int k) {
@sdpatil
sdpatil / SquareRootCalculator.java
Created August 14, 2017 15:39
Compute the integer square root
/**
* Problem is given a integer find largest integer whose square is less than or equal to k
* Ex. Given 16 return 4 or given 15 return 3
*
* Solution :- Basic idea is if square of a number x is less than k then no number less than x would
* be the answer same way if square of x is more than k then you can ignore all numbers more than x.
* You can use binary search to solve this problem
*/
public class SquareRootCalculator {
@sdpatil
sdpatil / LevenshteinDistance.java
Created August 14, 2017 00:37
Compute the Levenshtein distance
import java.util.Arrays;
/**
* Problem: Write a program that takes two strings and computes the minimum number of edits needed to transform the
* first string into the second string
*/
public class LevenshteinDistance {
/*
First create 2 dimensional array with int[A.length()+1][B.length()+1].
@sdpatil
sdpatil / FindLongestNonDecreasingSubsequence.java
Created August 14, 2017 00:24
Find the longest nondecreasing subsequence
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
/**
* Problem: Write a program that takes as input an array of numbers and returns the length of a longest nondecreasing/increasing
* subsequence in the array
*/
public class FindLongestNonDecreasingSubsequence {
@sdpatil
sdpatil / RemoveDuplicatesII80.java
Created August 13, 2017 23:48
Leetcode 80 Remove Duplicates from Sorted Array I
/*
Problem: Given an array of number remove {1,1,1,2,2,3} 3rd duplicate.
Ex. output in this case is {1,1,2,2,3,0}
Solution:- Basic idea is same as that of the RemoveDuplicate, you maintain write
index and start iterating through the array, whenver current element is more than
2nd last element in the new array then copy it to new array
*/
public class RemoveDuplicatesII80 {