Skip to content

Instantly share code, notes, and snippets.

View yanhsiah's full-sized avatar

Yan-Hsiang (Allen) Huang yanhsiah

View GitHub Profile
priority_queue<string> pqZtoA;
pqZtoA.push("hello"); pqZtoA.push("word");
unordered_map<string, priority_queue<string>> mpZtoA;
mpZtoA["key"] = pqZtoA;
mpZtoA["key"].push("allen");
while (!mpZtoA["key"].empty()) {
cout << mpZtoA["key"].top() << ", ";
mpZtoA["key"].pop();
}
cout << endl;
string minWindow(string s, string t) {
vector<int> hash(128); // ASCII 0-127;
int match = 0, start = 0, end = 0;
int pos = start, len = INT_MAX; // return s.substr(pos, len);
for (char c : t) hash[c]++;
while (end < s.length()) {
// Means it's one of the char in t
if (hash[s[end]] > 0) { match++; }
@yanhsiah
yanhsiah / gist:6c2ff2732ca896a04fd770a54022ffad
Last active August 25, 2019 21:10
779. K-th Symbol in Grammar
class Solution {
public:
int kthGrammar(int N, int K) {
if (N == 1 && K == 1) return 0;
if (K % 2) {
return kthGrammar(N - 1, (K + 1) / 2) ? 1 : 0;
} else {
return kthGrammar(N - 1, (K + 1) / 2) ? 0 : 1;
}
@yanhsiah
yanhsiah / ConcurrentLoop.java
Last active April 27, 2019 07:05
Use Vert.x worker threads to run a for-loop.
import io.vertx.core.*;
public class ConcurrentLoop {
private static String runTask(int idx) {
System.out.println("run task " + idx + " on " + Thread.currentThread().getName());
long sum = 0;
for (long j = 0; j < Long.MAX_VALUE / 1000000000; j++) {
sum += 1;
@yanhsiah
yanhsiah / tweet.json
Created March 31, 2019 08:26
sample tweet
{
"created_at": "Fri Apr 04 05:40:26 +0000 2014",
"id": 451957445636534300,
"id_str": "451957445636534272",
"text": "RT @anneotago: @jendab @chelsea_goulton @LogiBeer88 hoping for a good result this week in #gigatowndun :)",
"source": "<a href=\"http://twitter.com/download/iphone\" rel=\"nofollow\">Twitter for iPhone</a>",
"truncated": false,
"in_reply_to_status_id": null,
"in_reply_to_status_id_str": null,
"in_reply_to_user_id": null,
@yanhsiah
yanhsiah / run.sh
Last active March 30, 2019 07:57
etl-instance setup script
#!/bin/bash
wget https://gist.githubusercontent.com/yanhsiah/bd1c59d06f9daf149480182f85d117dd/raw/058cdd4c3511bbd83b1b2368edddc152f2c15c79/.tmux.conf;
sudo apt update; sudo apt install -y default-jre default-jdk openjdk-8-jdk maven;
sudo add-apt-repository ppa:webupd8team/java; sudo apt-get install -y oracle-java8-installer oracle-java8-set-default;
echo "JAVA_HOME=/usr/lib/jvm/java-8-oracle" >> ~/.bash_profile;
echo "JRE_HOME=/usr/lib/jvm/java-8-oracle/jre" >> ~/.bash_profile;
. ~/.bash_profile;
gcloud init; gcloud auth application-default login;
git clone https://github.com/GoogleCloudPlatform/cloud-bigtable-examples.git;
git clone https://github.com/CloudComputingTeamProject/CaptainMAL-S19.git;
@yanhsiah
yanhsiah / .tmux.conf
Last active March 19, 2019 09:48
.tmux.conf
unbind C-b
set -g prefix ^A
bind a send-prefix
# set colors
set-window-option -g window-status-current-fg blue
set-window-option -g window-status-current-bg yellow
set-window-option -g window-status-current-attr default
# switch to next window
vector<string> rearrange(vector<int>& baskets);
// [ 2 , 1 , 4 , 5 , 3 , empty_basket ]
vector<int> baskets = {2,1,4,5,3,0};
vector<string> commands = rearrange(baskets);
for (string command : commands) {
cout << command << endl;
}
@yanhsiah
yanhsiah / Lock
Last active April 16, 2018 09:11
Lock
https://gist.github.com/alexklibisz/7cffdfe90c97986f8393
https://www.ibm.com/developerworks/cn/linux/thread/posix_thread2/index.html
https://github.com/trenskow/TrReadWriteLock
@yanhsiah
yanhsiah / collectionview update
Created February 20, 2018 06:36
collectionview update
- (NSArray<NSIndexPath *> *)indexPathsFromItems:(NSArray *)items section:(NSUInteger)section
{
NSMutableArray *indexPaths = [NSMutableArray arrayWithCapacity:items.count];
[items enumerateObjectsUsingBlock:^(id tag, NSUInteger idx, BOOL *stop) {
[indexPaths addObject:[NSIndexPath indexPathForItem:idx inSection:section]];
}];
return indexPaths;
}
[self.collectionView performBatchUpdates:^{