Skip to content

Instantly share code, notes, and snippets.

View sundeepblue's full-sized avatar
🎯
Focusing

Protoss In Matrix sundeepblue

🎯
Focusing
View GitHub Profile
@sundeepblue
sundeepblue / arrange
Last active August 29, 2015 13:55
arrange array to form a smallest number
bool comp(string s1, string s2) {
if((s1+s2).compare(s2+s1) < 0) return true;
return false;
}
// cannot handle the case when exist 0
string get_smallest_number(vector<int> &nums) {
if(nums.empty()) return "";
string res = "";
vector<string> strs;
@sundeepblue
sundeepblue / get_min_k_numbers.cpp
Last active August 29, 2015 13:55
get minimum k numbers in array
// key idea: use max heap not min heap!!!! And the max heap is declared using less<int> for priority_queue
// maybe confusing
// time: build heap O(k), scan the rest n-k numbers takes O((n-k)logk),
// so, overall, time is O(nlogk), space is k.
// idea 2: use quick selection algorithm
vector<int> get_min_k_numbers(vector<int> &A, int k) {
if(k >= A.size()) return A;
vector<int> res;
@sundeepblue
sundeepblue / place_odd_before_even
Created January 30, 2014 06:44
place odd number before even number in array
void place_odd_before_even(int A[], int n) {
if(n <= 1) return;
int left = 0, right = n-1;
while(left < right) {
while(left < right && A[left]%2 != 0) left++;
while(left < right && A[right]%2 == 0) right--;
if(left < right)
swap(A[left++], A[right--]);
}
}
@sundeepblue
sundeepblue / delete_node.cpp
Last active August 29, 2015 13:55
delete a node in a linked list in O(1)
void delete_node(TreeNode **head, TreeNode *p) {
if(*head == NULL || p == NULL) return;
if(*head == p) {
delete p;
p = NULL; // necessary
*head = NULL;
return;
}
if(p->next != NULL) {
TreeNode *nxt = p->next;
@sundeepblue
sundeepblue / get_last_kth_node.cpp
Last active August 29, 2015 13:55
get last kth node in list
// key idea: first, go k-1 step.
ListNode* get_last_kth_node(ListNode *head, int k) {
if(!head) return NULL;
if(k == 0) return NULL;
ListNode *p = head;
// run k-1 steps
for(int i=0; i<k-1; i++) {
if(p->next == NULL) return NULL;
p = p->next;
@sundeepblue
sundeepblue / inorder_traversal.cpp
Last active August 29, 2015 13:55
[tree] inorder traversal, concise iterative version
void in_order(TreeNode *root) {
if(!root) return;
stack<TreeNode*> s;
TreeNode *curr = root;
//s.push(curr);
while(true) {
if(curr) {
s.push(curr);
curr = curr->left;
}
@sundeepblue
sundeepblue / count_words.cpp
Last active August 29, 2015 13:55
count the words in a string
// ====================== a good concise version ===================== 4/30/2014 ==
// ================================================================================
bool is_letter(char c) { return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z'; }
int count_words(const string& s) {
int i = 0, N = s.size(), count = 0;
while(i < N) {
while(i < N && !is_letter(s[i])) i++;
if(i == N) break;
@sundeepblue
sundeepblue / replace pattern.cpp
Last active August 29, 2015 13:55
In a string replace all occurrence of the given pattern to 'X'
// http://learn.hackerearth.com/question/168/in-a-string-replace-all-occurrence-of-the-given-pattern-to-x/
/*
In a string replace all occurrence of the given pattern to 'X'
Replace all occurrence of the given pattern to 'X'.
For example, given that the pattern="abc", replace "abcdeffdfegabcabc" with "XdeffdfegX". Note that
multiple occurrences of abc's that are contiguous will be replaced with only one 'X'.
*/
@sundeepblue
sundeepblue / interleave_string.cpp
Created February 3, 2014 19:19
dp, Three strings say A,B,C are given to you. Check weather 3rd string is interleaved from string A and B. Ex: A="abcd" B="xyz" C="axybczd". answer is yes.
bool canInterleave(char *a, char *b, char *c) {
if(*a == '\0' && *b == '\0' && *c == '\0') return true;
if(*a == *b) {
if(*a == *c) return canInterleave(a+1, b, c+1) || canInterleave(a, b+1, c+1);
else return false;
}
if(*a == *c) return canInterleave(a+1, b, c+1);
if(*b == *c) return canInterleave(a, b+1, c+1);
}
@sundeepblue
sundeepblue / serialize deserialize Binary Tree.cpp
Last active August 29, 2015 13:56
serialize deserialize Binary Tree
// in pre-order
void serialize_btree(TreeNode *root, string &s) {
if(!root) {
s += '#';
return;
}
s += to_string(root->data);
serialize_btree(root->left, s);
serialize_btree(root->right, s);