Skip to content

Instantly share code, notes, and snippets.

View rohith2506's full-sized avatar

Rohith Uppala rohith2506

View GitHub Profile
@rohith2506
rohith2506 / 167_3.cpp
Created February 14, 2013 11:46
codeforces 167 div 2 problem 3:-
#include <iostream>
#include <stdio.h>
#include <vector>
#include <cmath>
typedef long long int ll;
using namespace std;
@rohith2506
rohith2506 / 167_4.cpp
Created February 15, 2013 12:22
codeforces 167 div 2 problem 4:-
#include <iostream>
#include <stdio.h>
#include <set>
#include <map>
#define MAX 100010
int a[MAX],b[MAX];
using namespace std;
set<int> s;
map<int,int> mp;
@rohith2506
rohith2506 / 167_5.cpp
Created February 16, 2013 09:57
codeforces 167 div2 problem 5:- (a very nice problem)
#include<iostream>
#include<vector>
#include<set>
using namespace std;
#define pb push_back
#define mp make_pair
typedef long long ll;
ll n,m,a[333333],b[333333],x,y;
vector<ll>v[333333];
@rohith2506
rohith2506 / srm_570_3.cpp
Created February 17, 2013 12:38
Topcoder srm 570 div 2 level 3:- (actually this can be implemented as number of contiguous subsets of given tree )
/*
number of ways will be number of ways of its children can be arranged consecutively excluding
its root
*/
#include <iostream>
#include <stdio.h>
#include <vector>
#include <set>
#include <map>
@rohith2506
rohith2506 / palindrome.cpp
Created April 26, 2013 04:14
palindrome partitioning (leetcode)
/*
"This is palindrome partitioning"
@author : rohit
*/
#include <iostream>
#include <stdio.h>
#include <cmath>
#include <vector>
#include <set>
#include <algorithm>
@rohith2506
rohith2506 / palindrome_3.cpp
Created April 26, 2013 04:32
palindrome minimum cuts
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cstring>
#include <cmath>
#define MAXI 1000000
using namespace std;
int func(string s){
@rohith2506
rohith2506 / palindrome_substr.cpp
Created April 30, 2013 11:24
Longest Palindromic sub string(DP Approach)
/*
Longest palindromic substring (DP approach)
time complexity :- O(n^2)
space complexity:- O(n^2)
@Author : rohit
*/
#include <iostream>
#include <stdio.h>
#include <cmath>
@rohith2506
rohith2506 / 635_3.cpp
Created October 24, 2014 08:02
To find diameter of graph.(longest path between any two vertices)
const long INF = 4000000000000LL;
int n;
// Storing the graph. Each g[i] is a list of edges adjacent to vertex i
// each edge is a pair (j,e), where j is the other vertex connected to i
// and e is the id of the edge. So L[e] is the weight of the edge.
vector<list<tuple<int, int>>> g;
vector<int> L;
long dist[2000];
// Pre-order using iterative traversal
void preorder(node *root){
stack<node *> stk;
stk.push(root);
while(!stk.empty()){
node *temp = stk.top();
cout << temp -> data << " ";
stk.pop();
if(temp -> right)
/*
Great solution:-
Idea is to swap each positive integer you encounter to its "rightful" place at index (x-1) where x is the integer. It's O(n) because you visit each integer in at most 2 unique loop iterations.
Best time complexity:- O(n)
Worst time complexity:- O(n^2)
Space:- O(1)
*/
class Solution {
public: