coderbolt's blog

By coderbolt, history, 12 months ago, In English

This is a simple Dijkstra algorithm. Please tell me correct Time & Space complexities for this algorithm. Different articles on internet giving different answers. I am asking this for interview purpose

Also, let me know if this algorithm is correct or not.

int main ()
{
    cin >> n >> m;

    while (m--)
    {
        cin >> a >> b >> w;
        v[a].push_back({b, w});
        v[b].push_back({a, w});
    }

    priority_queue<int, vector<int>, greater<int>> q;

    dist[1] = 0
    q.push({1,0});
 
    while (q.size() > 0)
    {
        auto curr = q.top();
        q.pop();

        int k = curr.first;
        int currCost = curr.second;

        for(auto it : grph[k])
        {
            child = it.first
            w = it.second;
            
            if ((currCost + w) < dist[child]) 
            {
                dist[child] = currCost + w;
                q.push({dist[child], child});
            }
        }
    }
    
    cout << dist[n] << "\n";
}

Full text and comments »

  • Vote: I like it
  • +16
  • Vote: I do not like it

By coderbolt, history, 12 months ago, In English

1) unordered_map<int, int> map1;

  • i) cout << map1[20] << "\n"; ---> getting value using key, when key is integer
  • ii) map1.find(20) != map1.end() ---> finding value using key, when key is integer

2) ordered_map<int, int> map2;

  • i) cout << map2[20] << "\n"; ---> getting value using key, when key is integer
  • ii) map2.find(20) != map2.end() ---> finding value using key, when key is integer

3) unordered_map<string, int> map3;

  • i) cout << map3["banana"] << "\n"; ---> getting value using key, when key is string
  • ii) map3.find("apple") != map3.end() ---> finding value using key, when key is String

4) ordered_map<int, int> map4;

  • i) cout << map4["banana"] << "\n"; ---> getting value using key, when key is string
  • ii) map4.find("apple") != map4.end() ---> finding value using key, when key is string

5) vector vec5 = {"Apple", "Mango", "Banana", "peanuts", "Cherry"};

sort(vec5.begin(), vec5.end())  ---> sorting a vector of strings

Full text and comments »

  • Vote: I like it
  • +3
  • Vote: I do not like it

By coderbolt, history, 21 month(s) ago, In English

Given a linked list. Each linked list has a character. We need to find the length of the longest palindromic sub-string possible by this linked list. Anyone please give me code and its approach along with Time complexity.

Full text and comments »

  • Vote: I like it
  • -8
  • Vote: I do not like it

By coderbolt, history, 3 years ago, In English

You are given a number n. You have to remove elements [1 to n] which are present in even indexes every time repeatedly and print the last remaining element.

Testcase 1: input --> n=8

The flow of the program is [1,2,3,4,5,6,7,8] --> [2,4,6,8] ---> [4,8] ---> [8]

so here the last remaining element is [8].

Testcase 2: input --> n=5

The flow of the program is [1,2,3,4,5] --> [2,4] ---> [4]

so here the last remaining element is [4]

I want both brute force and optimal solutions for this problem

Full text and comments »

  • Vote: I like it
  • -19
  • Vote: I do not like it