সোমবার, ২৯ জুলাই, ২০২৪

Factory Pattern

Factory Method is a creational design pattern that provides an interface for creating objects in a superclass but allows subclasses to alter the type of objects that will be created.

শনিবার, ২৭ জুলাই, ২০২৪

gRPC and REST API

gRPC

gRPC (gRPC Remote Procedure Call) is a modern, open-source framework developed by Google for making remote procedure calls. It uses Protocol Buffers (protobuf) as its interface definition language and data serialization format, allowing for efficient communication between services.
gRPC uses HTTP/2, which allows for multiplexing, reducing the latency and improving the performance compared to traditional HTTP/1.1 used by REST APIs. It also uses Protocol Buffers (protobuf) for data serialization, which is more efficient than JSON.

Pros:

  • Performance and Efficiency: gRPC uses HTTP/2, which allows for multiplexing, reducing the latency and improving the performance compared to traditional HTTP/1.1 used by REST APIs. It also uses Protocol Buffers (protobuf) for data serialization, which is more efficient than JSON
  • Bi-directional Streaming: gRPC supports client, server, and bi-directional streaming, enabling real-time communication and more efficient handling of large data transfers.
  • Cross-platform and Multi-Language Support
  • Built-in Error Handling and Metadata: gRPC has standardized mechanisms for handling errors and sending metadata
Cons:

  • Complexity: gRPC can be more complex to set up and understand, particularly for developers who are not familiar with Protocol Buffers and HTTP/2.
  • Limited Browser Support:
  • Less Human-Readable: Protocol Buffers are binary, which makes them less human-readable compared to the JSON format commonly used with REST APIs.

Use Cases:

  • High performance and efficiency are crucial, such as in microservices communication.
  • You need real-time communication features like streaming.
  • You have a polyglot environment where multiple programming languages are used.
  • You prefer a strongly typed schema and strict contract between client and server.

বৃহস্পতিবার, ১২ অক্টোবর, ২০২৩

Factorization with prime Sieve

  1. vector <int> prime; char sieve[1000009]; int N=1000009; void primeSieve ( ) { sieve[0] = sieve[1] = 1; prime.push_back(2); for ( int i = 4; i <= N; i += 2 ) sieve[i] = 1; int sqrtn = sqrt ( N ); for ( int i = 3; i <= sqrtn; i += 2 ) { if ( sieve[i] == 0 ) { for ( int j = i * i; j <= N; j += 2 * i ) sieve[j] = 1; } } for ( int i = 3; i <= N; i += 2 ) if ( sieve[i] == 0 ) prime.push_back(i); }
  2. vector <int> factors; void factorize( ll n ) { ll sqrtn = sqrt ( n ); for ( ll i = 0; i < prime.size() && prime[i] <= sqrtn; i++ ) { if ( n % prime[i] == 0 ) { while ( n % prime[i] == 0 ) { n /= prime[i]; factors.push_back(prime[i]); } sqrtn = sqrt ( n ); } } if ( n != 1 ) { factors.push_back(n); } }

বৃহস্পতিবার, ৩ মার্চ, ২০২২

Longest Increasing Subsequence & Path Print (LIS)

class Solution {

public:

    int lengthOfLIS(vector<int>& nums) {

        int n = nums.size();

        vector<int> sub, subIndex; 

        vector<int> path(n, -1); 

        for (int i = 0; i < n; ++i) {

            if (sub.empty() || sub[sub.size() - 1] < nums[i]) {

                path[i] = sub.empty() ? -1 : subIndex[sub.size() - 1];

                sub.push_back(nums[i]);

                subIndex.push_back(i);

            } else {

                int idx = lower_bound(sub.begin(), sub.end(), nums[i]) - sub.begin();

                path[i] = idx == 0 ? -1 : subIndex[idx - 1];

                sub[idx] = nums[i];

                subIndex[idx] = i;

            }

        }

        vector<int> ans;

        int t = subIndex[subIndex.size() - 1];

        while (t != -1) {

            ans.push_back(nums[t]);

            t = path[t];

        }

        reverse(ans.begin(), ans.end());

        for(int i=0;i<ans.size();i++){

            cout<<ans[i]<<" ";

        }

        cout<<endl;

        return ans.size();

    }

};

শনিবার, ২৯ জানুয়ারী, ২০২২

Longest non-decreasing subsequence - TC: O(nlogn)

int longestNonDecresingSubsequenceLength(vector<int> a) {

    vector<int> s;

    s.push_back(a[0]);

    int n = a.size();

    int len = 1;

    for (int i = 1; i < n; i++) {

        int idx = upper_bound(s.begin(), s.end(), a[i]) - s.begin();

        int m = s.size();

        if (m > idx) {

            s[idx] = a[i];

        } else {

            s.push_back(a[i]);

        }

    }

    return s.size();

}

সোমবার, ৩০ আগস্ট, ২০২১

Mobius Inversion (Mobius Precalculate)

 const ll N=1e6+5;

ll lp[N];

ll mob[N];

void mobiusPreCalc(){

    mob[1] = 1;

    for (ll i = 2; i < N; ++i) {

        if (!lp[i]) for (ll j = i; j < N; j += i)

            if (!lp[j]) lp[j] = i;

        mob[i] = [](ll x) {

            ll cnt = 0;

            while (x > 1) {

                ll k = 0, d = lp[x];

                while (x % d == 0) {

                    x /= d;

                    ++k;

                    if (k > 1) return 0;

                }

                ++cnt;

            }

            if (cnt & 1) return -1;

            return 1;

        }(i);

    }

}




সোমবার, ২৩ আগস্ট, ২০২১

Detect Cycle and Print Cycle in an directed Graph

Problem :

https://leetcode.com/problems/course-schedule-ii/ 


class Solution {

public:

    vector<int>vis;

    vector<int>vec[5005];

    vector<int> ans;

    bool dfsAndReturnCycle(int src)

    {

        if(vis[src] == 1) 

            return true;

        if(vis[src] == 2) 

            return false;

        vis[src] = 1; 

        for (const auto& nextNode: vec[src])

        {

if(dfsAndReturnCycle(nextNode))

                return true;

        }

        vis[src] = 2;

        ans.push_back(src);

        return false;

        

    }

    vector<int> findOrder(int numCourses, vector<vector<int>>& ar) {

        int n=ar.size();

        for(int i=0;i<n;i++){

            int v=ar[i][0];

            int u=ar[i][1];

            vec[u].push_back(v);

        }

        int flag=0;

        vis = vector<int>(numCourses+2,0);

        for(int i=0;i<numCourses;i++)

        {

            if(vis[i]==0){

                if(dfsAndReturnCycle(i))

                {

                    return {};

                }

            }

        }

        reverse(ans.begin(),ans.end());

        return ans;

    }

};

Factory Pattern

Factory Method  is a creational design pattern that provides an interface for creating objects in a superclass but allows subclasses to alte...