Skip to content

Instantly share code, notes, and snippets.

View ducalpha's full-sized avatar

Duc Bui ducalpha

View GitHub Profile
@ducalpha
ducalpha / LogErrno.cc
Created April 15, 2016 07:51
Logging errors
void LogError(const char *fmt, ...) {
va_list vlist;
va_start(vlist, fmt);
const size_t kLogMessageLength = 128;
char log_message[kLogMessageLength];
vsnprintf(log_message, kLogMessageLength, fmt, vlist);
LogError(log_message);
va_end(vlist);
@ducalpha
ducalpha / verify_preorder_serialization.cpp
Created March 23, 2016 15:20
Verify Preorder Serialization of a Binary Tree
// https://leetcode.com/problems/verify-preorder-serialization-of-a-binary-tree/
class Solution {
public:
bool isValidSerialization(string preorder) {
if (preorder.empty())
return true;
int i = preorder.length() - 1;
int stack_size = 0;
while (i >= 0) {
@ducalpha
ducalpha / triplet_increasing_subarray.cpp
Created March 22, 2016 18:50
Check whether or not an array contains an increasing triplet
// https://leetcode.com/problems/increasing-triplet-subsequence/
class Solution {
public:
bool increasingTriplet(vector<int>& nums) {
if (nums.size() < 3)
return false;
int l = 0, r = nums.size() - 1, u = 0;
// increase l as much as possible
while (l < nums.size() - 1 && nums[l] >= nums[l + 1])
++l;
@ducalpha
ducalpha / largest_bst.cpp
Created February 26, 2016 11:52
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest means subtree with largest number of nodes in it.
/**
* https://leetcode.com/problems/largest-bst-subtree/
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
@ducalpha
ducalpha / test_css_position.html
Created February 21, 2016 07:23
test css position
<!DOCTYPE html>
<head>
<style>
#container{
border: 1px solid black;
margin: 40px auto;
width: 200px;
}
#div1 {
@ducalpha
ducalpha / iframe_a_target.html
Created February 21, 2016 02:36
iframe and a tag target example
<html><head>
</head>
<body>
<iframe src="http://www.w3schools.com/" name="A">&lt;p&gt; This my first iframe&lt;/p&gt;</iframe>
<iframe src="http://cps.kaist.ac.kr/" name="B">&lt;p&gt; This my second iframe&lt;/p&gt;</iframe>
<iframe src="http://www.example.com/" name="C">&lt;p&gt; This my third iframe&lt;/p&gt;</iframe>
<a href="http://www.kaist.ac.kr" target="B">Change B</a>
@ducalpha
ducalpha / convert_min_first_to_max_first.cpp
Created January 29, 2016 12:04
Convert a min-first BST to a max-first BST
#include <my_epi/common.h>
template <typename T>
struct BTNode {
T data;
shared_ptr<BTNode<T>> left, right;
};
typedef BTNode<int> NodeType;
int FindMinTimePeriod(const vector<int>& tasks, int K) {
unordered_map<int, int> task_to_last_time;
int cur_time = 0;
for (int i = 0; i < tasks.size(); ++i) {
int cur_task = tasks[i];
auto it = task_to_last_time.find(cur_task);
if (it != task_to_last_time.end()) {
cur_time = max(cur_time, it->second + K + 1);
it->second = cur_time;
} else {
@ducalpha
ducalpha / is_valid_schedule.cpp
Created January 4, 2016 00:27
Check valid schedule
struct Meeting {
int start, end;
};
struct CompareMeeting {
bool operator()(const Meeting& a, const Meeting& b) const {
if (a.start == b.start) return a.end < b.end;
return a.start < b.start;
}
};
bool IsValidSchedule(vector<Meeting> meetings) {
@ducalpha
ducalpha / find_longest_subarray_closest_to_k.cpp
Created December 18, 2015 14:36
find longest subarray closest/equal to k
pair<int, int> FindSubarrayClosestToK(const vector<int>& a, int k) {
pair<int, int> range{0, -1};
if (a.empty()) return range;
map<int, int> sum_to_idx;
int sum = 0;
int min_diff = k;
sum_to_idx.emplace(0, -1); // S[-1] = 0 so push 0, -1 here!
for (int i = 0; i < a.size(); ++i) {