Skip to content

Instantly share code, notes, and snippets.

View Kishy-nivas's full-sized avatar
🏠
Working from home

Kishore Srinivas Kishy-nivas

🏠
Working from home
  • Chennai, India
View GitHub Profile
@Kishy-nivas
Kishy-nivas / stl.cpp
Created November 9, 2018 09:17
STL and templates quick reference
#include <bits/stdc++.h>
using namespace std;
// function template examples
template <typename T>
void print_vector(const vector<T>& temp){
cout<<"{";
for(int i=0;i<temp.size();i++){
cout<<temp[i];
if(i!= temp.size()-1)
@Kishy-nivas
Kishy-nivas / minheap.cpp
Last active August 7, 2018 05:58
minheap implementation in CPP
#include <bits/stdc++.h>
using namespace std;
/*
Time complexity:
getMin() : O(1)
extractMin(): O(LogN)
decreaseKey : O(LogN)
insert : O(LogN)
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> pi;
int main() {
int t;
cin>>t;
class Solution {
public:
bool isSameTree(TreeNode* p, TreeNode* q) {
if(p== nullptr and q == nullptr) // reached the end, so far it passed, so return true
return true;
if(p== nullptr || q== nullptr) // failed,before traversing the entire tree
return false;
if(p->val != q->val) // again a failure
return false;
return isSameTree(p->left,q->left) && isSameTree(p->right,q->right); //both of the left and right child must pass the test !
@Kishy-nivas
Kishy-nivas / greater_tree.cpp
Created June 20, 2018 14:48
greater_tree.cpp
class Solution {
public:
int sum =0;
TreeNode* convertBST(TreeNode* root) {
if(root == nullptr)
return nullptr;
convertBST(root->right);
sum += root->val;
root->val = sum ;
convertBST(root->left);
string tree2str(TreeNode* root){
if(root == nullptr)
return "";
else
return root->val + "(" + tree2str(root->left) + ")" + "(" + tree2str(root->right) + ")";
}
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
def almostIncreasingSequence(sequence)
max = -100000
mis_match = 0
second_max = -10000
sequence.each do |val|
if val > max
second_max = max
max = val
elsif val > second_max
max = val