Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

Lu Dang eclipselu

🏠
Working from home
  • Kitchener, Ontario
View GitHub Profile
@eclipselu
eclipselu / largetst_rect_in_hist.cpp
Created Sep 25, 2016
Largest Rectangle in Histogram
View largetst_rect_in_hist.cpp
class Solution {
public:
/**
* @param height: A list of integer
* @return: The area of largest rectangle in the histogram
*/
int largestRectangleArea(vector<int> &height) {
stack<int> heightSt;
stack<int> indexSt; // start index
View lru_cache.cpp
class Node {
public:
int key;
int val;
Node *prev;
Node *next;
Node (int key, int val, Node *prev = NULL, Node *next = NULL) {
this->key = key;
this->val = val;
View evaluate_division.cpp
class ValType {
public:
double val;
string sym;
ValType () {
this->val = 1.0;
this->sym = "";
}
View random_pick_index.cpp
class Solution {
private:
vector<int> nums;
public:
Solution(vector<int> nums) {
this->nums = nums;
srand(time(NULL));
}
int pick(int target) {
View integer_replacement.cpp
class Solution {
public:
int integerReplacement(int n) {
return helper(n);
}
private:
int helper(long n) {
if (n == 1)
return 0;
View rotate_function.cpp
class Solution {
public:
int maxRotateFunction(vector<int>& A) {
int len = A.size();
int sum = 0;
int psum = 0;
// sum
for (int i = 0; i < len; ++i) {
sum += A[i];
@eclipselu
eclipselu / longest_substring.cpp
Created Sep 9, 2016
Longest Substring with At Least K Repeating Characters
View longest_substring.cpp
class Solution {
public:
int longestSubstring(string s, int k) {
vector<int> count = getCharCount(s);
for (int i = 0; i < count.size(); ++i) {
char ch = i + 'a';
if (count[i] > 0 && count[i] < k) {
int max_len = INT_MIN;
vector<string> splits = splitString(s, ch);
View decode.cpp
class Solution {
public:
string decodeString(string s) {
int len = s.length();
if (len == 0)
return s;
stack<int> num_st;
stack<string> sym_st;
View valid_utf8.cpp
class Solution {
public:
bool validUtf8(vector<int>& data) {
return isValid(data, 0);
}
private:
bool isValid(vector<int> &data, int startIndex) {
if (startIndex == data.size())
return true;
View subsequence.cpp
class Solution {
public:
bool isSubsequence(string s, string t) {
queue<char> q;
for (char ch: s)
q.push(ch);
for (char ch: t) {
if (q.empty())
break;