This file contains 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
const chunkSize = 3; | |
const numberOfTasks = 10; | |
let tasksSleepInput = Array(numberOfTasks).fill(30).map(v => ({delay: v, index: '' })); | |
// add indices | |
Object.keys(tasksSleepInput).forEach(k => tasksSleepInput[Number.parseInt(k)].index = k); |
This file contains 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 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 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 final Lock lock = new ReentrantLock(isFair); | |
// usage: | |
try{ | |
lock.lock(); | |
// Do some stuff | |
} finally{ | |
lock.unlock(); | |
} |
This file contains 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 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 RunnableJob implements Runnable{ | |
@Override | |
public void run() { | |
} | |
} | |
public void runJobs(){ | |
CompletableFuture<Void> f = CompletableFuture.runAsync(new RunnableJob()); | |
f.thenRun(new RunnableJob()); |
This file contains 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 FilterLogin implements Filter { | |
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain){ | |
// inject cookie | |
Cookie c = getCookie(httpServletResponse, "WASReqURL"); | |
if(c == null){ | |
// inject the cookie if it is not there | |
c = new Cookie("WASReqURL", ""); | |
httpServletResponse.addCookie(c); | |
} | |
String newUrl = "/my_action_servlet"; |
This file contains 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
Uri builtUri = Uri.parse(BASE_URL).buildUpon() | |
// add various params | |
.appendQueryParameter(PARAM_QUERY, githubSearchQuery) | |
.appendQueryParameter(PARAM_SORT, sortBy) | |
// build the uri | |
.build(); | |
// Convert the Uri to a URL: | |
URL url = null; | |
try { |
This file contains 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 MainActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
// other code to setup the activity | |
} | |
// other code | |
} |