This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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 ; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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 | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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/ | |
*/ |