Skip to content

Instantly share code, notes, and snippets.

View rohan-varma's full-sized avatar

Rohan Varma rohan-varma

View GitHub Profile
int* getProductsOfAllIntsExceptAtIndexFaster(int* arr, int len) {
int* beforeIndex = new int[len];
beforeIndex[0] = 1;
for(int i = 1; i < len ; i ++) {
beforeIndex[i] = beforeIndex[i-1] * arr[i-1];
}
int* afterIndex = new int[len];
afterIndex[len - 1] = 1;
for(int i = len - 2 ; i >= 0; --i) {
int* getProductsOfAllIntsExceptAtIndexFaster(int* arr, int len) {
...
int* afterIndex = new int[len]; //keep track of products after an index
afterIndex[len - 1] = 1;
for(int i = len - 2 ; i >= 0; --i) {
afterIndex[i] = afterIndex[i + 1] * arr[i + 1]; //multiply by the element after this index.
}
... }
Node* lowest_common_ancestor(Node* node_1, Node* node_2) {
if(node_1==nullptr && node_2==nullptr) {
return nullptr;
}
if(node_1->parent == node_2->parent) {
return node_1->parent;
}
int node_1_depth = (node_1->depth == -1) ? node_1->depth=get_depth(node_1):node_1->depth;
int node_2_depth = (node_2->depth == -1) ? node_2->depth=get_depth(node_2):node_2->depth;
//set parent depths
bool search_tree(Node* root, Node* to_search) {
if(root == to_search) {
return true;
}
else return search_tree(root->left, to_search) || search_tree(root->right, to_search);
}
Node* lowest_common_ancestor(Node* root_node, Node* node_1, Node* node_2) {
//if either node is the root node, then thats the LCA.
if(node_1 == root_node || node_2 == root_node) {
return root_node;
}
if(search_tree(root_node->left, node_1) && !(search_tree(root_node->left, node_2))) {
return root_node;
}
else if(search_tree(root_node->right, node_1) && !(search_tree(root_node->right, node_2))) {
return root_node;
Node* lowest_common_ancestor(Node* root_node, Node* node_1, Node* node_2) {
//if either node is the root node, then thats the LCA.
if(node_1 == root_node || node_2 == root_node) {
return root_node;
}
bool node_1_exists_in_left = search_tree(root_node->left, node_1);
bool node_2_exists_in_left = search_tree(root_node->left, node_2);
if(node_1_exists_in_left && node_2_exists_in_left) {
return lowest_common_ancestor(root_node->left, node_1, node_2);
int gcd_list(vector<int> nums) {
vector<int> non_zeros;
for(int i = 0 ; i < nums.size() ; i++) {
if(nums[i]!=0) {
non_zeros.push_back(nums[i]);
}
}
if(non_zeros.size() == 1)
return non_zeros[0];
pair<int, int> mins = get_min(non_zeros);
@rohan-varma
rohan-varma / gdcpy.py
Last active September 24, 2016 01:31
def gcd_list(li):
return [x for x in li if x!=0][0] if len([x for x in li if x!=0])==1 else return gcd_list(map(lambda x: x%min([x for x in li if x!=0]) if x!=min([x for x in li if x!=0]) else x, [x for x in li if x!=0]))
vector<int> merge_sorted_lists(vector<vector<int>> &lists) {
vector<int> ans;
if(lists.empty())
return ans;
int* list_ptrs = new int[lists.size()]; //an array that, for each lists, keeps track of the index of
//the element that has not yet been processed
priority_queue<pair<int, int>> pq;
for(int i = 0 ; i < lists.size() ; i ++) {
pq.push({lists[i][0], i}); //where i is the list number
[DataContract]
public class NeuralNetwork {
[DataMember]
public int NumLayers { get; set; }
[DataMember]
public WeightMatrix weights { get; set; }
[DataMember] public NonLinearFunction sigmoid { get; set; }