Skip to content

Instantly share code, notes, and snippets.

View rohith2506's full-sized avatar

Rohith Uppala rohith2506

View GitHub Profile
'''
Only trciky part is dictioanry mod.
How did he do it??
let's say 0.147851...
First when 1 encouters mod[1] = 0(index) will be stored
now when again 1 encounters, it's already present
so first for loop is from (0 to 0)
and second for loop is from (1 to 5)(5 is the index where it again occured)
and return the solution
/*
For ref:
https://oj.leetcode.com/discuss/12780/my-concise-c-solution-ac-90-ms
This is the classic problem.
1) Know how to solve largest rectangle in histogram.
Solution:-
for every index i {
for every index j less than its height{
calculate height * (distance between this rectangle and that rectangle)
/*
Solution still not complete.
*/
class Solution{
public:
void dfs(vector<vector<int> > paths, vector<string> words, vector<string> v, string start, string end){
if(start == end){
result.push_back(v);
return ;
/*
O(n) solution using BFS.
Let's say we have [2,3,1,1,4]. Divide values into nodes such that nodes at level i can be reached by jump from level (i-1)
now didiving into levels, [2 || 3,1 || 1,4]
*/
class Solution {
public:
/*
Simple logic.
if char is '(': store that locations in stack. push 0 into vec ( for storing the longest parantheses value until there in vec )
else:
if it is not empty: valid parantheses, take the location from stack. calculate length. update it via the value stored in vec. update max
else: just push 0 into vec ( for storing longest parantheses length until there )
return total_max
*/
class Solution:
# @return an integer
def lengthOfLongestSubstring(self, s):
prev_length = 0
max_len = 0
char_dict = {}
for i,c in enumerate(s):
if c in char_dict.keys() and (i - char_dict[c] <= prev_length):
prev_length = (i - char_dict[c])
else:
/*
Using simple three loops
*/
class Solution {
public:
long int stoi(string s){
long int num = 0;
for(int i=0;i<s.length();i++)
num = (num * 10) + (int)(s[i] - '0');
/**
* Definition for singly-linked list with a random pointer.
* struct RandomListNode {
* int label;
* RandomListNode *next, *random;
* RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
* };
*/
class Solution {
public:
class Solution {
public:
int threeSumClosest(vector<int> &num, int target) {
int min = 10e5;
int result = 0;
sort(num.begin(),num.end());
for(int i=0;i<num.size();i++){
int j = i+1;
int k = num.size()-1;
while(j < k){
/*
Simple logic:-
if open > close: call close one and increase its count
if open < n: call open one and increase its count
breaking point: when close == n
http://www.geeksforgeeks.org/print-all-combinations-of-balanced-parentheses/
*/