Skip to content

Instantly share code, notes, and snippets.

View criskgl's full-sized avatar
👨‍💻
Coding...

criskgl

👨‍💻
Coding...
View GitHub Profile
@criskgl
criskgl / stockMaximizationI.java
Last active October 24, 2019 10:35
### AS MANY TRANSACTIONS AS WE WANT **BUT** ### MUST SELL the stock before buying again
public int stockMaximizeII(int[] prices) {
if(prices.length == 0) return 0;
int profit = 0;
int sell = prices[prices.length - 1];
for(int i = prices.length - 2; i >= 0; i--){
if(prices[i] >= sell) sell = prices[i];
else{
profit += sell - prices[i];
sell = prices[i];
}
@criskgl
criskgl / kmpSearch.java
Last active October 22, 2019 16:58
# SUBSTRINGS SEARCH THAT IMPROVES THE O(N*M) NAIVE TO O(N + M). ## It is a ***must know*** algorithm.
public static void main(String[] args) {
int result = KMP("aaabbabababaxxxaaccaca", "xxxaac");
if(result == -1) System.out.println("Pattern not found");
System.out.println("pattern found starting at index: "+result+" of txt");
}
//THIS FUNCTION ENHANCES THE NAIVE O(N*M) to O(N+M);
public static int KMP(String txt, String pat){
char[] txtArr = txt.toCharArray();
char[] patArr = pat.toCharArray();
private class myComparator implements Comparator<Interval> {
@Override
public int compare(Interval a, Interval b) {//things to compare
//comparissons here.
//1. if we want a to evaluate to left(go before) we return -1
//2. if we want a to evaluate to right(go after) we return 1
//3 .if we need to reevaluate for more ocnditions we return 0
// and reevaluate steps 1 & 2 until we can return 1 or -1
return a.start < b.start ? -1 : a.start == b.start ? 0 : 1;
}
int[] arr = list.stream().mapToInt(i->i).toArray();
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
char[] tasks = {'C','D','D','H'};
int coolingInterval = 2;
System.out.println(leastInterval(tasks, coolingInterval));
}
public static int leastInterval(char[] tasks, int n) {
int[] counter = new int[3];
@criskgl
criskgl / logsLambda.java
Last active October 6, 2019 17:47
# NUMBER AND LETTER LOGS We receive either letter logs * Letter-logs come before digit-logs; * Letter-logs are sorted alphanumerically, by content then identifier; * Digit-logs remain in the same order. > OUR LAMBDA EXPRESSION ASSUMES THAT IT WIL
public String[] reorderLogFiles(String[] logs) {
if (logs == null || logs.length == 0) return logs;
int len = logs.length;
List<String> letterList = new ArrayList<>();
List<String> digitList = new ArrayList<>();
for (String log : logs) {
if (log.split(" ")[1].charAt(0) < 'a') {
digitList.add(log);
} else {
letterList.add(log);
public int generateRandomFrom0UpTo(int upperExclusiveBound){
System.out.println("pseudo random int between 0 and upperExclusiveBound: " + randomInt);
int randomInt = (int)(upperExclusiveBound * Math.random());
return randomInt;
}
boolean isPalindrome(String s){
if(s.length() == 0) return true;
if(s.length() == 1) return true;
char[] sa = s.toCharArray();
Stack<Character> stk = new Stack<>();
for(int i = 0; i < sa.length/2; i++){
stk.add(sa[i]);
}
int rightSideStart = sa.length/2;
if(sa.length % 2 != 0) rightSideStart++;
@criskgl
criskgl / kruskalMinSpanningTree.java
Created September 2, 2019 18:04
# KRUSKAL MINIMUM SPANNING TREE * There is only one exclusive path from a node to every other node. * The subgraph is of minimum overall weight (sum of all edges) among all such subgraphs. * No cycles are formed > The strategy is to make the disjoi
public class KruskalMST {
// Finds the REPRESENTING NODE for vertex i
static int find(int i, int[] parent)
{
while (parent[i] != i)
i = parent[i];
return i;
}
// Does union of i and j.
/***ITERATIVE VERSION*****/
int binarySearch(int [] a, int x){
//set limits and midpoint.
int low = 0;
int high = a.length-1;
int mid;
while(low <= high){//too small
mid(low+high) / 2;
if(a[mid] < x){
low = mid + 1;