399. Evaluate Division | GRAPHS | | LeetCode Daily Challenge POTD

  Рет қаралды 1,467

DeepCodes

DeepCodes

Күн бұрын

Пікірлер: 8
@dianamujahed3600
@dianamujahed3600 Жыл бұрын
thank you very much
@bkmeher9005
@bkmeher9005 Жыл бұрын
Superb explanation bhai ... thanks
@orzAR26
@orzAR26 Жыл бұрын
Thanks bro, you are the one making me consistent to do leetcode ❤
@shikharshukla8836
@shikharshukla8836 Жыл бұрын
thanks
@salmankhader1258
@salmankhader1258 Жыл бұрын
Your videos are helping me a lot. nice explanation keep going!
@deepcodes
@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
@peakpotential9 Жыл бұрын
Why are we using map, can't we use vector instead?
@deepcodes
@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.
934. Shortest Bridge | Graphs | LeetCode Daily Challenge
14:26
DeepCodes
Рет қаралды 1,3 М.
Evaluate Division - Leetcode 399 - Python
17:37
NeetCodeIO
Рет қаралды 33 М.
This Game Is Wild...
00:19
MrBeast
Рет қаралды 186 МЛН
Yay😃 Let's make a Cute Handbag for me 👜 #diycrafts #shorts
00:33
LearnToon - Learn & Play
Рет қаралды 117 МЛН
How To Choose Mac N Cheese Date Night.. 🧀
00:58
Jojo Sim
Рет қаралды 98 МЛН
Why C is so Influential - Computerphile
10:50
Computerphile
Рет қаралды 1,9 МЛН
956. Tallest Billboard | LeetCode Daily Challenge
12:43
DeepCodes
Рет қаралды 2,3 М.
How to: Work at Google - Example Coding/Engineering Interview
24:02
Life at Google
Рет қаралды 7 МЛН
This Game Is Wild...
00:19
MrBeast
Рет қаралды 186 МЛН