Skip to content

Instantly share code, notes, and snippets.

View exhesham's full-sized avatar
💭
the grass is greener over here, you're the fog that makes it so clear

Hesham Yassin exhesham

💭
the grass is greener over here, you're the fog that makes it so clear
View GitHub Profile
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);
@exhesham
exhesham / coinChange.cpp
Created March 23, 2018 18:13
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
/* 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++) {
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;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// COMPLETED (9) Within onCreateOptionsMenu, use getMenuInflater().inflate to inflate the menu
getMenuInflater().inflate(R.menu.main, menu);
// COMPLETED (10) Return true to display your menu
return true;
}
private final Lock lock = new ReentrantLock(isFair);
// usage:
try{
lock.lock();
// Do some stuff
} finally{
lock.unlock();
}
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:
public class RunnableJob implements Runnable{
@Override
public void run() {
}
}
public void runJobs(){
CompletableFuture<Void> f = CompletableFuture.runAsync(new RunnableJob());
f.thenRun(new RunnableJob());
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";
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 {
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