Thanks bro, you are the one making me consistent to do leetcode ❤
@shikharshukla8836 Жыл бұрын
thanks
@salmankhader1258 Жыл бұрын
Your videos are helping me a lot. nice explanation keep going!
@deepcodes Жыл бұрын
class Solution { public: unordered_map adjList; vectorans; bool dfs(string dividend, string divisor, unordered_setvis, double currProd) { if (vis.find(dividend) != vis.end()) { return false; } vis.insert(dividend); if (dividend == divisor) { ans.push_back(currProd); return true; } bool found = false; for (auto ne : adjList[dividend]) { found = dfs(ne.first, divisor, vis, currProd*ne.second); if (found) { return true; } } return false; } vector calcEquation(vector& equations, vector& values, vector& queries) { int n = equations.size(), m = queries.size(); adjList.clear(); ans.clear(); for (int i = 0; i < n ; i++) { string dividend = equations[i][0]; string divisor = equations[i][1]; adjList[dividend].push_back({divisor, values[i]}); adjList[divisor].push_back({dividend, 1.0 / values[i]}); } for (auto &q : queries) { string dividend = q[0]; string divisor = q[1]; if (adjList.find(dividend) == adjList.end() || adjList.find(divisor) == adjList.end()) { ans.push_back(-1.0); } else { unordered_setvis; if (!dfs(dividend, divisor, vis, 1.0)) { ans.push_back(-1); } } } return ans; } };
@peakpotential9 Жыл бұрын
Why are we using map, can't we use vector instead?
@deepcodes Жыл бұрын
Nodes are string, not int. If you want to access value in vector in O(1) then you pass integer index. But here, indexing is done on nodes that are string values.