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
Predicate<Person> isAnAdult = person -> person.getAge() >= 18; | |
List<Person> people = getAllPeople(); | |
Integer nrOfAdults = people.stream() .filter(isAnAdult).count(); |
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
CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> "Hello"); | |
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> "Beautiful"); | |
CompletableFuture<String> future3 = CompletableFuture.supplyAsync(() -> "World"); | |
CompletableFuture<Void> combinedFuture = CompletableFuture.allOf(future1, future2, future3); | |
combinedFuture.get(); | |
assertTrue(future1.isDone()); | |
assertTrue(future2.isDone()); | |
assertTrue(future3.isDone()); | |
// the result of each one can be processed as follows: |
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 add_no_arithm(int a, int b) { | |
if (b == 0) return a; | |
int sum = a ^ b; // add without carrying | |
int carry = (a & b) << 1; // carry, but don’t add | |
return add_no_arithm(sum, carry); // recurse | |
} |
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
function lock(boolean *lock) { | |
while (test_and_set(lock) == 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
/* Dynamic programming solution: | |
1. Let dp[i] where i is between [0..amount+1] represents the minimum amount of coins needed for i. | |
2. dynamic programming solution gives: | |
for each coin in coins and foreach amount i: dp[i] = min(dp[i], dp[i - coins[j]] + 1); | |
*/ | |
int coinChange(vector<int>& coins, int amount) { | |
int Max = amount + 1; | |
vector<int> dp(amount + 1, Max); | |
dp[0] = 0; | |
for (int i = 1; i <= amount; 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 boolean isCycleUtil(int vertex, boolean[] visited, boolean[] recursiveArr) { | |
visited[vertex] = true; | |
recursiveArr[vertex] = true; | |
//recursive call to all the adjacent vertices | |
for (int i = 0; i < adjList[vertex].size(); i++) { | |
//if not already visited | |
int adjVertex = adjList[vertex].get(i); | |
if (!visited[adjVertex] && isCycleUtil(adjVertex, visited, recursiveArr)) { | |
return true; |
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 findUnsortedSubarray(int[] A) { | |
int n = A.length, beg = -1, end = -2, min = A[n-1], max = A[0]; | |
for (int i=1;i<n;i++) { | |
max = Math.max(max, A[i]); | |
min = Math.min(min, A[n-1-i]); | |
if (A[i] < max) end = i; | |
if (A[n-1-i] > min) beg = n-1-i; | |
} | |
return end - beg + 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
public int lengthOfLongestSubstring(String s) { | |
HashMap<Character,Integer> set = new HashMap<>(); | |
int max = 0;int j=0; | |
for(int i=0;i<s.length();i++){ | |
char c = s.charAt(i); | |
if(set.containsKey(c)){ | |
// as we dont delete history, we dont | |
// want to jump back to index in a substring that is dead | |
j = Math.max(j, set.get(c)+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
.gradle | |
/local.properties | |
/.idea/workspace.xml | |
/.idea/libraries | |
.DS_Store | |
/build |
NewerOlder