Skip to content

Instantly share code, notes, and snippets.

Avatar

Damola Kolade deedee47

View GitHub Profile
@deedee47
deedee47 / checkCyclicPath.java
Created Jun 5, 2021
Detects if there is at least a cyclic path in all available routes
View checkCyclicPath.java
public boolean isThereCycle(Map<Character, List<Character>> map){
if(map == null || map.isEmpty()) return false;
//use each node as starting point
for(char item : map.keySet()){
//create new tracking experience for each start point
Stack<Character> trackPad = new Stack<>();
List<Character> visitedPoints = new ArrayList<>();
visitedPoints.add(item);
@deedee47
deedee47 / playNumberLetterCombination.java
Created May 30, 2021
Returns a list of combined words given a sequence of numbers and a Number-Letter Mapping
View playNumberLetterCombination.java
//Keypad Number to Letter Mapping
//2-9 corresponds to letters on the phone keypad and the same index of the array
private static String[] keyPadMapping = {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv" ,"wxyz"};
public static List<String> playNumberLetterCombination(String number){
List<String> combinedWords = new ArrayList<>();
//empty or wrong input
if(number.isEmpty() || !number.matches("\\d+")) return combinedWords;
@deedee47
deedee47 / getMinTimeDifference.java
Last active May 28, 2021
Returns minimum time difference from a given list of HH:mm time entries
View getMinTimeDifference.java
public long getMinDifferenceInMinutes(String[] journal){
if(journal == null) return 0;
if(journal.length == 0) return 0;
//Using a set to store unique and valid entries
//if any entry is duplicated, it will auto break because that will be the minimum time difference
//TreeSet stores data in natural order - O(Log N) time complexity - better than sorting array with NLogN
//sorting keeps times close together and will eliminate comparing each entry with all other elements
//Tradeoff extra space for faster sort process
@deedee47
deedee47 / shuffleLine.java
Last active May 21, 2021
Move elements in an array to the front or back depending on the set direction
View shuffleLine.java
//-1 move student from front of line to back
//+1 move student from back of line to front
//0 no movement
public int[] shuffleLine(int[] line, int numberToShuffle){
if(line == null) return new int[0];
int lineLength = line.length;
if(numberToShuffle == 0 || lineLength == 0 || Math.abs(numberToShuffle) == lineLength) return line;
if(Math.abs(numberToShuffle) > lineLength)
@deedee47
deedee47 / mergeSortedArrays.java
Last active May 13, 2021
Merge sorted arrays into one
View mergeSortedArrays.java
public int[] mergeSortedArray(int[] classA, int[] classB){
if(classA == null && classB == null) return new int[0];
else if (classA == null) return classB;
else if (classB == null) return classA;
int aIndex = 0, bIndex = 0, mergeIndex = 0, classLength = classA.length+classB.length;
int[] merged = new int[classLength];
while(mergeIndex < classLength){
if(aIndex<classA.length && bIndex<classB.length){
@deedee47
deedee47 / getProductArray.java
Last active May 2, 2021
Return a new array consisting of the product of all elements in the array excluding the current index
View getProductArray.java
public int[] getProducts(int[] nums){
if(nums == null) return new int[0];
if(nums.length == 0) return new int[0];
if(nums.length == 1) return new int[]{0};
int[] products = new int[nums.length];
//any number multiplied by 1 is the same number; therefore allProducts cannot be initialized to 0
@deedee47
deedee47 / boundaryIndexesOfVal.java
Last active Apr 27, 2021
Return an array of indexes to indicate the first and last occurrence of a value in an unsorted array
View boundaryIndexesOfVal.java
public int[] getBoundaryIndexesOfVal(int[] nums, int val){
if(nums == null) return new int[] {-1, -1};
if (nums.length == 0) return new int[] {-1, -1};
int startIndex = 0;
int endIndex = nums.length - 1;
//keep count of elements before and after the specified value
int preValCount = 0;
@deedee47
deedee47 / countOfElementsExcludingVal.java
Last active Apr 27, 2021
Returns new count of elements in a Numbers array excluding occurrences of a specified value
View countOfElementsExcludingVal.java
public int countOfElementsExcludingVal(int[] nums, int val){
if(nums == null) return 0;
int countWithoutVal = nums.length;
for(int item : nums){
if (item == val) countWithoutVal--;
}
return countWithoutVal;
@deedee47
deedee47 / UniqueCountOfElements.java
Last active Apr 12, 2021
Returns the Count of Distinct Elements from an Sorted Array of Numbers,
View UniqueCountOfElements.java
public int uniqueCountOfElements(int[] nums)
{
if (nums == null) return 0;
int arrSize = nums.length;
if (arrSize == 0) return 0;
int finalCount = 1; // Count the first element by default
for(int count = 0; count < arrSize; count++)
{