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
| 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]; | |
| } |
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
| 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(); |
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
| 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; | |
| } |
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
| int[] arr = list.stream().mapToInt(i->i).toArray(); |
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
| 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]; |
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
| 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); |
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
| public int generateRandomFrom0UpTo(int upperExclusiveBound){ | |
| System.out.println("pseudo random int between 0 and upperExclusiveBound: " + randomInt); | |
| int randomInt = (int)(upperExclusiveBound * Math.random()); | |
| return randomInt; | |
| } |
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
| 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++; |
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
| 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. |
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
| /***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; |