├── .idea
├── .gitignore
├── Daily-Coding-Problem.iml
├── misc.xml
├── modules.xml
└── vcs.xml
├── CMakeLists.txt
├── CODE_OF_CONDUCT.md
├── README.md
├── Solution
├── Day-001.cpp
├── Day-002.cpp
├── Day-003.cpp
├── Day-004.cpp
├── Day-005.cpp
├── Day-006.cpp
├── Day-007.cpp
├── Day-008.cpp
├── Day-009.cpp
├── Day-010.cpp
├── Day-011.cpp
├── Day-012.cpp
├── Day-013.cpp
├── Day-014.cpp
├── Day-015.cpp
├── Day-016.cpp
├── Day-017.cpp
├── Day-018.cpp
├── Day-019.cpp
├── Day-020.cpp
├── Day-021.cpp
├── Day-022.cpp
├── Day-023.cpp
├── Day-025.cpp
├── Day-026.cpp
├── Day-027.cpp
├── Day-028.cpp
├── Day-029.cpp
├── Day-030.cpp
├── Day-031.cpp
├── Day-032.cpp
├── Day-033.cpp
├── Day-034.cpp
├── Day-035.cpp
├── Day-036.cpp
├── Day-037.cpp
├── Day-038.cpp
├── Day-039.cpp
├── Day-040.cpp
├── Day-041.cpp
├── Day-042.cpp
├── Day-043.cpp
├── Day-044.cpp
├── Day-045.cpp
├── Day-046.cpp
├── Day-047.cpp
├── Day-048.cpp
├── Day-049.cpp
├── Day-050.cpp
├── Day-051.cpp
├── Day-052.cpp
├── Day-053.cpp
├── Day-054.cpp
├── Day-055.cpp
├── Day-056.cpp
├── Day-057.cpp
├── Day-058.cpp
├── Day-059.cpp
├── Day-060.cpp
├── Day-061.cpp
├── Day-062.cpp
├── Day-063.cpp
├── Day-064.cpp
├── Day-065.cpp
├── Day-068.cpp
├── Day-069.cpp
├── Day-070.cpp
├── Day-071.cpp
├── Day-072.cpp
├── Day-073.cpp
├── Day-074.cpp
├── Day-075.cpp
├── Day-076.cpp
├── Day-077.cpp
├── Day-078.cpp
├── Day-079.cpp
├── Day-080.cpp
├── Day-081.cpp
├── Day-082.cpp
├── Day-083.cpp
├── Day-084.cpp
├── Day-085.cpp
├── Day-086.cpp
├── Day-087.cpp
├── Day-088.cpp
├── Day-089.cpp
├── Day-090.cpp
├── Day-091.py
├── Day-092.cpp
├── Day-093.cpp
├── Day-094.cpp
├── Day-095.cpp
├── Day-096.cpp
├── Day-097.cpp
├── Day-098.cpp
├── Day-099.cpp
├── Day-100.cpp
├── Day-101.cpp
├── Day-102.cpp
├── Day-103.cpp
├── Day-104.cpp
├── Day-105.cpp
├── Day-106.cpp
├── Day-107.cpp
├── Day-108.cpp
├── Day-109.cpp
├── Day-110.cpp
├── Day-111.cpp
├── Day-112.cpp
├── Day-113.cpp
├── Day-114.cpp
├── Day-115.cpp
├── Day-116.cpp
├── Day-117.cpp
├── Day-118.cpp
├── Day-119.cpp
├── Day-120.cpp
├── Day-122.cpp
├── Day-124.cpp
├── Day-128.cpp
├── Day-133.cpp
├── Day-134.cpp
├── Day-136.cpp
├── Day-166.cpp
├── Day-170.cpp
├── Day-178.cpp
├── Day-180.cpp
├── Day-185.cpp
├── Day-191.cpp
├── Day-200.cpp
├── Day-214.cpp
├── Day-221.cpp
├── Day-222.cpp
├── Day-223.cpp
├── Day-224.cpp
├── Day-225.cpp
├── Day-231.cpp
├── Day-232.cpp
├── Day-241.cpp
├── Day-242.cpp
├── Day-244.cpp
├── Day-255.cpp
├── Day-256.cpp
├── Day-257.cpp
├── Day-258.cpp
├── Day-259.cpp
├── Day-260.cpp
├── Day-261.cpp
├── Day-262.cpp
├── Day-263.cpp
├── Day-265.cpp
├── Day-269.cpp
├── Day-272.cpp
├── Day-273.cpp
├── Day-282.cpp
├── Day-285.cpp
├── Day-287.cpp
├── Day-288.cpp
├── Day-340.cpp
├── Day-341.cpp
├── Day-351.cpp
├── Day-362.cpp
├── Day-547.cpp
├── Day-558.cpp
├── Day-559.cpp
├── Day-560.cpp
├── Day-561.cpp
├── Day-562.cpp
├── Day-567.cpp
├── Day-568.cpp
├── Day-574.cpp
├── Day-577.cpp
├── Day-578.cpp
├── Day-579.cpp
├── Day-580.cpp
└── Day-582.cpp
└── cmake-build-debug
├── .cmake
└── api
│ └── v1
│ └── query
│ ├── cache-v2
│ ├── cmakeFiles-v1
│ ├── codemodel-v2
│ └── toolchains-v1
└── CMakeFiles
├── 3.24.2
├── CMakeSystem.cmake
└── CompilerIdC
│ └── CMakeCCompilerId.c
├── CMakeOutput.log
└── clion-environment.txt
/.idea/.gitignore:
--------------------------------------------------------------------------------
1 | # Default ignored files
2 | /shelf/
3 | /workspace.xml
4 | # Editor-based HTTP Client requests
5 | /httpRequests/
6 | # Datasource local storage ignored files
7 | /dataSources/
8 | /dataSources.local.xml
9 |
--------------------------------------------------------------------------------
/.idea/Daily-Coding-Problem.iml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | cmake_minimum_required(VERSION 3.24)
2 | project(Daily_Coding_Problem)
3 |
4 | set(CMAKE_CXX_STANDARD 14)
5 |
6 | add_executable(Daily_Coding_Problem )
7 |
--------------------------------------------------------------------------------
/CODE_OF_CONDUCT.md:
--------------------------------------------------------------------------------
1 | * Be professional.
2 | * Be responsible.
3 | * Be welcoming.
4 | * Be kind.
5 | * Be respectful of other viewpoints and ideas.
6 | * Be supportive and look out for each other.
7 |
--------------------------------------------------------------------------------
/Solution/Day-001.cpp:
--------------------------------------------------------------------------------
1 | class Solution {
2 | public:
3 | vector twoSum(vector& nums, int target) {
4 | vector< int > result(2 , -1);
5 | map< int, int > temp;
6 | int totalSize = static_cast(nums.size());
7 | for( int index = 0; index < totalSize; ++index ) {
8 | if(temp.find( nums[ index ] ) != temp.end() ){
9 | result[0] = index;
10 | result[1] = temp[ nums[ index ] ];
11 | break;
12 | }
13 | temp[ target - nums[ index ] ] = index;
14 | }
15 | return result;
16 | }
17 | };
18 |
--------------------------------------------------------------------------------
/Solution/Day-002.cpp:
--------------------------------------------------------------------------------
1 | class Solution {
2 | public:
3 | vector productExceptSelf(vector& nums) {
4 | const size_t n= nums.size();
5 | vectorans(n,1);
6 | int answer =1;
7 | for(size_t i=0;i=0;--i){
16 | if(i!=0){
17 | temp = nums[i];
18 | nums[i] = ans[i-1]*answer;
19 | answer*=temp;
20 | }else{
21 | nums[i] = answer;
22 | }
23 | }
24 | return nums;
25 | }
26 | };
27 |
--------------------------------------------------------------------------------
/Solution/Day-003.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Definition for a binary tree node.
3 | * struct TreeNode {
4 | * int val;
5 | * TreeNode *left;
6 | * TreeNode *right;
7 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8 | * };
9 | */
10 | class Codec {
11 | queueq;
12 | TreeNode* desr(void) {
13 | if (q.empty()) {
14 | return nullptr;
15 | }
16 | string temp = q.front();
17 | q.pop();
18 | if (temp =="&") {
19 | return nullptr;
20 | }
21 | TreeNode* root = new TreeNode(atoi(temp.c_str()));
22 | root -> left = desr();
23 | root -> right = desr();
24 | return root;
25 | }
26 | string s;
27 | void h_ser(TreeNode* root) {
28 | if (root == nullptr) {
29 | s += "&/";
30 | return ;
31 | }
32 | s += to_string(root->val) + "/";
33 | h_ser(root->left);
34 | h_ser(root->right);
35 | }
36 | public:
37 |
38 | // Encodes a tree to a single string.
39 | string serialize(TreeNode* root) {
40 | s = "";
41 | h_ser(root);
42 | return s;
43 | }
44 |
45 | // Decodes your encoded data to tree.
46 | TreeNode* deserialize(string data, int index = 0) {
47 | string temp{};
48 | for (int i=0; iserialize(root);
64 | // TreeNode* ans = deser->deserialize(tree);
65 | // return ans;
66 |
--------------------------------------------------------------------------------
/Solution/Day-004.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | * This problem was asked by Stripe. Given an array of integers,
3 | * find the first missing positive integer in linear time and constant space.
4 | *
5 | * In other words, find the lowest positive integer that does not exist in the array.
6 | * The array can contain duplicates and negative numbers as well.
7 | *
8 | * For example, the input [3, 4, -1, 1] should give 2.
9 | *
10 | * The input [1, 2, 0] should give 3.
11 | *
12 | * You can modify the input array in-place.
13 | *
14 | */
15 | /*
16 | * 1. We can do simply with N^2 time complexity just search for each element form 1
17 | *
18 | * 2. We can use un_ordered map & can do it in O(n) but it require extra O(n) space
19 | *
20 | * 3. if array doesn't contain duplicate and negative number then we can use formula n*(n+1)/2 and
21 | * subtract those number present in the array at last we will remain with the lowest number not
22 | * present in the array
23 | *
24 | * 4. now let's separate the negative and positive element and then mark the index at element as -ve
25 | * now the first +ve index is our answer
26 | *
27 | */
28 | #include
29 | using namespace std;
30 | int main(void){
31 | int n;
32 | cin>>n;
33 | vectorv(n);
34 | for(auto &itr:v){
35 | cin>>itr;
36 | itr--;
37 | }
38 | auto itr = partition(v.begin() , v.end() , [](int a){return a>=0;});
39 | for(auto i = v.begin(); i!=itr; ++i){
40 | if(*i>=v.size())
41 | continue;
42 | v.at(*i) = -v.at(*i);
43 | }
44 | int counter = 0;
45 | for(auto i = v.begin();counter<(int)v.size()&&i!=itr; ++i){
46 | if(v.at(counter)>0){
47 | cout<
24 | using namespace std;
25 | paircons(int a, int b){
26 | return make_pair(a,b);
27 | }
28 | int car(pairp){
29 | return p.first;
30 | }
31 | int cdr(pairp){
32 | return p.second;
33 | }
34 | int main(void){
35 | cout<
2 | #include
3 | using namespace std;
4 | struct NODE{
5 | public:
6 | int data;
7 | NODE *npx; // reference as Next Previous Xor
8 | };
9 | NODE* Xor(NODE*a , NODE*b){
10 | return (NODE*)((uintptr_t)a ^ (uintptr_t)b);
11 | }
12 | NODE* PREV = NULL; // store address of previous NODE form last NODE
13 | NODE* END = NULL; // store address of last Node
14 | NODE* NEXT = NULL; // store address of upcoming Node (isn't interesting :)
15 | void add(NODE**HEAD , int value){
16 | NODE *new_node = new NODE();
17 |
18 | new_node -> data = value;
19 |
20 | if(*HEAD!=NULL){
21 | END->npx = Xor(PREV , new_node);
22 | PREV = END;
23 | END = new_node;
24 | END->npx = Xor(PREV , NEXT);
25 | }else{
26 | new_node->npx = Xor(PREV , NEXT);
27 | *HEAD = new_node;
28 | END = new_node;
29 | }
30 |
31 | return ;
32 | }
33 | NODE * get(int index ,NODE *HEAD){
34 | NODE * current = HEAD;
35 | NODE * Prev = NULL;
36 | NODE * Next = NULL;
37 | int i=1;
38 | while(inpx , Prev);
41 | Prev = current;
42 | current = Next;
43 | }
44 | return current;
45 | }
46 | void print(NODE*HEAD){
47 | NODE * current = HEAD;
48 | NODE * Prev = NULL;
49 | NODE * Next = NULL;
50 | while(current!=NULL){
51 | cout<data<<' ';
52 | Next = Xor(current->npx , Prev);
53 | Prev = current;
54 | current = Next;
55 | }
56 | cout<data==30);
72 | cout<data<
14 | using namespace std;
15 | int main(void){
16 | string s;
17 | cin>>s;
18 | vectordp(s.size()+1,0);
19 | for(int i=0;i<=(int)s.size();++i){
20 | if(i==0){
21 | dp[i] = 1;
22 | continue;
23 | }
24 | if(s[i]!=0){
25 | dp[i]=dp[i-1];
26 | }
27 | int x = s[i-1]-'0';
28 | x*=10;
29 | x+=(s[i]-'0');
30 | if(x>=10 and x<=26){
31 | dp[i]+=((i-2)>=0)?dp[i-2]:1;
32 | }
33 | }
34 | cout<
22 | using namespace std;
23 | struct Node{
24 | Node*left;
25 | Node*right;
26 | int data;
27 | Node():left(nullptr),right(nullptr),data(0){}
28 | };
29 | int counter{};
30 | bool unival(Node*root){
31 | if(root==nullptr){
32 | return true;
33 | }
34 | bool ok = true; // let this be a unival tree
35 | bool left = unival(root->left);
36 | bool right = unival(root->right);
37 | ok &=left;
38 | ok &=right;
39 | if(!ok)/*if either left or right is not uni-value*/{
40 | return ok;
41 | }
42 | if(root->left && root->left->data !=root->data){
43 | return false;
44 | }
45 | if(root->right && root->right->data !=root->data){
46 | return false;
47 | }
48 | counter++;
49 | return true;
50 | }
51 | int main(void){
52 | // tree formation
53 | Node*root = new Node();
54 | root->data = 0;
55 | Node*n1 = new Node();
56 | n1->left = nullptr;
57 | n1->right= nullptr;
58 | n1->data = 1;
59 | root->left = n1;
60 |
61 | Node*n2 = new Node();
62 | n2->data = 0;
63 | root->right= n2;
64 |
65 | Node*n3 = new Node();
66 | Node*n4 = new Node();
67 | n2->right = n3; n3->data = 0;
68 | n2->left = n4; n4->data = 1;
69 | Node*n5 = new Node();
70 | Node*n6 = new Node();
71 | n4->right = n5;
72 | n4->left = n6;
73 | n5->data = 1;
74 | n6->data = 1;
75 | // counting uni-value trees
76 | unival(root);
77 | cout<
17 | using namespace std;
18 | int main(void){
19 | int number_of_element;
20 | cin>>number_of_element;
21 | vectorelement(number_of_element);
22 | for(auto &itr:element){
23 | cin>>itr;
24 | }
25 | int including(0) , excluding(0); // consider these as state of dp . HOW ?
26 | // take including as max sum we get till now including this element
27 | // take excluding as max sum we get till now excluding this element
28 | //
29 | // initially both of them are 0.
30 | //
31 | // so in next state what will be our including simple max(including , excluding+current_element)
32 | //
33 | // what will be excluding ; simple the including we got till now because we excluded the current element
34 | // and the max sum we achieved till now is the same as present in the including variable
35 | //
36 | // but how take care of 0 and negative number ?????
37 | //
38 | // simple you can just ignore them they are not going to contribute to answer in any way ;)
39 | //
40 |
41 | for(int current_Index=0;current_Index
2 | #include
3 | #include
4 | #include
5 | using namespace std;
6 | void test_function(void){
7 | cout<<"This is testing function "<t ,unsigned int period){
13 |
14 | std::this_thread::sleep_for(std::chrono::milliseconds(period));
15 |
16 | t();
17 |
18 | return;
19 | }
20 |
21 | int main(void){
22 | timer_function(test_function , 3000);
23 | return 0;
24 | }
25 |
--------------------------------------------------------------------------------
/Solution/Day-011.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | This problem was asked by Twitter.
3 |
4 | Implement an autocomplete system.
5 |
6 | That is, given a query string s and a set of all possible query strings, return all strings in the set that have s as a prefix.
7 |
8 | For example, given the query string de and the set of strings [dog, deer, deal], return [deer, deal].
9 |
10 | Hint: Try preprocessing the dictionary into a more efficient data structure to speed up queries.
11 |
12 |
13 | */
14 | #include
15 | using namespace std;
16 | struct Node{
17 | Node*alphabets[26];
18 | bool isEnd; // for the end of word
19 | Node(){
20 | for(int i=0;i<26;++i){
21 | alphabets[i] = nullptr;
22 | }
23 | isEnd = false;
24 | }
25 | };
26 | void addword(Node*root,string s){
27 | Node*t_Node=root;
28 | for(int i=0; i<(int)s.size(); ++i){
29 | int index = s[i]-'a';
30 | if(t_Node->alphabets[index]==nullptr){
31 | t_Node->alphabets[index]=new Node();
32 | }
33 | t_Node = t_Node->alphabets[index];
34 | }
35 | t_Node->isEnd = true;
36 | return;
37 | }
38 | bool last(Node*temp){
39 | for(int i=0;i<26;++i){
40 | if(temp->alphabets[i]!=nullptr){
41 | return false;
42 | }
43 | }
44 | return true;
45 | }
46 |
47 | void util(Node*root , string ans){
48 | if(root->isEnd){
49 | cout<alphabets[i]!=nullptr){
56 | ans.push_back(char((int)'a'+i));
57 | util(root->alphabets[i],ans);
58 | ans.pop_back();
59 | }
60 | }
61 | }
62 | void autocomplete(Node*root ,string search_Pattern){
63 | struct Node*temp = root;
64 | for(int i=0;i<(int)search_Pattern.size();++i){
65 | int index = search_Pattern[i]-'a';
66 | if(temp->alphabets[index]==nullptr){
67 | return;
68 | }
69 | temp=temp->alphabets[index];
70 | }
71 | if(last(temp)){
72 | cout<
26 | using namespace std;
27 | int main(void){
28 | int x; // number of stairs can be climbed
29 | cin>>x; // 2 by default
30 | vectorpossible(x);
31 | for(auto &itr:possible){
32 | cin>>itr;
33 | }
34 | int n; // final stair
35 | cin>>n;
36 | vectordp(n+2);
37 | for(int i=1;i<=n;++i){
38 | dp[i]=0;
39 | for(auto &itr:possible){
40 | if(itr==i){
41 | dp[i]+=1;
42 | }
43 | if(itr>i){
44 | break;
45 | }
46 | dp[i]+=(i-itr>=0)?dp[i-itr]:0;
47 | }
48 | }
49 | cout<
26 | using namespace std;
27 | int main(void){
28 | string s;
29 | int k ;
30 | cin>>s>>k;
31 | int i=0 , j=0 , t_max =0 , mx = 1;
32 | arrayfrequency;
33 | fill(frequency.begin() , frequency.end() , 0);
34 | int t_frequency = 0;
35 | while(i<(int)s.size() && j<(int)s.size()){
36 | if(t_frequency<=k)/*case where distinct element are in range*/{
37 | if(frequency[s[j]-'a']==0)/*this means that s[j] is not encounter before so this is distinct character for us*/{
38 | t_frequency++;
39 | }
40 | frequency[s[j]-'a']++; // simple frequency counter
41 | if(t_frequency<=k)/*case where number of distinct character goes out of scope*/{
42 | mx = max(mx , abs(i-j-1));
43 | j++;
44 | }
45 | }else/*this will handle the out of scope case*/{
46 | frequency[s[i]-'a']--;
47 | if(frequency[s[i]-'a']==0)/*this means that s[i] is vanished from our selected string so we can say that we have only
48 | (n-1) distinct character left */{
49 | t_frequency--;
50 | }
51 | i++;
52 | mx = max(mx , abs(i-j-1)); // choose the max length of string
53 | }
54 | }
55 | cout< P(__) = PI*(r*r) / 4(r*r)
18 | * ==> P(__) = PI/4
19 | * ==> 4*P(__) = PI
20 | */
21 |
22 | #include
23 | using namespace std;
24 | int main(void){
25 | srand(time(NULL));
26 | double C{},R{}; // C = Point generated inside the Circle & R is point generated inside Rectangle
27 | for(int i=0;i<100000;++i){
28 | double x = double((rand()%(100000+1))/100000.0f);
29 | double y = double((rand()%(100000+1))/100000.0f);
30 | if(x*x+y*y<=1.0f){
31 | C++;
32 | }
33 | R++;
34 | }
35 | cout<<"Expected Value of PI = "<
24 | using namespace std;
25 | int main(void){
26 | srand(time(NULL));
27 | int current_Element = NULL;
28 | double probability = 0;
29 | int n;
30 | cin>>n; // number of elements ; don't worry we are not going to use to calculate the probability of selection
31 | int element_Processed={0};
32 | while(n--){
33 | srand(time(NULL));
34 | int element;
35 | cin>>element; //
36 | if(element_Processed == 0){
37 | element_Processed++;
38 | current_Element = element;
39 | probability = 1.0f;
40 | continue;
41 | }else if(rand()%2==1)/*case when element should be replaced*/{
42 | current_Element = element;
43 | }
44 | probability = (probability * (1.0f - double(1.0f/double(element_Processed+1.0f))));
45 | element_Processed++;
46 | }
47 | printf("Element %d is chosen with probability %.3f",current_Element , probability);
48 | return 0;
49 | }
50 |
--------------------------------------------------------------------------------
/Solution/Day-016.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | * This problem was asked by Twitter.
3 |
4 | You run an e-commerce website and want to record the last N order ids in a log.
5 | Implement a data structure to accomplish this, with the following API:
6 |
7 | record(order_id): adds the order_id to the log
8 | get_last(i): gets the ith last element from the log. i is guaranteed to be smaller than or equal to N.
9 | You should be as efficient with time and space as possible.
10 | *
11 | */
12 |
13 |
14 | /*
15 | * let me know if you got any better idea : offamitkumar@gmail.com
16 | */
17 | #include
18 | using namespace std;
19 | vectorLog;
20 | void record(int order_id){
21 | Log.emplace_back(order_id);
22 | }
23 | int get_last(int i){
24 | assert((int)Log.size()>=i);
25 | return Log.at((int)Log.size()-(i-1)-1);
26 | }
27 | int main(void){
28 | int n; // number of order
29 | cin>>n;
30 | while(n--){
31 | int order_id;
32 | cin>>order_id;
33 | record(order_id);
34 | }
35 | cout<
22 | using namespace std;
23 | int main(void){
24 | int n;// size of array
25 | cin>>n;
26 | vectorv(n);
27 | for(auto &itr:v){
28 | cin>>itr;
29 | }
30 | int k; // window size
31 | cin>>k;
32 | int i=0;
33 | dequeq;
34 | for(i=0;i
15 | using namespace std;
16 | int main(void){
17 | int n , m; cin>>n>>m;
18 | vector>v(n,vector(m,0));
19 | for(int i=0;i>v[i][j];
22 | }
23 | }
24 | int k=m; // number of colors
25 | vector>dp(n+1,vector(k,0));
26 | // dp[i][j] = cost of building ith house with jth color
27 | for(int j=0;j 7 -> 8 -> 10 and B = 99 -> 1 -> 8 -> 10, return the node with value 8.
8 |
9 | In this example, assume nodes with the same value are the exact same node objects.
10 |
11 | Do this in O(M + N) time (where M and N are the lengths of the lists) and constant space.
12 | *
13 | *
14 | *
15 | *
16 | */
17 | #include
18 | using namespace std;
19 |
20 | struct Node {
21 | int data;
22 | struct Node *next;
23 | };
24 | int length(struct Node *head) {
25 | int len = 0;
26 | while(head != NULL) {
27 | len++;
28 | head = head->next;
29 | }
30 | return len;
31 | }
32 | struct Node* findMergePoint(struct Node *A, struct Node *B) {
33 | int m = length(A);
34 | int n = length(B);
35 | int d = n - m;
36 | if(m > n) {
37 | struct Node* temp = A;
38 | A = B;
39 | B = temp;
40 | d = m - n;
41 | }
42 | int i;
43 | for(i=0;inext;
45 | }
46 | while(A != NULL && B != NULL) {
47 | if(A == B) {
48 | return A;
49 | }
50 | A = A->next;
51 | B = B->next;
52 | }
53 | return NULL;
54 | }
55 | int main()
56 | {
57 | struct Node *head1 = NULL, *head2 = NULL;
58 | struct Node *temp[7];
59 | for(int i=0;i<7;i++) {
60 | temp[i] = (Node *)malloc(sizeof(Node));
61 | }
62 | temp[0]->data = 4;
63 | temp[0]->next = temp[1];
64 | temp[1]->data = 6;
65 | temp[1]->next = temp[2];
66 | temp[2]->data = 7;
67 | temp[2]->next = temp[3];
68 | temp[3]->data = 1;
69 | temp[3]->next = NULL;
70 | temp[4]->data = 9;
71 | temp[4]->next = temp[5];
72 | temp[5]->data = 3;
73 | temp[5]->next = temp[6];
74 | temp[6]->data = 5;
75 | temp[6]->next = temp[2];
76 |
77 | head1 = temp[0];
78 | head2 = temp[4];
79 | struct Node *C = findMergePoint(head1, head2);
80 | cout<data;
81 | return 0;
82 | }
83 |
--------------------------------------------------------------------------------
/Solution/Day-021.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | * This problem was asked by Snapchat.
3 |
4 | Given an array of time intervals (start, end) for classroom lectures
5 | (possibly overlapping), find the minimum number of rooms required.
6 |
7 | For example, given [(30, 75), (0, 50), (60, 150)], you should return 2.
8 | *
9 | *
10 | */
11 | #include
12 | using namespace std;
13 | bool compare(paira,pairb){
14 | return a.first>v;
18 | v.push_back({30,75});
19 | v.push_back({0,50});
20 | v.push_back({60,150});
21 | sort(v.begin(),v.end(),compare);
22 | int counter{},max_end_time=-1;//initially it is -1
23 | for(auto&[start_time,end_time]:v){
24 | if(max_end_time==-1){
25 | max_end_time=end_time;
26 | counter=1;
27 | }else{
28 | if(max_end_time>start_time){
29 | counter++;
30 | }else{
31 | max_end_time=end_time;
32 | }
33 | }
34 | }
35 | cout<(s.size());
5 | int col = static_cast(p.size());
6 | vector< vector< bool > > dp(row+1 ,vector< bool >( col+1 , false ) );
7 | dp[ 0 ] [ 0 ] = true;
8 | for(int i =1; i <= col; ++i ) {
9 | if( p[ i-1 ] == '*' ) {
10 | dp[0][i] = dp[ 0 ][ i-2 ];
11 | }
12 | }
13 | for(int i =1; i <= row; ++i ) {
14 | for(int j=1; j <= col; ++j){
15 | if( p[ j-1 ] == s[ i-1 ] or p[ j-1 ] == '.' ) {
16 | dp[ i ][ j ] = dp[ i-1 ][ j-1 ];
17 | }else if( p[ j-1 ] == '*'){
18 | dp[ i ][ j ] = dp[ i ][ j-2 ];
19 | if(p[ j-2 ] == '.' || p[ j-2 ] == s[ i-1 ]){
20 | dp[ i ][ j ] = dp[i][j] | dp[i-1][j];
21 | }
22 | }
23 | }
24 | }
25 | return dp.back().back();
26 | }
27 | };
28 |
--------------------------------------------------------------------------------
/Solution/Day-026.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Given a singly linked list and an integer k, remove the kth last element from the list.
3 | * k is guaranteed to be smaller than the length of the list.
4 |
5 | The list is very long, so making more than one pass is prohibitively expensive.
6 |
7 | Do this in constant space and in one pass.
8 | */
9 |
10 | /*
11 | * Idea is to use two pointer Here & Tortoise , where Tortoise will first move K+1 node ahead and then both will move with
12 | * at once , when Tortoise pointer hit null then we have to delete the Here+1 node
13 | */
14 | #include
15 | using namespace std;
16 | struct Node{
17 | int data;
18 | Node *next;
19 | };
20 | void print(Node*root){
21 | Node*temp= root;
22 | while(temp!=NULL){
23 | cout<data<<" ";
24 | temp= temp->next;
25 | }
26 | puts("");
27 | return;
28 | }
29 |
30 | Node* get_node(int value){
31 | Node *temp = new Node();
32 | temp->next = NULL;
33 | temp->data = value;
34 | return temp;
35 | }
36 | Node* add(Node *root , int value){
37 | if(root==NULL){
38 | return get_node(value);
39 | }else{
40 | root->next = add(root->next , value);
41 | return root;
42 | }
43 | }
44 | void remove(Node*root , int k ){
45 | Node* here = root , *tor = root; // tortoise & here
46 | ++k;
47 | while(k--){
48 | tor = tor->next;
49 | }
50 | while(tor!=NULL){
51 | tor = tor->next;
52 | here = here->next;
53 | }
54 | here->next = here->next->next;
55 | return;
56 | }
57 | int main(int argc , char *argv[]){
58 | // write you code here
59 | Node *root = NULL;
60 | root = add(root , 10);
61 | root = add(root , 20);
62 | root = add(root , 30);
63 | root = add(root , 40);
64 | root = add(root , 50);
65 | root = add(root , 60);
66 | print(root);
67 | remove(root , 2); /// will delete 50
68 | print(root);
69 | remove(root,1);
70 | print(root);
71 | return 0;
72 | }
73 |
--------------------------------------------------------------------------------
/Solution/Day-027.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | *
3 | This problem was asked by Facebook.
4 |
5 | Given a string of round, curly, and square open and closing brackets, return whether the brackets are balanced (well-formed).
6 |
7 | For example, given the string "([])[]({})", you should return true.
8 |
9 | Given the string "([)]" or "((()", you should return false.
10 |
11 | */
12 |
13 | class Solution {
14 |
15 | stacktemp;
16 |
17 | public:
18 |
19 | bool isValid(string s) {
20 |
21 | for(auto&itr:s){
22 |
23 | if(itr == '(' || itr == '[' || itr == '{') {
24 |
25 | temp.push(itr);
26 |
27 | } else{
28 |
29 | if(temp.size() == 0){
30 |
31 | return false;
32 |
33 | }
34 |
35 | if( itr == ')' ){
36 |
37 | if( temp.top() != '(' ) {
38 |
39 | return false;
40 |
41 | }else{
42 |
43 | temp.pop();
44 |
45 | }
46 |
47 | }else if( itr == ']' ){
48 |
49 | if( temp.top() != '[' ) {
50 |
51 | return false;
52 |
53 | }else {
54 |
55 | temp.pop();
56 |
57 | }
58 |
59 | }else if( itr == '}' ) {
60 |
61 | if( temp.top() != '{' ) {
62 |
63 | return false;
64 |
65 | }else {
66 |
67 | temp.pop();
68 |
69 | }
70 | }
71 | }
72 | }
73 | return temp.size() == 0;
74 | }
75 | };
76 |
77 | int main( void ) {
78 | return 0;
79 | }
80 |
--------------------------------------------------------------------------------
/Solution/Day-029.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | This problem was asked by Amazon.
3 |
4 | Run-length encoding is a fast and simple method of encoding strings.
5 | The basic idea is to represent repeated successive characters as a single count and character.
6 | For example, the string "AAAABBBCCDAA" would be encoded as "4A3B2C1D2A".
7 |
8 | Implement run-length encoding and decoding.
9 | You can assume the string to be encoded have no digits and consists solely of alphabetic characters.
10 | You can assume the string to be decoded is valid.
11 |
12 | *
13 | *
14 | *
15 | */
16 | #include
17 | using namespace std;
18 | string encode(string &s){
19 | string res="";
20 | for(int i=0;i<(int)s.size();++i){
21 | int j = i;
22 | int counter{};
23 | while(s[i]==s[j]){
24 | ++counter;
25 | ++j;
26 | }
27 | res += to_string(counter) + s[i];
28 | i = (j-1);
29 | }
30 | return res;
31 | }
32 | string decode(string &s){
33 | string res = "";
34 | for(int i=0;i<(int)s.size();++i){
35 | int counter{};
36 | while(s[i]>='0' && s[i]<='9'){
37 | counter = (counter*10)+int(int(s[i])-int('0'));
38 | ++i;
39 | }
40 | res += string(counter , s[i]);
41 | }
42 | return res;
43 | }
44 | int main(void){
45 | string text = "AAAABBBCCDAA";
46 | string encoded_string = encode(text);
47 | cout<
25 |
26 | using namespace std;
27 |
28 | int main(void){
29 | int size_of_array;
30 | cin >> size_of_array;
31 |
32 | vector< int > height_array ( size_of_array );
33 |
34 | for(auto &height : height_array){
35 | cin >> height;
36 | }
37 |
38 | int max_level{} , left = 0 , right = size_of_array - 1 , ans{};
39 |
40 | while(left < right){
41 |
42 | int lower_height = height_array.at( height_array.at(left) < height_array.at(right) ? left++ : right-- ); // take the lower
43 |
44 | max_level = max( max_level , lower_height ); // max-level/height we have seen so far
45 |
46 | ans += (max_level - lower_height);
47 | }
48 |
49 | cout << ans << '\n' ;
50 |
51 | return 0;
52 | }
53 |
--------------------------------------------------------------------------------
/Solution/Day-031.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | *
3 | This problem was asked by Google.
4 |
5 | The edit distance between two strings refers to the minimum number of character
6 | insertions,
7 | deletions,
8 | and substitutions
9 | required to change one string to the other .
10 | For example, the edit distance between “kitten” and “sitting” is three: substitute the “k” for “s”,
11 | substitute the “e” for “i”, and append a “g”.
12 |
13 | Given two strings, compute the edit distance between them.
14 | */
15 | /*
16 | * This is well-known dynamic programming problem.
17 | */
18 |
19 | #include
20 | using namespace std;
21 | int main(void){
22 | string a , b;
23 | cin >> a >> b;
24 | vector< vector< int > > dp ((int)b.size() + 1 , vector((int)a.size()+1));
25 | for(int i=0; i < (int) a.size(); ++i){
26 | dp[i][0] = i;
27 | }
28 |
29 | for(int i=0; i < (int) b.size(); ++i){
30 | dp[0][i] = i;
31 | }
32 |
33 | for(int i{1}; i < (int) a.size() ; ++i){
34 | for(int j{1}; j < (int) b.size(); ++j){
35 | if(a[i-1] == b[i-1]){
36 | dp[i][j] = dp[i-1][j-1];
37 | }else{
38 | dp[i][j] = min( { dp[i-1][j] , dp[i-1][j-1] , dp[i][j-1] } ) + 1;
39 | }
40 | }
41 | }
42 | cout << dp[(int)a.size() - 1][(int)b.size() - 1] << '\n' ;
43 | return 0;
44 | }
45 |
--------------------------------------------------------------------------------
/Solution/Day-033.cpp:
--------------------------------------------------------------------------------
1 | class MedianFinder {
2 | priority_queue, greater>mn; // min of greater values
3 | priority_queue, less>mx; // max of lesser values
4 | public:
5 | MedianFinder() {
6 |
7 | }
8 |
9 | void addNum(int num) {
10 | if (mn.empty()) mn.push(num);
11 | else if (mx.empty()){
12 | if (mn.top() > num) {
13 | mx.push(num);
14 | } else {
15 | mx.push(mn.top());
16 | mn.pop();
17 | mn.push(num);
18 | }
19 | }
20 | else if (num < mx.top()) mx.push(num);
21 | else mn.push(num);
22 |
23 | if ((int)mx.size() -(int) mn.size() >= 2) {
24 | mn.push(mx.top());
25 | mx.pop();
26 | } else if ((int)mn.size() -(int) mx.size() >=2) {
27 | mx.push(mn.top());
28 | mn.pop();
29 | }
30 | }
31 |
32 | double findMedian() {
33 | if ((mn.size() + mx.size())&1) {
34 | if (mn.size() > mx.size()) {
35 | return mn.top();
36 | } else{
37 | return mx.top();
38 | }
39 | } else {
40 | return (mx.top() + mn.top())/2.0;
41 | }
42 | }
43 | };
44 |
45 | /**
46 | * Your MedianFinder object will be instantiated and called as such:
47 | * MedianFinder* obj = new MedianFinder();
48 | * obj->addNum(num);
49 | * double param_2 = obj->findMedian();
50 | */
51 |
--------------------------------------------------------------------------------
/Solution/Day-034.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | This problem was asked by Quora.
3 |
4 | Given a string, find the palindrome that can be made by inserting the fewest
5 | number of characters as possible anywhere in the word.
6 |
7 | If there is more than one palindrome of minimum length that can be made,
8 | return the lexicographically earliest one (the first one alphabetically).
9 |
10 | For example, given the string "race", you should return "ecarace",
11 | since we can add three letters to it (which is the smallest amount to make a palindrome).
12 |
13 | There are seven other palindromes that can be made from "race" by adding three letters, but "ecarace" comes first alphabetically.
14 |
15 | As another example, given the string "google", you should return "elgoogle".
16 |
17 | */
18 |
19 | #include
20 | using namespace std;
21 | bool palin(string s){
22 | int i=0 , j=(int)s.size()-1;
23 | while(iright.size()){
41 | return right;
42 | }else if(left.size()right){
46 | return right;
47 | }else{
48 | return left;
49 | }
50 | }
51 | }
52 | int main(void){
53 | string s , rev;
54 | cin >> s;
55 | cout<& nums) {
4 | const int n = (int)nums.size();
5 | int one=0,zero=0, two=n-1;
6 | while(one <= two){
7 | if(nums[one] == 2 && one < two){
8 | swap(nums[two] , nums[one]);
9 | --two;
10 | }else if(nums[one] == 0 && one > zero){
11 | swap(nums[zero] , nums[one]);
12 | ++zero;
13 | }else{
14 | ++one;
15 | }
16 | }
17 | }
18 | };
19 |
--------------------------------------------------------------------------------
/Solution/Day-036.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Definition for a binary tree node.
3 | * struct TreeNode {
4 | * int val;
5 | * TreeNode *left;
6 | * TreeNode *right;
7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {}
8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10 | * };
11 | */
12 | class Solution {
13 | int ans;
14 | bool isAnsFound;
15 | int tempCounter;
16 | void getans(TreeNode* root , int k){
17 | if(isAnsFound){
18 | return;
19 | }
20 | if(root->right!=nullptr){
21 | getans(root->right , k);
22 | }
23 | tempCounter++;
24 | if(k==tempCounter){
25 | isAnsFound = true;
26 | ans = root -> val;
27 | tempCounter++;
28 | return;
29 | }
30 | if(!isAnsFound && root->left != nullptr){
31 | getans(root->left , k);
32 | }
33 | if(k==tempCounter){
34 | isAnsFound = true;
35 | ans = root -> val;
36 | return;
37 | }
38 | }
39 | public:
40 | int kthSmallest(TreeNode* root) {
41 | tempCounter =0;
42 | ans = -1;
43 | isAnsFound = false;
44 | getans(root , 2);
45 | return ans;
46 | }
47 | };
48 |
--------------------------------------------------------------------------------
/Solution/Day-037.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | This problem was asked by Google.
3 |
4 | The power set of a set is the set of all its subsets. Write a function that, given a set, generates its power set.
5 |
6 | For example, given the set {1, 2, 3}, it should return {{}, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}}.
7 |
8 | You may also use a list or array to represent a set.
9 |
10 | */
11 |
12 |
13 | /*
14 | * you could use vector < vector > and sort them in last to get a list of list
15 | *
16 | */
17 | #include
18 | using namespace std;
19 | int main(int argc , char *argv[]){
20 | // write you code here
21 | vector< int > arr {1 , 2 , 3};
22 | int bit = (1 << (int)arr.size());
23 | for(int i{}; i < bit; ++i){
24 | vector< int > element;
25 | for(int j=0; j<(int)arr.size(); ++j){
26 | if(i&(1<
13 | using namespace std;
14 | bitset<40>column , d1 , d2; // d1 -> diagonal 1 & d2 -> diagonal 2
15 | int NQueen(int n , int current){
16 | if(current == n){
17 | return 1;
18 | }
19 |
20 | int ways{};
21 |
22 | for(int i=0; i>n;
39 | cout << NQueen(n , 0) << '\n';
40 | return 0;
41 | }
42 |
--------------------------------------------------------------------------------
/Solution/Day-040.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | This problem was asked by Google.
3 |
4 | Given an array of integers where every integer occurs three times except for one integer,
5 | which only occurs once, find and return the non-duplicated integer.
6 |
7 | For example, given [6, 1, 3, 3, 3, 6, 6], return 1. Given [13, 19, 13, 13], return 19.
8 |
9 | Do this in O(N) time and O(1) space.
10 | */
11 | #include
12 | using namespace std;
13 | int main(int argc , char *argv[]){
14 | // write you code here
15 | vector< int > array_1 { 6, 1, 3, 3, 3, 6, 6 };
16 | vector< int > array_2 { 13, 19, 13, 13 };
17 | vector< int > bit(30, 0); // if values goes higher then take a largest bit - array
18 | for(auto&itr: array_1){
19 | for(int i=0; i < 30; ++i){
20 | if(itr&(1<
2 | using namespace std;
3 | vectorans;
4 | bool get_sum(int k , vector&arr , int current_index){
5 | if(current_index>(int)arr.size()){
6 | return false;
7 | }
8 | if(k==0){
9 | return true;
10 | }
11 | if(k<0){
12 | return false;
13 | }
14 | if(get_sum(k-arr[current_index],arr,current_index+1)){
15 | ans.push_back(arr[current_index]);
16 | return true;
17 | }else {
18 | return get_sum(k,arr,current_index+1);
19 | }
20 | }
21 | int main(int argc , char *argv[]){
22 | // write you code here
23 | int k = 24;
24 | vectorarr{12 , 1 , 61 , 5 , 9 , 2};
25 | ans.clear();
26 | if(get_sum(k , arr , 0)){
27 | for(auto&itr:ans){
28 | cout<
2 | using namespace std;
3 | class Stack{
4 | private:
5 | vectortemp_stack;
6 | vectormax_stack;
7 | public:
8 | void push(int x){
9 | temp_stack.push_back(x);
10 | if(max_stack.size()==0){
11 | max_stack.push_back(x);
12 | }else{
13 | int t = move(max_stack.back());
14 | max_stack.push_back(std::max(t,x));
15 | }
16 | }
17 | void pop(void){
18 | try{
19 | if(temp_stack.size()==0){
20 | throw "Emtpy Stack";
21 | }
22 | temp_stack.pop_back();
23 | max_stack.pop_back();
24 | }catch(const char *s){
25 | cout< A[j] but i < j. That is, a smaller element appears after a larger element.
6 |
7 | Given an array, count the number of inversions it has. Do this faster than O(N^2) time.
8 |
9 | You may assume each element in the array is distinct.
10 |
11 | For example, a sorted list has zero inversions. The array [2, 4, 1, 3, 5] has three inversions: (2, 1), (4, 1), and (4, 3).
12 |
13 | The array [5, 4, 3, 2, 1] has ten inversions: every distinct pair forms an inversion.
14 |
15 |
16 | */
17 | #include
18 | using namespace std;
19 | int inv_counter{};
20 | void merge(vector&v, int start , int end){
21 | vectortemp(v.size(),0);
22 | int mid = (start+end)/2;
23 | int i{start} , j{(start+end)/2+1} ,k{};
24 | while(i<=mid && j<=end){
25 | if(v[i]>v[j]){
26 | inv_counter+=(mid-i+1);
27 | temp[k++]=v[j++];
28 | }else{
29 | temp[k++]=v[i++];
30 | }
31 | }
32 | while(i<=mid){
33 | temp[k++] = v[i++];
34 | }
35 | while(j<=end){
36 | temp[k++] = v[j++];
37 | }
38 | k = 0;
39 | for(int i{start};i<=end;++i){
40 | v[i] = temp[k++];
41 | }
42 | }
43 | void count_inversion(vector&v , int start , int end){
44 | if(start>=end){
45 | return;
46 | }
47 | int mid{(start+end)/2};
48 | count_inversion(v,start,mid);
49 | count_inversion(v,mid+1,end);
50 | merge(v,start,end);
51 | }
52 | int main(int argc , char *argv[]){
53 | // write you code here
54 | vectorv{2,4,1,3,5};
55 | count_inversion(v,0,4);
56 | cout<
11 | using namespace std;
12 | inline bool palin(string ans){
13 | int i=0 , j=(int)ans.size()-1;
14 | while(iright.size()){
31 | return left;
32 | }else{
33 | return right;
34 | }
35 | assert(false);
36 | }
37 | int main(void){
38 | string s;
39 | cin>>s;
40 | cout<
10 | using namespace std;
11 | int main(void){
12 | vectorarr = { 9, 11, 8, 5, 7, 10 };
13 | int buy=arr[0];
14 | int profit{};
15 | for(auto&sell:arr){
16 | profit = max(profit , sell-buy);
17 | buy = min(buy , sell); // we will buy current stock as it have minimum price then the current we have
18 | // and try to sell this one
19 | // why this works :
20 | // let's say we buy 9 and sell it at price of 11 then we get a profit of 2
21 | // but next largest value we can see is 10 so we are going to sell our stock [9] at price of 10 by making a profit of 1
22 | // what if we buy a stock which have minimum price value and also appear before the price at which we want to sell the current stock.
23 | // so to automate this current algorithm will work fine
24 | }
25 | cout<
24 | using namespace std;
25 | struct Node{
26 | char value;
27 | Node* leftNode;
28 | Node* rightNode;
29 | };
30 | Node* getNewNode(int x){
31 | Node* temp = new Node();
32 | temp->value = x;
33 | temp->leftNode = nullptr;
34 | temp->rightNode = nullptr;
35 | return temp;
36 | }
37 | int preOrderIndex;
38 | Node* formTree(vector&preorder , vector&inorder , int start , int end , unordered_map&index){
39 | if(start>end){
40 | return nullptr;
41 | }
42 |
43 | Node* root = getNewNode(preorder[ preOrderIndex ]); // set the root
44 | if(start==end){
45 | preOrderIndex++;
46 | return root;
47 | }
48 | // now we have to look for next root which we can find with index
49 | int middle = index[ preorder[ preOrderIndex++ ] ];
50 |
51 | root->leftNode = formTree(preorder , inorder , start , middle-1 , index);
52 | root->rightNode = formTree(preorder , inorder , middle+1 , end , index);
53 |
54 | return root;
55 | }
56 | Node* binaryTree(vector&preorder , vector&inorder) {
57 | preOrderIndex = 0;
58 | unordered_mapindex; // for fast search
59 | for( int i{}; i < (int) inorder.size() ; ++i ){
60 | index[ inorder[ i ] ] = i;
61 | }
62 | return formTree(preorder , inorder, 0, (int)inorder.size()-1 , index);
63 | }
64 |
65 | void preOrder(Node*root){
66 | if(root==nullptr){
67 | return;
68 | }
69 | cout<value<<' ';
70 | preOrder(root->leftNode);
71 | preOrder(root->rightNode);
72 | return;
73 | }
74 |
75 | int main(void){
76 | vector preorder { 'a', 'b', 'd', 'e', 'c', 'f', 'g' };
77 | vector inorder { 'd', 'b', 'e', 'a', 'f', 'c', 'g' };
78 | Node* root = binaryTree(preorder , inorder);
79 | preOrder(root);
80 | cout<<'\n';
81 | return 0;
82 | }
83 |
--------------------------------------------------------------------------------
/Solution/Day-049.cpp:
--------------------------------------------------------------------------------
1 | /*
2 |
3 | This problem was asked by Amazon.
4 |
5 | Given an array of numbers, find the maximum sum of any contiguous subarray of the array.
6 |
7 | For example, given the array [34, -50, 42, 14, -5, 86], the maximum sum would be 137, since we would take elements 42, 14, -5, and 86.
8 |
9 | Given the array [-5, -1, -8, -9], the maximum sum would be 0, since we would not take any elements.
10 |
11 | Do this in O(N) time.
12 |
13 | */
14 | #include
15 | using namespace std;
16 | int getMaxSum(vector & arr){
17 | int max_sum{} , temp_sum{};
18 | for(auto&itr:arr){
19 | temp_sum+=itr;
20 | if(temp_sum>0){
21 | max_sum = max(temp_sum , max_sum);
22 | }else{
23 | temp_sum = 0;
24 | }
25 | }
26 | return max_sum;
27 | }
28 | int main(void){
29 |
30 | vector arr1 = { 34, -50, 42, 14, -5, 86 };
31 | vector arr2 = { -5, -1, -8, -9 };
32 | cout<< getMaxSum(arr1) << '\n';
33 | cout<< getMaxSum(arr2) << '\n';
34 | return 0;
35 | }
36 |
--------------------------------------------------------------------------------
/Solution/Day-050.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | This problem was asked by Microsoft.
3 |
4 | Suppose an arithmetic expression is given as a binary tree. Each leaf is an integer and each internal node is one of '+', '−', '∗', or '/'.
5 |
6 | Given the root to such a tree, write a function to evaluate it.
7 |
8 | For example, given the following tree:
9 |
10 | *
11 | / \
12 | + +
13 | / \ / \
14 | 3 2 4 5
15 |
16 | You should return 45, as it is (3 + 2) * (4 + 5).
17 | */
18 | #include
19 | using namespace std;
20 | struct Node{
21 | string value;
22 | Node* left;
23 | Node* right;
24 |
25 | Node(): left{nullptr} , right{nullptr} { };
26 | };
27 |
28 | double solExpression( Node* root ) {
29 | if(root == nullptr){
30 | return 0.0f;
31 | }
32 |
33 | double leftAns = solExpression(root -> left);
34 | double rightAns = solExpression(root -> right);
35 |
36 | if(root -> value == "+"){
37 | return leftAns + rightAns;
38 | }
39 |
40 | if(root -> value == "-"){
41 | return leftAns - rightAns;
42 | }
43 |
44 | if(root -> value == "*"){
45 | return leftAns * rightAns;
46 | }
47 |
48 | if(root -> value == "/"){
49 | return leftAns / rightAns;
50 | }
51 |
52 | return std::stof(root->value);
53 |
54 | }
55 |
56 | int main(void){
57 | Node*root = new Node();
58 | root -> value = "*";
59 |
60 | root -> left = new Node();
61 | root -> left -> value = "+";
62 |
63 | root -> left -> left = new Node();
64 | root -> left -> left -> value = "3";
65 |
66 | root -> left -> right = new Node();
67 | root -> left -> right -> value = "2";
68 |
69 | root -> right = new Node();
70 | root -> right -> value = "+";
71 |
72 | root -> right -> left = new Node();
73 | root -> right -> left -> value = "4";
74 |
75 | root -> right -> right = new Node();
76 | root -> right -> right -> value = "5";
77 |
78 |
79 | double ans = solExpression(root);
80 |
81 | if(ans - (int)ans==0){
82 | cout << ans ;
83 | }else{
84 | cout << fixed << setprecision(2) << ans;
85 | }
86 | cout << '\n';
87 |
88 | return 0;
89 | }
90 |
--------------------------------------------------------------------------------
/Solution/Day-051.cpp:
--------------------------------------------------------------------------------
1 | /*
2 |
3 | This problem was asked by Facebook.
4 |
5 | Given a function that generates perfectly random numbers between 1 and k (inclusive),
6 | where k is an input, write a function that shuffles a deck of cards represented as an array using only swaps.
7 |
8 | It should run in O(N) time.
9 |
10 | Hint: Make sure each one of the 52! permutations of the deck is equally likely.
11 |
12 | */
13 | #include
14 | using namespace std;
15 | int main(void){
16 | srand(time(NULL));
17 |
18 | vector v = {1 , 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
19 |
20 | for(int i=0;i<(int)v.size()-1;++i){
21 | int new_position = rand()%(v.size()-i-1);
22 | swap(v[i] , v[new_position]);
23 | }
24 |
25 | for(auto&itr:v){
26 | cout<
16 | using namespace std;
17 |
18 | class LRUCache {
19 | list>deQ; // take it as dequeue {key , value}
20 | unordered_map>::iterator> keyValue;
21 | int Capacity;
22 | public:
23 |
24 | LRUCache(int capacity):Capacity(capacity){}
25 |
26 | int get(int key) {
27 | if(keyValue.find(key)==keyValue.end()){
28 | return -1;
29 | }else{
30 | int value = keyValue[key]->second;
31 | deQ.erase(keyValue[key]);
32 | deQ.push_front(make_pair(key , value));
33 | keyValue[key] = deQ.begin();
34 | return value;
35 | }
36 | }
37 | void set(int key, int value) {
38 | if(keyValue.find(key)!=keyValue.end()){
39 | // key is already present
40 | deQ.erase(keyValue[key]);
41 | }else if(Capacity == deQ.size()){
42 | keyValue.erase(deQ.back().first);
43 | //cout<
11 | using namespace std;
12 | template
13 | struct Queue{
14 | private:
15 | stack< Object > enq;
16 | stack< Object > deq;
17 | public:
18 | void enqueue( Object value ) {
19 | enq.push( value );
20 | }
21 |
22 | Object dequeue( void ) {
23 | while( !enq.empty() ) {
24 | deq.push( enq.top() );
25 | enq.pop();
26 | }
27 |
28 | Object element = deq.top();
29 | deq.pop();
30 |
31 | while( !deq.empty() ) {
32 | enq.push( deq.top() );
33 | deq.pop();
34 | }
35 |
36 | return element;
37 | }
38 |
39 | };
40 | int main(void){
41 | Queue q;
42 | q.enqueue(100);
43 | q.enqueue(200);
44 | cout<
38 | using namespace std;
39 | int grid[9][9];
40 | pair getFreeBox( void ) {
41 | for(auto i = 0; i< 9; ++i){
42 | for(auto j=0; j<9; ++j){
43 | if(grid[i][j] == 0){
44 | return make_pair(i,j);
45 | }
46 | }
47 | }
48 | return make_pair(-1 , -1);
49 | }
50 | bool isToPlace(pair&p , int k){
51 | // is row safe
52 | int row= p.first;
53 | int col = p.second;
54 | for(int i=0;i<9; ++i){
55 | if(grid[row][i] == k){
56 | return false;
57 | }
58 | }
59 | for(int i=0;i<9; ++i){
60 | if(grid[i][col]==k){
61 | return false;
62 | }
63 | }
64 | // is box safe
65 | int x = row - (row % 3);
66 | int y = col - (col % 3);
67 | for(auto i = 0; i<3; ++i){
68 | for(auto j=0; j<3; ++j){
69 | if(grid[i+x][j+y] == k){
70 | return false;
71 | }
72 | }
73 | }
74 | return true;
75 | }
76 | bool sol(void){
77 | pairp = getFreeBox();
78 | if(p.first == -1 ){
79 | return true;
80 | }
81 | for(int i=1; i<=9; ++i){
82 | if(isToPlace(p , i)){
83 | grid[p.first][p.second] = i;
84 | if(sol()){
85 | return true;
86 | }
87 | grid[p.first][p.second] = 0;
88 | }
89 | }
90 | return false;
91 | }
92 | int main(void){
93 | freopen("input" , "r" , stdin);
94 | for(auto i = 0; i<9; ++i){
95 | for(auto j = 0; j<9; ++j){
96 | cin>>grid[i][j];
97 | }
98 | }
99 | if(sol()){
100 | for(auto i = 0; i<9; ++i){
101 | for(auto j = 0; j<9; ++j){
102 | cout<
12 | using namespace std;
13 |
14 | class Solution {
15 | unordered_mapdec;
16 | unordered_mapenc;
17 | int index = 0;
18 | public:
19 |
20 | string encode(string longUrl) {
21 | enc[longUrl]=index;
22 | dec[to_string(index)]= longUrl;
23 | ++index;
24 | return to_string(index-1);
25 | }
26 |
27 | string decode(string shortUrl) {
28 | return dec[shortUrl];
29 | }
30 | };
31 |
32 | int main(void){
33 | return 0;
34 | }
35 |
--------------------------------------------------------------------------------
/Solution/Day-056.cpp:
--------------------------------------------------------------------------------
1 | /*
2 |
3 | This problem was asked by Google.
4 |
5 | Given an undirected graph represented as an adjacency matrix and an integer k,
6 |
7 | write a function to determine whether each vertex in the graph can be colored such that no two adjacent vertices share the same color using at most k colors.
8 |
9 |
10 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
11 |
12 | This seems to be a NP problem. I tried to find answer on web
13 | only information i got is , this problem is checking about if given number
14 | k is less then or equal chromatic number of given graph;
15 |
16 | It tried to find some better solution but didn't get any ;(
17 |
18 | if you find any better approach please mail me: offamitkumar@gmail.com
19 |
20 | :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
21 |
22 | */
23 | #include
24 | using namespace std;
25 | vectorvisited; // might possible that graph not connected
26 | vectorcolor; // stored the color of each node , -1 represent no coloring is done yet
27 | bool iscolorable(vector>&graph , int currentNode , const int &k){
28 | visited[currentNode] = true;
29 | for(int itr=0;itr<4;++itr){
30 | if(graph[currentNode][itr] == 0){
31 | continue;
32 | }
33 | if(color[itr] == color[currentNode]){ // if color match with adjacent vertex
34 | return false;
35 | }
36 | if(color[itr] == -1){ // let's color it
37 | for(int i=0;i > graph = {
59 | {0, 1, 1, 1},
60 | {1, 0, 1, 1},
61 | {1, 1, 0, 1},
62 | {1, 1, 1, 0}
63 | };
64 | int k = 3; // number of colors
65 | bool ispossible = true;
66 | for(int i=0;i
14 | using namespace std;
15 | /*
16 | * we will choose each word greedily.
17 | */
18 | vectorans;
19 | bool sol(string &s , const int &k){
20 | stringstream ss(s);
21 | string temp;
22 | while(ss>>s){
23 | if(s.size()>k) return false; // this can't be adjusted to given word limits
24 | if(temp.size()+s.size()+1<=k){
25 | if(temp.size()==0){
26 | temp+=s;
27 | }else{
28 | temp+=" "+s;
29 | }
30 | }else{
31 | ans.push_back(temp);
32 | temp =s;
33 | }
34 | }
35 | if(temp.size()>0){
36 | ans.push_back(temp);
37 | }
38 | return true; // if answer possible then return true
39 | }
40 | int main(void){
41 | string s = "the quick brown fox jumps over the lazy dog";
42 | int k = 10;
43 | if(sol(s ,k)){
44 | for(auto &itr:ans){
45 | cout<
20 | using namespace std;
21 | class Solution {
22 | public:
23 | int search(vector& arr, int target) {
24 | if(!arr.size())
25 | return -1; // consider -1 as NULL here
26 | auto get = [&](int start , int end)->int{
27 | int mid ;
28 | if(arr[start] == target){
29 | return start;
30 | }else if(arr[end] == target){
31 | return end;
32 | }
33 | while(start<=end){
34 | mid = (start+end)/2;
35 | if(arr[mid] == target){
36 | return mid;
37 | }
38 | if(arr[start] <= arr[mid]){ // we assumed that this is sorted
39 | if(target>=arr[start] and target<=arr[mid]){
40 | end = mid-1;
41 | }else{
42 | start = mid+1;
43 | }
44 | }else if(arr[mid]<=target and target<=arr[end]){ // we now assume that [mid , end] portion is sorted and target value is there
45 | start = mid+1;
46 | }else{ // [start , mid] is not sorted and if [start , mid] isn't sorted than we claim that [mid+1 , end] will be sorted and if target isn't present there , now we will look for the target in unsorted portion
47 | end = mid-1;
48 | }
49 | }
50 | return -1;
51 |
52 | };
53 | return get(0 , arr.size()-1);
54 | }
55 | };
56 |
57 | int main(void){
58 | Solution s1;
59 | vectorv = {4,5,6,7,0,1,2};
60 | int target = 0;
61 | assert(s1.search(v , target) == 4);
62 | return 0;
63 | }
64 |
--------------------------------------------------------------------------------
/Solution/Day-059.cpp:
--------------------------------------------------------------------------------
1 | //
2 | This problem was asked by Google.
3 |
4 | Implement a file syncing algorithm for two computers over a low-bandwidth network. What if we know the files in the two computers are mostly the same?
5 |
6 | // this question is based on implementation of merkle tree .
7 | // you can check about it on google.
8 | // it might get update in future.
9 |
--------------------------------------------------------------------------------
/Solution/Day-060.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | This problem was asked by Facebook.
3 |
4 | Given a multiset of integers, return whether it can be partitioned into two subsets whose sums are the same.
5 |
6 | For example, given the multiset {15, 5, 20, 10, 35, 15, 10}, it would return true, since we can split it up into {15, 5, 10, 15, 10} and {20, 35}, which both add up to 55.
7 |
8 | Given the multiset {15, 5, 20, 10, 35}, it would return false, since we can't split it up into two subsets that add up to the same sum.
9 |
10 | *
11 | */
12 | class Solution {
13 | unordered_mapdp;
14 | bool dfs(vector&n , int index , int target){
15 | if(dp.find(target)!=dp.end()){
16 | return dp[target];
17 | }
18 | if(index>=n.size() or target<0)return false;
19 | dp[target] = false;
20 | for(auto i=index;i<(int)n.size(); ++i){
21 | if(dfs(n , i+1 , target - n[i])){
22 | dp[target] = true;
23 | return dp[target];
24 | }
25 | }
26 | return dp[target];
27 | }
28 | public:
29 | bool canPartition(vector& nums) {
30 | int sum = accumulate(nums.begin() , nums.end() , 0);
31 | if(sum%2 or nums.size() == 1){
32 | return false;
33 | }else if(nums.size()==0){
34 | return true;
35 | }
36 | sort(nums.begin() , nums.end());
37 | dp[0] = true;
38 | return dfs(nums , 0 , sum/2);
39 | }
40 | };
41 |
--------------------------------------------------------------------------------
/Solution/Day-061.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | This problem was asked by Google.
3 |
4 | Implement integer exponentiation. That is, implement the pow(x, y) function, where x and y are integers and returns x^y.
5 |
6 | Do this faster than the naive method of repeated multiplication.
7 |
8 | For example, pow(2, 10) should return 1024.
9 | */
10 |
11 | class Solution {
12 | public:
13 | double myPow(double x, long long n) {
14 | double ans =1.0f;
15 | if(n>0){
16 | while(n){
17 | //double a =1;
18 | if(n&1){
19 | ans*=x;
20 | }
21 | x*=x;
22 | n>>=1;
23 | }
24 | }else{
25 | n = abs(n);
26 | while(n){
27 | if(n&1){
28 | ans/=x;
29 | }
30 | x*=x;
31 | n>>=1;
32 | cout<>grid(m , vector(n,0));
24 | grid[0][0] = 1;
25 | for(int i=0;i>visited;
22 | vector>dir{{-1, 0}, {0, -1}, {0, 1},{1, 0}};
23 | int row , col;
24 | bool dfs(vector>&grid , string &word ,int r , int c , int index = 1){
25 | if(index == (int)word.size()){
26 | return true;
27 | }
28 | if(index > (int)word.size()){
29 | return false;
30 | }
31 | if(r>=row || c >=col){
32 | return false;
33 | }
34 | visited[r][c] = true;
35 | for(auto &itr: dir){
36 | if(r+itr.first <0 or r+itr.first>=row){
37 | continue;
38 | }
39 | if(c+itr.second <0 or c+itr.second>=col){
40 | continue;
41 | }
42 | if(grid[r+itr.first][c+itr.second]==word[index]){
43 | if(visited[r+itr.first][c+itr.second]==false && dfs(grid , word , r+itr.first, c+itr.second , index+1)){
44 | return true;
45 | }
46 | }
47 | }
48 | visited[r][c] = false;
49 | return false;
50 | }
51 | public:
52 | bool exist(vector>& board, string word) {
53 | row = board.size();
54 | col = board[0].size();
55 | visited.resize(row , vector(col , false));
56 | for(int i=0;i
21 | using namespace std;
22 | vector>jump{{2,1},{2,-1},{-2,1},{-2,-1},{1,2},{-1,2},{1,-2},{-1,-2}};
23 | vector>grid;
24 | bool valid(int x , int y , int n){
25 | if(x>n || y>n || x<1 || y<1){
26 | return false;
27 | }
28 | if(grid[x][y] == true )
29 | return false;
30 | return true;
31 | }
32 | long long count_ways(int x , int y , int n ,int move_number = 1){
33 | if(move_number == n*n){
34 | return 1;
35 | }
36 | int ways_counter{};
37 | if(x>n || y>n || x<1 || y<1){
38 | return ways_counter;
39 | }
40 | grid[x][y] = true;
41 | for(int k=0;k<(int)jump.size();++k){
42 | if(valid(x+jump.at(k).first , y+jump.at(k).second , n)){
43 | ways_counter+=count_ways(x+jump.at(k).first , y+jump.at(k).second , n , move_number+1 );
44 | }
45 | }
46 | grid[x][y] = false;
47 | return ways_counter;
48 | }
49 | int main(void){
50 | int n; cin>>n;
51 | grid.assign(n+1 , vector(n+1,false));
52 | long long counter{};
53 | for(int i=1;i<=n;++i){
54 | for(int j=1;j<=n;++j){
55 | counter+=count_ways(i,j,n);
56 | }
57 | }
58 | cout<> dir= {
41 | { 0, 1} ,
42 | { 1, 0} ,
43 | {0, -1} ,
44 | {-1, 0}
45 | };
46 | vector>visited;
47 | public:
48 | vector spiralOrder(vector>& matrix) {
49 | vectorans;
50 | if(matrix.size()==0 || matrix[0].size() == 0){
51 | return ans;
52 | }
53 | int C = matrix[0].size();
54 | int R = matrix.size();
55 |
56 | visited.assign(R , vector(C , false));
57 | int r,c,d;
58 | r = c = d = 0;
59 |
60 | for(int tr,tc, _=0;_<(R*C);++_){
61 | tr = r + dir[d].first;
62 | tc = c + dir[d].second;
63 | ans.push_back(matrix[r][c]);
64 | visited[r][c] = true;
65 | if(tc>=0 and tc=0 and tr p1 , pair< int , int> p2 ) {
3 | if(p1.first + p1.second == p2.first + p2.second){
4 | return true;
5 | }
6 | if(p1.first - p1.second == p2.first - p2.second){
7 | return true;
8 | }
9 | return false;
10 | }
11 | public:
12 | /*
13 | * else solution can be if we create a map of every diagonal then for every co-ordinated
14 | * we will update the particular key value, at last we can count the pair by simple
15 | * combination trick.
16 | * Time Complexity : O(N*log(N)+N); ( N -> traversing all pair of co-ordinated )
17 | * Space Complexity: O(N)
18 | *
19 | */
20 | int getAttackingPair( vector< pair< int , int > > &bishop ) { // O(N^2)
21 | int n = static_cast< int >( bishop.size() );
22 | int counter{};
23 | for( int i{}; i < n; ++i ) {
24 | for( int j = i+1; j < n; ++j ) {
25 | if( attacking(bishop[i] , bishop[j]) ) {
26 | ++counter;
27 | }
28 | }
29 | }
30 | return counter;
31 | }
32 | };
33 |
--------------------------------------------------------------------------------
/Solution/Day-069.cpp:
--------------------------------------------------------------------------------
1 | class Solution {
2 | public:
3 | int maximumProduct(vector& nums) {
4 | int max1 , max2 , max3;
5 | int min1 , min2;
6 | max1 = max2 = max3 = numeric_limits::min();
7 | min1 = min2 = numeric_limits::max();
8 | for(auto&itr:nums){
9 | if(itr>=max1){
10 | max3 = max2;
11 | max2 = max1;
12 | max1 = itr;
13 | }else if(itr>=max2){
14 | max3 = max2;
15 | max2 = itr;
16 | }else if(itr>=max3){
17 | max3 = itr;
18 | }
19 | if(itr<=min1){
20 | min2 = min1;
21 | min1 = itr;
22 | }else if(itr<=min2){
23 | min2 = itr;
24 | }
25 | }
26 | return std::max({max1*max2*max3 , min1*min2*max1});
27 | }
28 | };
29 |
--------------------------------------------------------------------------------
/Solution/Day-070.cpp:
--------------------------------------------------------------------------------
1 | class Solution{
2 | public:
3 | int getNextPerfectNumber(int n){
4 | auto temp_sum = 0;
5 | auto temp = n;
6 | while(n){
7 | temp_sum += (n%10);
8 | n /= 10;
9 | }
10 | return temp*10+(10 - temp_sum);
11 | }
12 | };
13 |
--------------------------------------------------------------------------------
/Solution/Day-071.cpp:
--------------------------------------------------------------------------------
1 | class Solution{
2 | private:
3 | int rand5(void)const noexcept{
4 | srand(time(NULL));
5 | return rand()%5+1;
6 | }
7 | public:
8 | int rand7(void)noexcept{
9 | int generatedNumber = -1;
10 | while(true){
11 | generatedNumber = (rand5()-1)*5+rand5();
12 | if(generatedNumber<=21){
13 | return generatedNumber%7+1;
14 | }
15 | }
16 | }
17 | };
18 |
--------------------------------------------------------------------------------
/Solution/Day-072.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 | class InfinitePathException:std::exception{
4 | public:
5 | const char* what() const throw(){
6 | return "Detected Cycle, No Answer Possible";
7 | }
8 | };
9 |
10 | class Solution{
11 | private:
12 | mapfreqCounter;
13 | setvisited;
14 | map>graph;
15 | int maxPathValue;
16 | void createGraph(vector>&edges){
17 | for(auto&edge:edges){
18 | auto&& source = edge[0];
19 | auto&& destination = edge[1];
20 | graph[ source ].push_back(destination);
21 | }
22 | }
23 | void dfs(const string&pathCharacters , int currentNode){
24 | freqCounter[ pathCharacters[currentNode] ]++;
25 | visited.insert(currentNode);
26 | for(auto&itr:graph[currentNode]){
27 | if(visited.count(itr)){
28 | throw InfinitePathException();
29 | }else{
30 | dfs(pathCharacters , itr);
31 | }
32 | }
33 | maxPathValue = std::max(maxPathValue , freqCounter[ pathCharacters[ currentNode ] ]);
34 | visited.erase(currentNode);
35 | freqCounter[ pathCharacters[currentNode] ]--;
36 | }
37 | public:
38 | int getMaxValuePath(const string &pathCharacters , vector>&edge){
39 | maxPathValue = numeric_limits::min();
40 | createGraph(edge);
41 | for(auto&itr:graph){ // we need to check dfs from each node as there can be situation like A(1)->A(0)->A(2)->A(3)
42 | try{
43 | dfs(pathCharacters , itr.first);
44 | }catch(...){
45 | throw;
46 | }
47 | }
48 | if(maxPathValue == numeric_limits::min()){
49 | assert(false);
50 | }else{
51 | return maxPathValue;
52 | }
53 | }
54 | };
55 | int main(void){
56 |
57 | string s = "ABACA";
58 | vector>edges1;
59 | edges1.push_back({0,1});
60 | edges1.push_back({0,2});
61 | edges1.push_back({2,3});
62 | edges1.push_back({3,4});
63 | Solution s1;
64 | try{
65 | cout<>edges2;
71 | s = "A";
72 | edges2.push_back({0,0});
73 | try{
74 | cout<next==nullptr){
15 | return head;
16 | }
17 | ListNode* oldHead = head , *newHead = head , *newEnd = head ,*nextNode = head ->next;
18 | while(nextNode!=nullptr){
19 | newHead = nextNode;
20 | nextNode = newHead ->next;
21 | newHead->next = oldHead;
22 | newEnd->next = nextNode;
23 | oldHead = newHead;
24 | }
25 | return newHead;
26 | }
27 | };
28 |
--------------------------------------------------------------------------------
/Solution/Day-074.cpp:
--------------------------------------------------------------------------------
1 | class Solution{
2 | private:
3 | int counter{};
4 | public:
5 | int getAppearingValue( int n , int x ) {
6 | counter = 0;
7 | for(int i=1; i<=n and i<=sqrt(x);++i){
8 | if(!(x%i)){
9 | counter+=(x/i <= n);
10 | counter+=(x/i!=i and x/i<=n and i<=n);
11 | }
12 | }
13 | return counter;
14 | }
15 | };
16 |
--------------------------------------------------------------------------------
/Solution/Day-075.cpp:
--------------------------------------------------------------------------------
1 | class Solution {
2 | public:
3 | int lengthOfLIS(vector& nums) {
4 | const int array_size = static_cast(nums.size());
5 | if(!array_size){
6 | return 0;
7 | }
8 | vectordp(array_size , 1);
9 | for(int i=0; inums[j])
12 | dp[i] = max(dp[j]+1 , dp[i]);
13 | }
14 | }
15 | return *max_element(dp.begin() , dp.end());
16 | }
17 | };
18 |
--------------------------------------------------------------------------------
/Solution/Day-076.cpp:
--------------------------------------------------------------------------------
1 | class Solution {
2 | public:
3 | int minDeletionSize(vector& A) {
4 | int col = static_cast(A[0].size());
5 | int row = static_cast(A.size());
6 | int required_deletion{};
7 | for ( int c{}; c < col; ++c ) {
8 | for ( int r{}; r < row-1; ++r ) {
9 | if(A[r][c] > A[r+1][c]){
10 | ++required_deletion;
11 | break;
12 | }
13 | }
14 | }
15 | return required_deletion;
16 | }
17 | };
18 |
--------------------------------------------------------------------------------
/Solution/Day-077.cpp:
--------------------------------------------------------------------------------
1 | class Solution {
2 | static bool compare(vector&a , vector&b){
3 | return a[0] < b[0];
4 | }
5 | public:
6 | vector> merge(vector>& intervals) {
7 | sort(intervals.begin() , intervals.end() , compare);
8 | vector>res;
9 | int n = static_cast(intervals.size());
10 | for(int i=0;i=intervals[i+1][0]){
14 | current_largest_end = max(intervals[i+1][1] , current_largest_end);
15 | current_min_start = min(intervals[i+1][0] , current_min_start);
16 | ++i;
17 | }
18 | res.push_back({current_min_start , current_largest_end});
19 | }
20 | return res;
21 | }
22 | };
23 |
24 |
--------------------------------------------------------------------------------
/Solution/Day-078.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Definition for singly-linked list.
3 | * struct ListNode {
4 | * int val;
5 | * ListNode *next;
6 | * ListNode() : val(0), next(nullptr) {}
7 | * ListNode(int x) : val(x), next(nullptr) {}
8 | * ListNode(int x, ListNode *next) : val(x), next(next) {}
9 | * };
10 | */
11 | using Heap = set>;
12 | class Solution {
13 | public:
14 | ListNode* mergeKLists(vector& lists) {
15 | Heap h;
16 | ListNode* temp, *res;
17 | temp = res = nullptr;
18 | const int &k = lists.size();
19 | for (int i = 0; i < k; ++i) {
20 | if (lists[i] != nullptr) {
21 | h.insert(make_pair(lists[i] -> val, lists[i]));
22 | }
23 | }
24 | while (h.empty() == false) {
25 | ListNode* x = (*h.begin()).second;
26 | if (res == nullptr) {
27 | res = temp = x;
28 | } else {
29 | temp -> next = x;
30 | temp = temp -> next;
31 | }
32 | h.erase(h.begin());
33 | if (x -> next != nullptr) {
34 | x = x -> next;
35 | h.insert(make_pair(x -> val, x));
36 | }
37 | }
38 | return res;
39 | }
40 | };
41 |
--------------------------------------------------------------------------------
/Solution/Day-079.cpp:
--------------------------------------------------------------------------------
1 | class Solution {
2 | public:
3 | bool checkPossibility(vector& nums) {
4 | int counter{};
5 | const int n = static_cast(nums.size());
6 | for (int i = 0; i < n-1; ++i) {
7 | if(nums[i] > nums[i+1] && counter) {
8 | return false;
9 | }
10 | if(nums[i] > nums[i+1]) {
11 | if( i>0 && nums[i+1] < nums[i-1] ) {
12 | nums[i+1] = nums[i];
13 | }else{
14 | nums[i] = nums[i+1];
15 | }
16 | ++counter;
17 | }
18 | }
19 | return true;
20 | }
21 | };
22 |
23 |
--------------------------------------------------------------------------------
/Solution/Day-080.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Definition for a binary tree node.
3 | * struct TreeNode {
4 | * char val;
5 | * TreeNode *left;
6 | * TreeNode *right;
7 | * TreeNode() : val('0'), left(nullptr), right(nullptr) {}
8 | * TreeNode(char x) : val(x), left(nullptr), right(nullptr) {}
9 | * TreeNode(char x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10 | * };
11 | */
12 | class Solution {
13 | int deepest_height;
14 | char character;
15 | void getDeepestHeight(TreeNode* root, int current_height) {
16 | if (root == nullptr) {
17 | return ;
18 | }
19 | if (deepest_height < current_height) {
20 | deepest_height = current_height;
21 | character = root -> val;
22 | }
23 | getDeepestHeight(root -> right, current_height+1);
24 | getDeepestHeight(root -> left, current_height+1);
25 | }
26 | public:
27 | char maxDepth(TreeNode* root) {
28 | deepest_height=0;
29 | getDeepestHeight(root, 1);
30 | return character;
31 | }
32 | };
33 |
34 |
--------------------------------------------------------------------------------
/Solution/Day-081.cpp:
--------------------------------------------------------------------------------
1 | class Solution {
2 | vector dialer = {
3 | "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"
4 | };
5 | vectorresult;
6 | void getString(string &digits, int index, string currentString) {
7 | if (index >= digits.size()) {
8 | result.push_back(currentString);
9 | return;
10 | }
11 | for (auto & itr: dialer[digits[index]-'0']) {
12 | getString(digits, index+1, currentString+itr);
13 | }
14 | return ;
15 | }
16 | public:
17 | vector letterCombinations(string digits) {
18 | result.clear();
19 | if (digits.size() != 0) {
20 | getString(digits, 0, "");
21 | }
22 | return result;
23 | }
24 | };
25 |
26 |
--------------------------------------------------------------------------------
/Solution/Day-082.cpp:
--------------------------------------------------------------------------------
1 | class Reader{
2 | private:
3 | string raw_data;
4 | string buffer;
5 | int processed_till;
6 | string read7(){
7 | int start = processed_till; // point from where we have to read the file
8 | int end = min(processed_till+7, (int)raw_data.size());
9 | int old_start_point = start;
10 | processed_till = end; // updated till where file is read
11 | // so that next time we can start from the same point
12 | return raw_data.substr(old_start_point, end);
13 | }
14 | public:
15 | Reader(const string & refString): raw_data(refString), buffer(""), processed_till(0) { }
16 | string readN(int n) {
17 | while (int(buffer.size()) < n) {
18 | string get_char = read7();
19 | if (get_char == "") { // no more characters are remaining
20 | break;
21 | }
22 | buffer += get_char;
23 | }
24 | int end_point = min((int)buffer.size(), n);
25 | string to_be_returned = buffer.substr(0, end_point);
26 | if(end_point <= (int)buffer.size()){
27 | buffer = buffer.substr(end_point, (int)buffer.size());
28 | }
29 | return to_be_returned;
30 | }
31 | };
32 |
--------------------------------------------------------------------------------
/Solution/Day-083.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Definition for a binary tree node.
3 | * struct TreeNode {
4 | * int val;
5 | * TreeNode *left;
6 | * TreeNode *right;
7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {}
8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10 | * };
11 | */
12 | class Solution {
13 | public:
14 | TreeNode* invertTree(TreeNode* root) {
15 | if(root == nullptr) {
16 | return nullptr;
17 | }
18 | TreeNode*temp = root->left;
19 | root -> left = root -> right;
20 | root -> right = temp;
21 | invertTree(root -> left);
22 | invertTree(root -> right);
23 | return root;
24 | }
25 | };
26 |
--------------------------------------------------------------------------------
/Solution/Day-084.cpp:
--------------------------------------------------------------------------------
1 | class Solution {
2 | int row,col;
3 | vector>visited;
4 | void dfs(int i,int j, vector>&grid){
5 | if(i<0 || i>=row || j<0 || j>=col || visited[i][j] || grid[i][j]!='1'){
6 | return;
7 | }
8 | visited[i][j] = true;
9 | dfs(i+1,j,grid);
10 | dfs(i-1,j,grid);
11 | dfs(i,j+1,grid);
12 | dfs(i,j-1,grid);
13 | }
14 | public:
15 | int numIslands(vector>& grid) {
16 | row = (int)grid.size();
17 | if(!row)
18 | return 0;
19 | col = (int)grid[0].size();
20 | visited.assign(row,vector(col,false));
21 | int counter{};
22 | for(int i=0;ist;
6 | for(int i=0;i::min()){
6 | if(divisor == 1){
7 | return dividend;
8 | }else if(divisor == -1){
9 | return numeric_limits::max();
10 | }
11 | }
12 | divisor = (divisor > 0) ? -divisor : divisor;
13 | dividend = (dividend > 0) ? -dividend : dividend;
14 | int best_quotient = 0, temp=1 , quotient = divisor;
15 | while(quotient >=INT_MIN>>1 and dividend <= quotient + quotient){
16 | quotient += quotient;
17 | temp <<= 1;
18 | }
19 | while(dividend <= divisor) {
20 | if(dividend <= quotient) {
21 | dividend -= quotient;
22 | best_quotient += temp;
23 | }
24 | quotient >>=1;
25 | cout << quotient << '\n';
26 | temp >>= 1;
27 | }
28 | return sign < 0 ? -best_quotient : best_quotient;
29 | }
30 | };
31 |
32 |
--------------------------------------------------------------------------------
/Solution/Day-089.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Definition for a binary tree node.
3 | * struct TreeNode {
4 | * int val;
5 | * TreeNode *left;
6 | * TreeNode *right;
7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {}
8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10 | * };
11 | */
12 | class Solution {
13 | public:
14 | bool isValidBST(TreeNode* root, const std::optional &min_value=LLONG_MIN , const std::optional&max_value=LLONG_MAX) {
15 | if(root == nullptr){
16 | return true;
17 | }
18 | // according to DCP question binary search tree node can have equal value
19 | if(root -> val > max_value.value() || root->val < min_value.value()){
20 | return false;
21 | }
22 | return isValidBST(root->right,root->val,max_value) && isValidBST(root->left,min_value,root->val);
23 | }
24 | };
25 |
26 |
--------------------------------------------------------------------------------
/Solution/Day-090.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 | class Solution{
4 | vectorincluded;
5 | public:
6 | int generateRandomNumber(const array&excluded, const int &n) {
7 | setexclude(excluded.begin(), excluded.end());
8 | for(int i = 0;i(included.size());
14 |
15 |
16 | // random number generation
17 | std::random_device rnd;
18 | std::mt19937 rng(rnd());
19 | std::uniform_int_distribution distribution(0,inclusion_size);
20 | return included[distribution(rng)];
21 | }
22 | };
23 | int main(void){
24 | array exclude{2, 8, 11, 19, 23};
25 | Solution s1;
26 | cout << s1.generateRandomNumber(exclude, 30) << '\n';
27 | return 0;
28 | }
29 |
--------------------------------------------------------------------------------
/Solution/Day-091.py:
--------------------------------------------------------------------------------
1 | # it will print 9 , as first for loop exit at 9
2 | # so 'i' will have value 9 at the end of for loop & all lambda function use
3 | # i as their return value
4 | functions = []
5 | for i in range(10):
6 | functions.append(lambda : i)
7 | i = 0
8 | for f in functions:
9 | print(f())
10 | i = i + 1
11 |
--------------------------------------------------------------------------------
/Solution/Day-092.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 | class Solution{
4 | private:
5 | setid;
6 | mapin_deg;
7 | map>graph;
8 | vector Sort(void){
9 | vectorres;
10 | while(!id.empty()){
11 | string current = *(id.begin());
12 | id.erase(current);
13 | res.push_back(current);
14 | for(auto&itr:graph[current]){
15 | in_deg[itr]--;
16 | if(in_deg[itr]==0){
17 | id.insert(itr);
18 | }
19 | }
20 | }
21 | return res;
22 | }
23 | public:
24 | vector topologicalSort(map>&cID){
25 | // C++ map maintain sorted already so we don't have to sort the courses
26 | // If it wasn't map then we have to first sort the course , then their pre-requisites
27 | for(auto&courses:cID){
28 | if(courses.second.size() == 0){
29 | id.insert(courses.first);
30 | }else{
31 | for(auto&itr:courses.second){
32 | graph[itr].insert(courses.first);
33 | }
34 | }
35 | in_deg[courses.first] = courses.second.size();
36 | }
37 | return Sort();
38 | }
39 | };
40 | int main(void){
41 | map> cID{
42 | {"CSC300" , {"CSC100" , "CSC200"}},
43 | {"CSC200" , {"CSC100"}},
44 | {"CSC100" , {}}
45 | };
46 | Solution s1;
47 | vectorv = s1.topologicalSort(cID);
48 | for(auto&itr:v){
49 | cout << itr << ' ';
50 | }
51 | cout << '\n';
52 | return 0;
53 | }
54 |
--------------------------------------------------------------------------------
/Solution/Day-093.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Definition for binary tree
3 | *
4 | * struct TreeNode {
5 | * int val;
6 | * TreeNode *left;
7 | * TreeNode *right;
8 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
9 | * };
10 | */
11 | class Solution {
12 | private:
13 | struct Node{
14 | bool isBST;
15 | int ans;
16 | int min_value;
17 | int max_value;
18 | };
19 | Node getBST(TreeNode * root){
20 | if(root == nullptr){
21 | return {1,0,INT_MAX,INT_MIN};
22 | }
23 | Node left = getBST(root->left);
24 | Node right = getBST(root->right);
25 | Node currentNode;
26 | if(left.isBST==false || right.isBST==false || left.max_value>root->val || right.min_value <= root->val) {
27 | currentNode.isBST = false;
28 | currentNode.ans = max(left.ans , right.ans);
29 | return currentNode;
30 | }
31 | currentNode.isBST = true;
32 | currentNode.ans = left.ans + right.ans + 1;
33 | currentNode.min_value = root->left != nullptr ? left.min_value : root->val;
34 | currentNode.max_value = root->right != nullptr ? right.max_value : root -> val;
35 | return currentNode;
36 | }
37 | public:
38 | int maxSizeBST(TreeNode *root) {
39 | Node Ans = getBST(root);
40 | return Ans.ans;
41 | }
42 | };
43 |
44 |
--------------------------------------------------------------------------------
/Solution/Day-094.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Definition for a binary tree node.
3 | * struct TreeNode {
4 | * int val;
5 | * TreeNode *left;
6 | * TreeNode *right;
7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {}
8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10 | * };
11 | */
12 | class Solution {
13 | int intAns;
14 | int dfs(TreeNode* root){
15 | if(root == nullptr){
16 | return 0;
17 | }
18 | int intLeftSum = dfs(root -> left);
19 | int intRightSum = dfs(root -> right);
20 | //cout << intLeftSum << " : " << intRightSum << '\n';
21 | intAns = max(intAns , intRightSum + intLeftSum + root -> val);
22 | return max(0, root->val + max(intLeftSum , intRightSum));
23 | }
24 | public:
25 | int maxPathSum(TreeNode* root) {
26 | intAns = INT_MIN;
27 | dfs(root);
28 | return intAns;
29 | }
30 | };
31 |
--------------------------------------------------------------------------------
/Solution/Day-095.cpp:
--------------------------------------------------------------------------------
1 | class Solution {
2 | public:
3 | void nextPermutation(vector& nums) {
4 | int last = (int)nums.size()-2;
5 | while(last >= 0 && nums[last] >= nums[last+1]){
6 | last--;
7 | }
8 | if(last < 0){
9 | sort(nums.begin() , nums.end());
10 | return;
11 | }
12 | for(int i=nums.size()-1;i>last;--i){
13 | if(nums[i] > nums[last]){
14 | swap(nums[i] , nums[last]);
15 | break;
16 | }
17 | }
18 | reverse(nums.begin()+last+1 , nums.end());
19 | }
20 | };
21 |
--------------------------------------------------------------------------------
/Solution/Day-096.cpp:
--------------------------------------------------------------------------------
1 | class Solution {
2 | bitset<100>visited;
3 | vector>res;
4 | int n;
5 | void generatePermutation(vector& nums , vector&temp){
6 | if((int)temp.size() == n){
7 | res.push_back(temp);
8 | return;
9 | }
10 | for(int i=0;i> permute(vector& nums) {
22 | n = (int)nums.size();
23 | visited.reset();
24 | vectortemp;
25 | generatePermutation(nums, temp);
26 | return res;
27 | }
28 | };
29 |
30 |
--------------------------------------------------------------------------------
/Solution/Day-097.cpp:
--------------------------------------------------------------------------------
1 | class TimeMap {
2 | public:
3 | /** Initialize your data structure here. */
4 | unordered_map>>mp;
5 | TimeMap() {
6 |
7 | }
8 |
9 | void set(string key, string value, int timestamp) {
10 | mp[key].push_back(make_pair(value , timestamp));
11 | }
12 |
13 | string get(string key, int timestamp) {
14 | const auto &v = mp[key];
15 | if(v.size()==0){
16 | return "";
17 | }
18 | auto c = upper_bound(v.begin() , v.end() ,
19 | [](int val , auto &p){
20 | return val < p.second;
21 | }
22 | );
23 | return c.second;
24 | }
25 | };
26 |
27 | /**
28 | * Your TimeMap object will be instantiated and called as such:
29 | * TimeMap* obj = new TimeMap();
30 | * obj->set(key,value,timestamp);
31 | * string param_2 = obj->get(key,timestamp);
32 | */
33 |
34 |
--------------------------------------------------------------------------------
/Solution/Day-098.cpp:
--------------------------------------------------------------------------------
1 | class Solution {
2 | vector>visited;
3 | vector>dir{{-1, 0}, {0, -1}, {0, 1},{1, 0}};
4 | int row , col;
5 | bool dfs(vector>&grid , string &word ,int r , int c , int index = 1){
6 | if(index == (int)word.size()){
7 | return true;
8 | }
9 | if(index > (int)word.size()){
10 | return false;
11 | }
12 | if(r>=row || c >=col){
13 | return false;
14 | }
15 | //cerr<=row){
19 | continue;
20 | }
21 | if(c+itr.second <0 or c+itr.second>=col){
22 | continue;
23 | }
24 | //cerr<>& board, string word) {
36 | row = board.size();
37 | col = board[0].size();
38 | visited.resize(row , vector(col , false));
39 | for(int i=0;is;
3 | public:
4 | int longestConsecutive(vector& nums) {
5 | for(auto&itr: nums){
6 | s.insert(itr);
7 | }
8 | int best_ans {};
9 | for(auto&itr:s){
10 | if(s.find(itr-1)==s.end()){
11 | int current = itr;
12 | int temp_ans = 1;
13 | while(s.find(current+1)!=s.end()){
14 | ++temp_ans;
15 | ++current;
16 | }
17 | best_ans = max(temp_ans , best_ans);
18 | }
19 | }
20 | return best_ans;
21 | }
22 | };
23 |
--------------------------------------------------------------------------------
/Solution/Day-100.cpp:
--------------------------------------------------------------------------------
1 | class Solution{
2 | public:
3 | int findMinSteps(vector>&points){
4 | int step_counter{};
5 | const int &n = (int)points.size();
6 | for(int i=0;i
2 | using namespace std;
3 | class Solution{
4 | vectorisPrime;
5 | void sieve(int n){
6 | isPrime.assign(n+2 , true);
7 | isPrime[0] = isPrime[1] = false;
8 | for(int i=2;i<=n/2;++i){
9 | if(isPrime[i]){
10 | for(int j=i*i;j<=n;j+=i){
11 | isPrime[j]=false;
12 | }
13 | }
14 | }
15 | return;
16 | }
17 | public:
18 | vectorprimeNumber(int n){
19 | sieve(n);
20 | for(int i=2;i<=n;++i){
21 | if(isPrime[i] && n-i>0 && isPrime[n-i]){
22 | return {i,n-i};
23 | }
24 | }
25 | assert(false);
26 | }
27 | };
28 | int main(void){
29 | Solution s1;
30 | vector v = s1.primeNumber(4);
31 | cout << v[0] <<" "<< v[1] << '\n';
32 | vector i = s1.primeNumber(99);
33 | cout << i[0] <<" "<< i[1] << '\n';
34 | return 0;
35 | }
36 |
--------------------------------------------------------------------------------
/Solution/Day-102.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 | class Solution{
4 | public:
5 | vector continuousArray(vector&nums , int k){
6 | dequeq;
7 | int start = 0 , end_ =0;
8 | const int &n = (int)nums.size();
9 | int sum{};
10 | while(end_ < n){
11 | if(sum == k){
12 | vectorres(q.begin() , q.end());
13 | return res;
14 | }
15 | if(sum > k){
16 | q.pop_front();
17 | sum-=nums[start++];
18 | }else{
19 | q.push_back(nums[end_]);
20 | sum+=nums[end_++];
21 | }
22 | }
23 | assert(false);
24 | }
25 | };
26 | int main(void){
27 | vectorv = {1,2,3,4,5};
28 | Solution s;
29 | vectorres = s.continuousArray(v , 9);
30 | for(auto&itr:res){
31 | cout << itr << ' ';
32 | }
33 | cout << '\n';
34 | return 0;
35 | }
36 |
--------------------------------------------------------------------------------
/Solution/Day-103.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 | class Solution{
4 | public:
5 | string minWindow(const string &s , const string &pattern){
6 | unordered_mapst,pt;
7 | for(auto&itr:pattern){
8 | pt[itr]++;
9 | }
10 | int start = 0 , end_ = s.size(), i=0;
11 | int min_window =INT_MAX;
12 | string ans;
13 | int win_size=0;
14 | listq;
15 | while(i= (int)pattern.size()){
22 | while(start < i && (pt.find(s[start])==pt.end() || st[s[start]]-1>=pt[s[start]])){
23 | st[s[start]]--;
24 | q.pop_front();
25 | ++start;
26 | }
27 | string temp(q.begin() , q.end());
28 | if(ans.size()==0 || temp < ans){
29 | ans = temp;
30 | }
31 | min_window = min(min_window , i - start +1);
32 | }
33 | ++i;
34 | }
35 | return ans;
36 | }
37 | };
38 | int main(void){
39 | Solution s1;
40 | cout << s1.minWindow("figehaeci","aei") << '\n';
41 | return 0;
42 | }
43 |
--------------------------------------------------------------------------------
/Solution/Day-104.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Definition for singly-linked list.
3 | * struct ListNode {
4 | * int val;
5 | * ListNode *next;
6 | * ListNode() : val(0), next(nullptr) {}
7 | * ListNode(int x) : val(x), next(nullptr) {}
8 | * ListNode(int x, ListNode *next) : val(x), next(next) {}
9 | * };
10 | */
11 | class Solution {
12 | public:
13 | bool isPalindrome(ListNode* head) {
14 | stack