This file contains hidden or 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
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) |
This file contains hidden or 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
/** | |
* 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 |
This file contains hidden or 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
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). | |
*/ |
This file contains hidden or 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
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 | |
*/ |
This file contains hidden or 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
/** | |
* 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) { |
This file contains hidden or 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
/** | |
* 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) { |
This file contains hidden or 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
/** | |
* 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 { | |
This file contains hidden or 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
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]. |
This file contains hidden or 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
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 { |
This file contains hidden or 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
/* | |
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 { | |
NewerOlder