question_slug
stringlengths
3
77
title
stringlengths
1
183
slug
stringlengths
12
45
summary
stringlengths
1
160
author
stringlengths
2
30
certification
stringclasses
2 values
created_at
stringdate
2013-10-25 17:32:12
2025-04-12 09:38:24
updated_at
stringdate
2013-10-25 17:32:12
2025-04-12 09:38:24
hit_count
int64
0
10.6M
has_video
bool
2 classes
content
stringlengths
4
576k
upvotes
int64
0
11.5k
downvotes
int64
0
358
tags
stringlengths
2
193
comments
int64
0
2.56k
goat-latin
Ruby O(n) using map 😤😤😤😤😤😤😤
ruby-on-using-map-by-deter-abcu
Split the given string by spaces so you have an array of each string, and then map through the array and perform the requested actions on each string. Join at t
deter
NORMAL
2018-08-23T21:05:45.894327+00:00
2018-10-25T14:13:42.403653+00:00
184
false
Split the given string by spaces so you have an array of each string, and then map through the array and perform the requested actions on each string. Join at the end and seperate by spaces to create the final string\n```\n# @param {String} s\n# @return {String}\n\ndef to_goat_latin(s)\n\n vowels = {"a" => true, "e"...
2
0
[]
0
goat-latin
Java 16ms solution
java-16ms-solution-by-yxzrandom-v88j
\nclass Solution {\n public String toGoatLatin(String S) {\n if (S == null || S.length() == 0) {\n return "";\n }\n String[]
yxzrandom
NORMAL
2018-06-10T07:39:31.010574+00:00
2018-10-25T14:13:48.237489+00:00
295
false
```\nclass Solution {\n public String toGoatLatin(String S) {\n if (S == null || S.length() == 0) {\n return "";\n }\n String[] str = S.split(" ");\n String source = "aeiouAEIOU";\n StringBuilder res = new StringBuilder();\n for (int i = 0; i < str.length; i++) {\...
2
0
[]
0
goat-latin
JavaScript solution
javascript-solution-by-jessieyang-9al9
\nvar toGoatLatin = function(S) {\n\n const vowel = [\'a\', \'e\', \'i\', \'o\', \'u\'];\n let arr = S.split(\' \');\n\t\t\t\t\n for (let i
jessieyang
NORMAL
2018-05-21T06:28:36.582545+00:00
2018-05-21T06:28:36.582545+00:00
238
false
```\nvar toGoatLatin = function(S) {\n\n const vowel = [\'a\', \'e\', \'i\', \'o\', \'u\'];\n let arr = S.split(\' \');\n\t\t\t\t\n for (let i = 0; i < arr.length; i++) {\n let word = arr[i];\n if (vowel.indexOf(word[0].toLowerCase()) > -1) {\n word += \'ma\';\n...
2
0
[]
0
goat-latin
Java: 27ms Clean
java-27ms-clean-by-solodjavadev91-n7se
\nclass Solution {\n public String toGoatLatin(String S) {\n String[] sp = S.split("\\\\s+");\n StringBuilder r = new StringBuilder();\n
solodjavadev91
NORMAL
2018-05-17T07:32:39.724059+00:00
2018-10-25T14:14:01.477888+00:00
181
false
```\nclass Solution {\n public String toGoatLatin(String S) {\n String[] sp = S.split("\\\\s+");\n StringBuilder r = new StringBuilder();\n int i = 1;\n for(String s : sp){\n r.append(iBuild(s,i));\n r.append(" ");\n i++;\n }\n r.setLength(r....
2
0
[]
0
goat-latin
[ Javascript / Python3 / C++ ] solutions
javascript-python3-c-solutions-by-clayto-7pwy
Javascript\n\nlet toGoatLatin = (words, isVowel = c => 0 <= \'aeiou\'.indexOf(c.toLowerCase())) =>\n words.split(\' \')\n .map(s => isVowel(s[0]) ? s
claytonjwong
NORMAL
2018-05-02T03:56:50.034715+00:00
2020-08-19T23:54:21.899886+00:00
244
false
*Javascript*\n```\nlet toGoatLatin = (words, isVowel = c => 0 <= \'aeiou\'.indexOf(c.toLowerCase())) =>\n words.split(\' \')\n .map(s => isVowel(s[0]) ? s : s.substring(1, s.length) + s[0])\n .map((s, i) => s + \'ma\' + \'a\'.repeat(i + 1)).join(\' \');\n```\n\n*Python3*\n```\nclass Solution:\n def ...
2
0
[]
1
goat-latin
Java O(n) Solution
java-on-solution-by-vjsfbay-zmc3
\nString[] strArr = S.split(" ");\n StringBuilder strBld = new StringBuilder();\n\n Set<Character> vowel = new HashSet<>();\n for (char c :
vjsfbay
NORMAL
2018-04-30T01:07:39.113335+00:00
2018-10-25T14:14:49.815967+00:00
551
false
```\nString[] strArr = S.split(" ");\n StringBuilder strBld = new StringBuilder();\n\n Set<Character> vowel = new HashSet<>();\n for (char c : "aeiouAEIOU".toCharArray()) vowel.add(c);\n StringBuilder aCounter = new StringBuilder();\n aCounter.append("a");\n\n for (int i = 0; i...
2
0
[]
1
goat-latin
Java: If you think like me..
java-if-you-think-like-me-by-coder11-ekfz
\nclass Solution {\n public String toGoatLatin(String S) {\n if(S==null || S.length()==0) return S;\n Set<Character> vowels = new HashSet<Chara
coder11
NORMAL
2018-04-29T22:40:31.760042+00:00
2018-10-25T14:14:18.151536+00:00
195
false
```\nclass Solution {\n public String toGoatLatin(String S) {\n if(S==null || S.length()==0) return S;\n Set<Character> vowels = new HashSet<Character>(Arrays.asList(\'a\',\'e\',\'i\',\'o\',\'u\',\'A\',\'E\',\'I\',\'O\',\'U\'));\n \n StringBuilder prev_a=new StringBuilder(),res=new String...
2
0
[]
0
goat-latin
Simulation
simulation-by-khaled-alomari-hm86
Complexity Time complexity: O(n2) Space complexity: O(n2) Code
khaled-alomari
NORMAL
2025-03-06T17:34:03.866608+00:00
2025-03-06T17:40:45.290963+00:00
45
false
# Complexity - Time complexity: $$O(n^2)$$ - Space complexity: $$O(n^2)$$ # Code ```typescript [] function toGoatLatin(sentence: string) { const vowels = new Set(['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']); return sentence.split(' ').map((w, i) => { const conso = !vowels.has(w[0]); ret...
1
0
['Array', 'Hash Table', 'String', 'String Matching', 'Simulation', 'Iterator', 'TypeScript', 'JavaScript']
0
goat-latin
Easy answer in 1 loop
easy-answer-in-1-loop-by-saksham_gupta-his7
Complexity Time complexity: O(n) Space complexity: O(n) Code
Saksham_Gupta_
NORMAL
2025-01-30T17:41:53.268029+00:00
2025-01-30T17:41:53.268029+00:00
256
false
<!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: ***O(n)*** <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: ***O(n)*** <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```java [] class Solution { public String toGoatLatin(String sente...
1
0
['String', 'Java']
0
goat-latin
Do As it Says | Simple | Readable | Linear O(N)
do-as-it-says-simple-readable-linear-on-wv56e
CodeComplexity Time complexity: O(N) Space complexity: O(N)
Apakg
NORMAL
2024-12-24T08:58:01.813918+00:00
2024-12-24T08:58:01.813918+00:00
185
false
# Code ```java [] class Solution { Set<String> set = Set.of("a", "e", "i", "o", "u", "A", "E", "I", "O", "U"); public String toGoatLatin(String sentence) { String[] words = sentence.split("\\s"); StringBuilder stack = new StringBuilder(); int wordsProcessed = 0; for(String word: ...
1
0
['String', 'Stack', 'Ordered Set', 'Java']
0
goat-latin
Brute Force approach
brute-force-approach-by-anshsavadatti-8m4f
Intuition\n Describe your first thoughts on how to solve this problem. \nAfter looking at the problem, came to a conclusing that basic STL operations, especiall
anshsavadatti
NORMAL
2024-09-24T13:32:25.592689+00:00
2024-09-24T13:32:25.592725+00:00
252
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nAfter looking at the problem, came to a conclusing that basic STL operations, especially of strings can solve the problem.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nBrute Force, using Standard Templete Librar...
1
0
['Array', 'String', 'C++']
0
goat-latin
ho gya !!! :D
ho-gya-d-by-yesyesem-tcir
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
yesyesem
NORMAL
2024-09-24T13:04:34.072515+00:00
2024-09-24T13:04:34.072544+00:00
119
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
1
0
['C++']
0
goat-latin
[Java] Easy solution
java-easy-solution-by-ytchouar-84x1
java\nimport java.util.StringJoiner;\n\nclass Solution {\n public String toGoatLatin(final String sentence) {\n final String[] words = sentence.split(
YTchouar
NORMAL
2024-05-28T19:08:39.906199+00:00
2024-05-28T19:08:39.906216+00:00
683
false
```java\nimport java.util.StringJoiner;\n\nclass Solution {\n public String toGoatLatin(final String sentence) {\n final String[] words = sentence.split("\\\\s+");\n final StringJoiner sj = new StringJoiner(" ");\n\n int n = 1;\n\n for(final String word : words) {\n final char ...
1
0
['Java']
0
goat-latin
Python solution without split()
python-solution-without-split-by-trisham-j3w0
Key Terms\n- prev: Initialize to space. Each time prev = space, you\'ve reached the start of a new word.\n- As: Initialize to one, meaning one \'a\' will be add
trishamorris
NORMAL
2024-05-12T00:57:29.940130+00:00
2024-05-12T00:57:29.940154+00:00
229
false
# Key Terms\n- prev: Initialize to space. Each time prev = space, you\'ve reached the start of a new word.\n- As: Initialize to one, meaning one \'a\' will be added to the end of the first word. Increment As for each word encountered. Multiply \'a\' by As each time you are adding a\'s to the end of a word\n- append: If...
1
0
['Python3']
1
goat-latin
Kotlin || O(N) Time and Space
kotlin-on-time-and-space-by-girl_coder_s-cs03
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
girl_coder_s
NORMAL
2024-04-03T18:01:06.307265+00:00
2024-04-03T18:01:06.307298+00:00
112
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
1
0
['Kotlin']
0
goat-latin
Easy Beginner Level Java Code
easy-beginner-level-java-code-by-saurabh-zq11
Complexity\n- Time complexity:\nO(n*n)\n- Space complexity:\nO(n)\n# Code\n\nclass Solution {\n public String toGoatLatin(String sentence) {\n HashSet
Saurabh_Mishra06
NORMAL
2024-01-18T14:15:39.006619+00:00
2024-01-18T14:15:39.006650+00:00
483
false
# Complexity\n- Time complexity:\nO(n*n)\n- Space complexity:\nO(n)\n# Code\n```\nclass Solution {\n public String toGoatLatin(String sentence) {\n HashSet<Character> vowels = new HashSet();\n for(char c : "aeiouAEIOU".toCharArray()){\n vowels.add(c);\n }\n\n String result = ""...
1
0
['Java']
0
goat-latin
Easy C++ Solution || Basic Approach & 3 line code
easy-c-solution-basic-approach-3-line-co-csfg
\n\n\n\n# Code\n\nclass Solution {\npublic:\n string toGoatLatin(string sentence) {\n string ans="";\n vector<string>p;\n string pp="";\
tiwariswapnil100
NORMAL
2024-01-10T16:31:42.430823+00:00
2024-01-10T16:31:42.430872+00:00
187
false
\n\n\n\n# Code\n```\nclass Solution {\npublic:\n string toGoatLatin(string sentence) {\n string ans="";\n vector<string>p;\n string pp="";\n for(auto x:sentence){\n if(x==\' \'){\n p.push_back(pp);\n pp="";\n }\n else{\n ...
1
0
['C++']
1
goat-latin
JAVA ~> Goat Latin :)
java-goat-latin-by-01aas-wvxi
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
agent_001
NORMAL
2023-12-03T16:48:21.094875+00:00
2023-12-03T16:48:21.094909+00:00
7
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
1
0
['Java']
0
goat-latin
C++ simple solution || beats 100% [0ms]💯
c-simple-solution-beats-100-0ms-by-shrey-qing
Code\n\nclass Solution {\npublic:\n string toGoatLatin(string sentence) {\n string result = "", word = "", vowels = "aeiouAEIOU";\n int index =
shreya4dhingra
NORMAL
2023-11-10T07:11:21.491648+00:00
2023-11-10T07:11:21.491674+00:00
33
false
# Code\n```\nclass Solution {\npublic:\n string toGoatLatin(string sentence) {\n string result = "", word = "", vowels = "aeiouAEIOU";\n int index = 1;\n for(int i=0; i<sentence.size(); i++) {\n if(sentence[i] == \' \') {\n if(vowels.find(word[0]) == string::npos)\n ...
1
0
['C++']
0
goat-latin
BEATS 100%😎 || JAVA VERY EASY SOLUTION 🔥🔥🔥
beats-100-java-very-easy-solution-by-sum-gkw3
\n\n# Code\n\nclass Solution {\n public String toGoatLatin(String sentence) {\n String[] s=sentence.split(" ");\n StringBuilder sb=new StringBu
sumo25
NORMAL
2023-09-01T18:52:37.092933+00:00
2023-09-01T18:52:37.092957+00:00
7
false
\n\n# Code\n```\nclass Solution {\n public String toGoatLatin(String sentence) {\n String[] s=sentence.split(" ");\n StringBuilder sb=new StringBuilder();\n for(int i=0;i<s.length;i++){\n String str=s[i];\n if(str.charAt(0)==\'a\'|| str.charAt(0)==\'e\' || str.charAt(0)==\'...
1
0
['Java']
0
goat-latin
Easy java solution using simpler concepts
easy-java-solution-using-simpler-concept-fde2
\n\n# Code\n\nclass Solution {\n public String toGoatLatin(String sentence) {\n sentence.toLowerCase();\n String[] words=sentence.split(" ",0);
Palakpreet
NORMAL
2023-05-07T07:48:16.174565+00:00
2023-05-07T07:48:16.174606+00:00
636
false
\n\n# Code\n```\nclass Solution {\n public String toGoatLatin(String sentence) {\n sentence.toLowerCase();\n String[] words=sentence.split(" ",0);\n int i=1;\n String ans="";\n for(String word:words){\n \n char p1=word.charAt(0);\n char p=word.toLowe...
1
0
['Java']
0
goat-latin
Goat Latin 100% runtime beats
goat-latin-100-runtime-beats-by-swakshan-da30
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
Swakshan
NORMAL
2023-04-05T17:56:10.289709+00:00
2023-04-05T17:56:10.289751+00:00
199
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
1
0
['C++']
0
goat-latin
Best [C++] Solution || Beats 100%
best-c-solution-beats-100-by-yashgagrani-hwg9
Code\n\nclass Solution {\npublic:\n bool isValid(string &ans){\n if(ans[0]==\'a\' || ans[0]==\'e\' || ans[0]==\'i\' || ans[0]==\'o\' || ans[0]==\'u\'
YashGagrani-
NORMAL
2023-02-16T14:21:11.172419+00:00
2023-02-16T14:21:11.172466+00:00
1,221
false
# Code\n```\nclass Solution {\npublic:\n bool isValid(string &ans){\n if(ans[0]==\'a\' || ans[0]==\'e\' || ans[0]==\'i\' || ans[0]==\'o\' || ans[0]==\'u\' || ans[0]==\'A\' || ans[0]==\'E\' || ans[0]==\'I\' || ans[0]==\'O\' || ans[0]==\'U\') return true;\n return false;\n }\n string toGoatLatin(st...
1
0
['C++']
0
goat-latin
C++ find
c-find-by-neillee1210-iyeo
Intuition\n Describe your first thoughts on how to solve this problem. \nlinear search \n# Approach\n Describe your approach to solving the problem. \nSearch a
NeilLee1210
NORMAL
2023-01-15T15:33:24.895630+00:00
2023-01-15T15:34:32.635291+00:00
38
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nlinear search \n# Approach\n<!-- Describe your approach to solving the problem. -->\nSearch a word in sentense\n\nConvert to goat latin\n\nCombine it to new sentense\n\nNext search\n# Complexity\n- Time complexity:\n<!-- Add your time com...
1
0
['C++']
0
goat-latin
[Accepted] Swift
accepted-swift-by-vasilisiniak-lqsv
\nclass Solution {\n func toGoatLatin(_ sentence: String) -> String {\n sentence\n .components(separatedBy: " ")\n .enumerated()
vasilisiniak
NORMAL
2022-12-27T09:35:03.751167+00:00
2022-12-27T09:35:03.751198+00:00
269
false
```\nclass Solution {\n func toGoatLatin(_ sentence: String) -> String {\n sentence\n .components(separatedBy: " ")\n .enumerated()\n .map { i, w -> String in\n if "aeiou".contains(w.first!.lowercased()) {\n return "\\(w)ma\\(String(repeating:...
1
0
['Swift']
1
goat-latin
JavaScript | JS | Easy To Understand | Simple Solution
javascript-js-easy-to-understand-simple-yhvm5
\n\n/**\n * @param {string} sentence\n * @return {string}\n */\nvar toGoatLatin = function(sentence) {\n const vowelSet = new Set([\'a\', \'e\', \'i\', \'o\'
seungwoo321
NORMAL
2022-12-11T13:23:39.655454+00:00
2022-12-11T13:23:39.655495+00:00
424
false
\n```\n/**\n * @param {string} sentence\n * @return {string}\n */\nvar toGoatLatin = function(sentence) {\n const vowelSet = new Set([\'a\', \'e\', \'i\', \'o\', \'u\', \'A\', \'E\', \'I\', \'O\', \'U\']);\n return sentence.split(\' \').map((str, i) => {\n return (\n vowelSet.has(str[0]) ?\n ...
1
0
['JavaScript']
0
goat-latin
c# faster than 100% 69ms
c-faster-than-100-69ms-by-wenhuan-zlgu
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
wenhuan
NORMAL
2022-12-05T03:17:15.395364+00:00
2022-12-05T03:20:37.013767+00:00
503
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n)\n<!-- Add your space complexity here, e.g. $...
1
0
['C#']
0
goat-latin
JS easy solution with O(n)
js-easy-solution-with-on-by-kunkka1996-ru0n
\nconst vowel = {\n "a": "a",\n "e": "e",\n "i": "i",\n "o": "o",\n "u": "u",\n "A": "A",\n "E": "E",\n "I": "I",\n "O": "O",\n "U
kunkka1996
NORMAL
2022-09-10T04:32:29.860323+00:00
2022-09-10T04:32:29.860356+00:00
754
false
```\nconst vowel = {\n "a": "a",\n "e": "e",\n "i": "i",\n "o": "o",\n "u": "u",\n "A": "A",\n "E": "E",\n "I": "I",\n "O": "O",\n "U": "U",\n}\n\nvar toGoatLatin = function(sentence) {\n let output = \'\';\n sentence = sentence.split(\' \');\n\n for (let i = 0; i < sentence.lengt...
1
0
['JavaScript']
1
goat-latin
Java easy to understand solution & super optimized solution
java-easy-to-understand-solution-super-o-4fgs
\nimport java.util.*;\nclass Solution {\n public String toGoatLatin(String sentence) {\n StringTokenizer st=new StringTokenizer(sentence," ");\n
mdmehedihassan
NORMAL
2022-08-18T11:15:40.052841+00:00
2022-08-18T11:18:35.562186+00:00
347
false
```\nimport java.util.*;\nclass Solution {\n public String toGoatLatin(String sentence) {\n StringTokenizer st=new StringTokenizer(sentence," ");\n StringBuilder sb=new StringBuilder();\n String a="a";\n while(st.hasMoreTokens()){\n String word=st.nextToken();\n if(!...
1
0
['Java']
1
goat-latin
📌Easy & Fast Java☕ Solution using StringBuilder
easy-fast-java-solution-using-stringbuil-mo8y
```\nclass Solution {\n public String toGoatLatin(String sentence) {\n String s[] = sentence.split(" ");\n StringBuilder sb = new StringBuilder
saurabh_173
NORMAL
2022-07-25T14:34:21.258682+00:00
2022-07-25T14:34:21.258737+00:00
179
false
```\nclass Solution {\n public String toGoatLatin(String sentence) {\n String s[] = sentence.split(" ");\n StringBuilder sb = new StringBuilder();\n int i=1;\n for(String sen:s)\n {\n if(check(sen.charAt(0)))\n sb.append(sen);\n else\n ...
1
0
['String', 'Java']
0
most-profitable-path-in-a-tree
✔✔✔ 2 DFS || 1 DFS || Simple Approach || C++
2-dfs-1-dfs-simple-approach-c-by-brehamp-mcmp
Main Idea \nAlice can travel to any leaf from 0 but there is only one path for Bob. So we will find the path Bob will follow and update the contribution of each
brehampie
NORMAL
2022-11-12T16:00:57.947739+00:00
2022-11-13T03:39:52.022383+00:00
10,114
false
<h5>Main Idea</h5>\nAlice can travel to any leaf from 0 but there is only one path for Bob. So we will find the path Bob will follow and update the contribution of each node in the path first.\n\nWith the first dfs we will find the time to travel to each node <b>\'u\'</b> from 0 and previous node of <b>\'u\'</b> in th...
124
5
['Depth-First Search', 'C']
16
most-profitable-path-in-a-tree
C++ || Simple DFS and BFS || Detailed explanation
c-simple-dfs-and-bfs-detailed-explanatio-62iy
\nclass Solution {\npublic:\n bool DFS(int src, int time, unordered_map<int,int> &path, vector<bool> &visited, vector<vector<int>> &graph){\n path[src
_Potter_
NORMAL
2022-11-12T16:04:29.985790+00:00
2022-11-12T18:05:16.833920+00:00
5,755
false
```\nclass Solution {\npublic:\n bool DFS(int src, int time, unordered_map<int,int> &path, vector<bool> &visited, vector<vector<int>> &graph){\n path[src] = time;\n visited[src] = true;\n if(src == 0){\n return true;\n }\n for(auto adj: graph[src]){\n if(!visi...
77
9
['C', 'C++']
8
most-profitable-path-in-a-tree
🚀Beats 100% | Most Profitable Path in a Tree | DFS Solution 🌳
beats-100-most-profitable-path-in-a-tree-3ohk
Youtube🚀Beats 100% | Most Profitable Path in a Tree | DFS Solution 🌳🔼 Please Upvote🔼 Please Upvote🔼 Please Upvote🔼 Please Upvote💡If this helped, don’t forget to
rahulvijayan2291
NORMAL
2025-02-24T05:26:19.852052+00:00
2025-02-24T05:26:19.852052+00:00
15,820
false
# Youtube https://youtu.be/2DioKI5nSjA ![image.png](https://assets.leetcode.com/users/images/0cb9d72b-5c01-497a-9314-f4c26c7f4679_1740373717.6809292.png) # 🚀 **Beats 100% | Most Profitable Path in a Tree | DFS Solution 🌳** --- # **🔼 Please Upvote** # **🔼 Please Upvote** # **🔼 Please Upvote** # **🔼 Pl...
76
1
['Array', 'Tree', 'Depth-First Search', 'Breadth-First Search', 'Graph', 'C++', 'Java', 'Python3']
3
most-profitable-path-in-a-tree
[Python] One DFS
python-one-dfs-by-lee215-cxkm
Explanation\nd0 is the distance from node 0 to node i\ndb is the distance from node i to node bob.\nIf node i is not ancestor of bob, we define db >= n.\n\nSo i
lee215
NORMAL
2022-11-12T16:40:49.519000+00:00
2022-11-12T16:40:49.519036+00:00
5,600
false
# **Explanation**\n`d0` is the distance from node `0` to node `i`\n`db` is the distance from node `i` to node `bob`.\nIf node `i` is not ancestor of `bob`, we define `db >= n`.\n\nSo in the dfs, we first pick out the biggest sum of sub path.\nIf the node has no child, then the biggest sum is `0`.\nnow we compare `d0` a...
67
3
[]
7
most-profitable-path-in-a-tree
Was this actually tough for Leetcode Medium?
was-this-actually-tough-for-leetcode-med-f5co
i felt this question was bit tougher for a typical Leetcode C. Using BFS+DFS+Time Synchroniazation in just one problem is bit of overkill for Leetcode C.\n\nMy
harem_jutsu
NORMAL
2022-11-12T16:22:39.132006+00:00
2022-11-12T16:22:39.132047+00:00
3,577
false
i felt this question was bit tougher for a typical Leetcode C. Using BFS+DFS+Time Synchroniazation in just one problem is bit of overkill for Leetcode C.\n\nMy opinion only. You can have different take on this.
41
10
[]
14
most-profitable-path-in-a-tree
twice DFS ||C++ 43ms beats 100% Py3
twice-dfs-c-51ms-beats-9971-by-anwendeng-5rmw
IntuitionUse DFS to solve, but twice. 1st one finds the parent 2nd one computes the max leaf sumC++ & Python are done. Approach Declare global arraysadj, parent
anwendeng
NORMAL
2025-02-24T01:19:39.079518+00:00
2025-02-24T13:33:38.601536+00:00
7,562
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> Use DFS to solve, but twice. 1. 1st one finds the parent 2. 2nd one computes the max leaf sum C++ & Python are done. # Approach <!-- Describe your approach to solving the problem. --> 0. Declare global arrays `adj, parent & Bob` with size `...
29
0
['Depth-First Search', 'C++', 'Python3']
5
most-profitable-path-in-a-tree
🔥SIMPLE 💯 % 🎯 |✨SUPER EASY BEGINNERS 👏| JAVA | C | C++ | PYTHON| JAVASCRIPT | DART
simple-super-easy-beginners-java-c-c-pyt-y581
IntuitionThe problem requires us to find the maximum profit Alice can collect while moving along a tree structure. Alice starts from the root, and Bob starts fr
CodeWithSparsh
NORMAL
2025-02-24T08:46:04.555215+00:00
2025-02-24T08:50:45.547943+00:00
3,316
false
![1000097308.png](https://assets.leetcode.com/users/images/c381c80e-f57c-4c7a-a85f-a67bad9a8011_1740387040.2097476.png) --- # Intuition The problem requires us to find the maximum profit Alice can collect while moving along a tree structure. Alice starts from the root, and Bob starts from a specific node, both m...
22
0
['Array', 'Tree', 'Depth-First Search', 'Graph', 'C', 'Python', 'C++', 'Java', 'Python3', 'JavaScript']
2
most-profitable-path-in-a-tree
✅ [Python] finally a concise DFS solution with clean code (explained)
python-finally-a-concise-dfs-solution-wi-9a5x
\u2705 IF YOU LIKE THIS SOLUTION, PLEASE UPVOTE.\n*\nThis solution employs a Depth First Search approach to find both Bob\'s path to 0 and Alice\'s most profita
stanislav-iablokov
NORMAL
2022-11-12T21:18:32.859664+00:00
2022-11-12T21:37:41.693478+00:00
1,554
false
**\u2705 IF YOU LIKE THIS SOLUTION, PLEASE UPVOTE.**\n****\nThis solution employs a Depth First Search approach to find both Bob\'s path to `0` and Alice\'s most profitable path. Time complexity is linear: **O(N)**. Space complexity is linear: **O(N)**.\n\n**Python.**\n```\nfrom numpy import sign\n\nclass Solution:\n ...
18
0
[]
3
most-profitable-path-in-a-tree
DP on Trees, Easy Readable Code with Explanation
dp-on-trees-easy-readable-code-with-expl-hc4k
```\nIdea -> 1. The first thing to notice is we have a specfic only path from Bob to 0 root. \n We\'ll do a dfs to find that path and mark the verti
akshattuknait123
NORMAL
2022-11-12T16:06:58.085617+00:00
2022-11-13T15:15:22.508138+00:00
2,706
false
```\nIdea -> 1. The first thing to notice is we have a specfic only path from Bob to 0 root. \n We\'ll do a dfs to find that path and mark the vertices on those path at how much distance they\'re at.\n\t 2. Now, we\'ll traverse the whole tree from 0, we can do a dfs search with taking distance as a ref...
18
8
['Dynamic Programming', 'Depth-First Search']
3
most-profitable-path-in-a-tree
Java || DFS ( Find Path ) + DFS ( get maxSum ) || Explained
java-dfs-find-path-dfs-get-maxsum-explai-apmo
find path bob -> ... -> alice\n2. modify the amount of node in the path by rule :\n\nleft half nodes to 0 (bob reaches first), if exact middle node exist, make
cooper--
NORMAL
2022-11-12T16:03:54.721472+00:00
2022-11-12T16:11:46.256256+00:00
2,201
false
1. find path `bob -> ... -> alice`\n2. modify the `amount` of `node` in the path by rule :\n```\nleft half nodes to 0 (bob reaches first), if exact middle node exist, make it half (reaching same time)\n```\n3. backtrack traverse tree to get max path Sum\n\n![image](https://assets.leetcode.com/users/images/0d42268d-814...
14
0
['Java']
2
most-profitable-path-in-a-tree
🚀 Java: faster than 100%: resolve Bob then optimize Alice
java-faster-than-100-resolve-bob-then-op-8lti
Intuition: We can compute Bob\'s path up to the root (0), and resolve what happens at each gate right away. Up to, but not including, the halfway point, Bob ta
mattihito
NORMAL
2022-11-13T00:52:41.714143+00:00
2022-12-29T07:05:38.464321+00:00
1,726
false
**Intuition**: We can compute Bob\'s path up to the root (0), and resolve what happens at each gate right away. Up to, but not including, the halfway point, Bob takes or pays the entire reward/fee at each gate. So we set `amount[i]` to 0 for each such node. If Bob\'s path has an even number of steps (an odd number o...
10
0
['Depth-First Search', 'Java']
0
most-profitable-path-in-a-tree
4ms C++ beats 100%, thank you again Adjacency Sum
4ms-c-beats-100-thank-you-again-adjacenc-pzl0
The general structure of the solution is probably explained in the editorial and all the other solutions, so I'll make it brief. First, we need to find the path
yjian012
NORMAL
2025-02-24T06:22:15.199780+00:00
2025-02-24T16:48:32.580069+00:00
889
false
The general structure of the solution is probably explained in the editorial and all the other solutions, so I'll make it brief. First, we need to find the path from Alice to Bob. Then, we must find the nodes on the path that's closer to Bob than to Alice and change their `amount` to zero. If there's one right in the m...
9
0
['C++']
2
most-profitable-path-in-a-tree
C++ Solution || Detailed Explanation
c-solution-detailed-explanation-by-rohit-62mg
IntuitionThe problem requires traversing the tree efficiently while considering the movement constraints imposed by Bob. Since it's a tree, we can use DFS and B
Rohit_Raj01
NORMAL
2025-02-24T00:40:46.947577+00:00
2025-02-24T00:40:46.947577+00:00
3,001
false
# Intuition The problem requires traversing the tree efficiently while considering the movement constraints imposed by Bob. Since it's a tree, we can use DFS and BFS to construct a proper traversal strategy. # Approach 1. **Tree Construction**: Convert the given `edges` list into an adjacency list representation. 2. *...
9
0
['Array', 'Tree', 'Breadth-First Search', 'Graph', 'C++']
2
most-profitable-path-in-a-tree
🌟 Beats 93.59% 🌟 | ✔️ Easiest Solution & Beginner Friendly ✔️ | 🔥 DFS + Backtracking 🔥
beats-9359-easiest-solution-beginner-fri-l02d
IntuitionThe problem involves a tree structure whereAlicemoves towards aleafnode whileBobmoves towards theroot. The key idea is to determine Bob’spathand adjust
ntrcxst
NORMAL
2025-02-24T03:17:39.565537+00:00
2025-02-24T03:17:39.565537+00:00
1,850
false
--- ![image.png](https://assets.leetcode.com/users/images/b490ab95-272f-4d2b-ac02-2aff7c241412_1740366856.9484284.png) --- # Intuition The problem involves a tree structure where **Alice** moves towards a **leaf** node while **Bob** moves towards the **root**. The key idea is to determine Bob’s `path` and adjust Alic...
8
0
['Array', 'Greedy', 'Tree', 'Depth-First Search', 'Breadth-First Search', 'Graph', 'Recursion', 'Shortest Path', 'C++', 'Java']
1
most-profitable-path-in-a-tree
One Pass
one-pass-by-votrubac-wli6
We traverse the tree, tracking the max profit and the current distance dist to node 0.\n\nIf we step on node bob, it means that open = dist + 1 nodes (this one
votrubac
NORMAL
2022-11-15T08:17:24.044086+00:00
2022-11-15T08:36:58.344819+00:00
1,242
false
We traverse the tree, tracking the max profit and the current distance `dist` to node `0`.\n\nIf we step on node `bob`, it means that `open = dist + 1` nodes (this one and previous) are opened.\n\nWe return `{res, open - 2}` to track the maximum `res` and the number of opened nodes.\n\n**C++**\n```cpp\npair<int, int> d...
8
1
['C']
1
most-profitable-path-in-a-tree
Preprocess then DFS || Java
preprocess-then-dfs-java-by-abdulazizms-yzaj
\n# Approach\nWe first find the unique path that Bob traversed as well as the distance from Bob\'s initial position to any node along the path.\nThen, a DFS sta
abdulazizms
NORMAL
2022-11-12T16:10:12.368788+00:00
2022-11-12T16:14:04.187830+00:00
1,182
false
\n# Approach\nWe first find the unique path that Bob traversed as well as the distance from Bob\'s initial position to any node along the path.\nThen, a DFS starting from the root to find the max income Alice can get. \n\n# Code\n```\nclass Solution {\n static int [] par;\n static int result; \n static int mos...
7
0
['Java']
1
most-profitable-path-in-a-tree
✅ Easy Explanation | Tree | DFS | Java | C++ | Python Detailed Video Explanation 🔥
easy-explanation-tree-dfs-java-c-python-59if2
IntuitionThe problem involves navigating a tree where Alice and Bob move along different paths. Bob starts from a given node and follows the shortest path to th
sahilpcs
NORMAL
2025-02-24T03:18:55.134049+00:00
2025-02-24T03:18:55.134049+00:00
1,851
false
# Intuition The problem involves navigating a tree where Alice and Bob move along different paths. Bob starts from a given node and follows the shortest path to the root (node 0). Alice starts from the root and explores all paths to maximize her profit while considering Bob's journey. # Approach 1. Construct an adjace...
6
0
['Array', 'Tree', 'Depth-First Search', 'Graph', 'Python', 'C++', 'Java', 'Python3']
1
most-profitable-path-in-a-tree
DFS Solution
dfs-solution-by-richyrich2004-aa2s
IntuitionApproachComplexity Time complexity: Space complexity: Code
RichyRich2004
NORMAL
2025-02-24T01:15:50.407226+00:00
2025-02-24T01:15:50.407226+00:00
1,095
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code `...
6
0
['Python3']
0
most-profitable-path-in-a-tree
🔥 Recursive DFS-based O(N) DP || 23ms in Rust & ~30ms in Go
recursive-dfs-based-on-dp-23ms-in-rust-3-4qzq
🚀 IntuitionInstead of performing multiple passes (BFS + DP), we solve the problem in one recursive DFS traversal. Alice moves towards a leaf while maximizing he
rutsh
NORMAL
2025-02-24T11:55:53.998659+00:00
2025-02-24T13:45:01.572000+00:00
308
false
# 🚀 Intuition Instead of performing multiple passes (__BFS + DP__), we solve the problem in __one recursive DFS traversal__. - Alice moves towards a leaf while maximizing her profit. - Bob moves towards the root, reducing profits at shared nodes. - We compute both Alice’s max profit & Bob’s distance in a single DFS,...
5
0
['Dynamic Programming', 'Tree', 'Recursion', 'Go', 'Rust']
0
most-profitable-path-in-a-tree
🔴💎 Solution!
solution-by-quantummaniac609-pkmt
IntuitionLooking at this problem, I realized we need to track two simultaneous paths: Alice's path from root to any leaf, and Bob's path from his starting posit
quantummaniac609
NORMAL
2025-02-24T05:43:34.091802+00:00
2025-02-24T05:43:34.091802+00:00
88
false
# Intuition Looking at this problem, I realized we need to track two simultaneous paths: Alice's path from root to any leaf, and Bob's path from his starting position to root. The key insight is that we can first determine Bob's path and timing, then use this information while exploring Alice's possible paths to find t...
5
0
['Ruby']
0
most-profitable-path-in-a-tree
Simple DFS Traversal
simple-dfs-traversal-by-mj30-umkw
//Mayank Jain\n\nThe idea is to construct parent vector firstly and setting parent value for each of the nodes. This will be helpful for Bob while traversing in
MJ30
NORMAL
2022-11-16T08:59:59.617953+00:00
2022-11-16T08:59:59.617991+00:00
584
false
//Mayank Jain\n\nThe idea is to construct parent vector firstly and setting parent value for each of the nodes. This will be helpful for Bob while traversing in upwards direction towards source node 0. \n\n-> We will do a DFS graph traversal for both Alice (starting node as 0) & Bob (starting node as bob) and will mark...
5
0
['Backtracking', 'Depth-First Search', 'Recursion']
0
most-profitable-path-in-a-tree
Double DFS || Level checking || Easy to understand || C++
double-dfs-level-checking-easy-to-unders-br2p
\n\n# Code\n\nclass Solution {\n vector<int>g[100005];\n int level_bob[100005];\n bool vis[100005];\n \n int pa[100005];\n \n long long max
BlueSharK_14
NORMAL
2022-11-12T16:05:19.214761+00:00
2022-11-12T16:46:00.767034+00:00
1,161
false
\n\n# Code\n```\nclass Solution {\n vector<int>g[100005];\n int level_bob[100005];\n bool vis[100005];\n \n int pa[100005];\n \n long long max_amount=-1e9;\n vector<int>Amount;\npublic:\n int mostProfitablePath(vector<vector<int>>& edges, int bob, vector<int>& amount) {\n Amount = amou...
5
0
['C++']
0
most-profitable-path-in-a-tree
C++ 🚀 | 2 DFS 🥳 | Intuition and Approach 🌟
c-intuition-and-approach-by-jatin1510-hhdf
IntuitionIn this problem, Alice wants to maximize her earnings by moving from the root (node0) to a leaf node, while Bob moves toward the root (0). The difficul
jatin1510
NORMAL
2025-02-24T14:14:28.258508+00:00
2025-02-24T14:21:08.950094+00:00
378
false
# Intuition In this problem, Alice wants to maximize her earnings by moving from the root (node `0`) to a leaf node, while Bob moves toward the root (`0`). The difficulty arises because: - If Bob reaches a node before Alice, the reward is completely lost. - If Alice and Bob reach the node at the same time, they split t...
4
0
['Tree', 'Depth-First Search', 'Graph', 'C++']
0
most-profitable-path-in-a-tree
Only 1 DFS call C++ solution.
only-1-dfs-call-c-solution-by-saraldwive-6j2g
IntuitionWe are given an undirected tree with 𝑛 nodes labeled from 0 to 𝑛−1, rooted at node 0. The tree is represented by the edges list, where each entry [𝑎𝑖,𝑏
SaralDwivedi21
NORMAL
2025-02-24T12:26:40.214196+00:00
2025-02-24T12:26:40.214196+00:00
239
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> We are given an undirected tree with 𝑛 nodes labeled from 0 to 𝑛−1, rooted at node 0. The tree is represented by the edges list, where each entry [𝑎𝑖,𝑏𝑖] represents an edge between nodes 𝑎𝑖 and 𝑏𝑖. At each node 𝑖,there is a gate...
4
0
['C++']
1
most-profitable-path-in-a-tree
DFS || Hard Solution || Best Solution || Beats 20% Users||
dfs-hard-solution-best-solution-beats-20-xqgb
IntuitionApproachComplexity Time complexity: Space complexity: Code
Heisenberg_wc
NORMAL
2025-02-24T05:52:13.175991+00:00
2025-02-24T05:52:13.175991+00:00
369
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code `...
4
0
['C++']
0
most-profitable-path-in-a-tree
🤯10 Lines C++ Concise Code 🚀
10-lines-c-concise-code-by-sapilol-u9ui
C++
LeadingTheAbyss
NORMAL
2025-02-24T05:06:14.558875+00:00
2025-02-24T05:09:54.958258+00:00
385
false
# C++ ```cpp int mostProfitablePath(vector<vector<int>>& e, int b, vector<int>& a) { int n = a.size(), ans = -1e9; vector<vector<int>> g(n); for(auto&e:e) g[e[0]].push_back(e[1]),g[e[1]].push_back(e[0]); vector<int> p(n,-1) , nums(n,1e9); function<void(int,int)> f=[&](int u,int par)...
4
0
['Depth-First Search', 'Graph', 'C++']
0
most-profitable-path-in-a-tree
Single DFS, simple approach with Video
single-dfs-simple-approach-with-video-by-jn6i
YouTube solutionIntuition and approachwe can solve it in single DFS pass with below intuition1)lets keep track the distance of every node from bobwhy ?? how??be
vinod_aka_veenu
NORMAL
2025-02-24T03:26:14.935375+00:00
2025-02-24T03:26:14.935375+00:00
1,127
false
# YouTube solution https://youtu.be/eflbh5qS_68?si=BYHfj_XBktuvhw2K # Intuition and approach we can solve it in single DFS pass with below intuition 1)lets keep track the distance of every node from bob **why ?? how??** because we need to keep track when Alic would reach at some node what would the reaching time is ...
4
0
['Java']
1
most-profitable-path-in-a-tree
C++ Solution Using BFS and DFS
c-solution-using-bfs-and-dfs-by-dellwynt-u0st
\n# Approach\n Describe your approach to solving the problem. \n\nWhat is given to us is an undirected tree rooted at node 0. Now, bob moves from a given node t
dellwyntennison
NORMAL
2023-07-06T12:07:33.685244+00:00
2023-07-06T12:07:33.685261+00:00
465
false
\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\nWhat is given to us is an undirected tree rooted at node `0`. Now, bob moves from a given node to the root `0`. In a tree, there is always only one path between any two nodes. Therefore, we can identify which nodes Bob passes through and also the ...
4
0
['Tree', 'Depth-First Search', 'Breadth-First Search', 'C++']
1
most-profitable-path-in-a-tree
[Python3] Easiest | DFS + BFS | O(N) |
python3-easiest-dfs-bfs-on-by-shriyansna-hbkt
Intuition\nSince this is a tree we know that there is only 1 path between any 2 given nodes. With this knowledge we now know that bob only has one path so we ca
shriyansnaik
NORMAL
2023-01-06T07:37:43.263946+00:00
2023-01-06T07:37:43.263990+00:00
500
false
# Intuition\nSince this is a tree we know that there is only 1 path between any 2 given nodes. With this knowledge we now know that bob only has one path so we can actually record at what step bob reaches a given gate. Then we can traverse as Alice and take the amount accordingly.\n\n# Approach\n1. Do a DFS and record ...
4
0
['Python3']
0
most-profitable-path-in-a-tree
Java | 2 solutions | DFS | BFS
java-2-solutions-dfs-bfs-by-conchwu-aq4b
2. one DFS\n\n\t//2. one DFS\n //Runtime: 82 ms, faster than 100.00% of Java online submissions for Most Profitable Path in a Tree.\n //Memory Usage: 185.
conchwu
NORMAL
2022-11-12T18:36:37.212943+00:00
2022-11-12T18:36:45.066232+00:00
985
false
# 2. one DFS\n```\n\t//2. one DFS\n //Runtime: 82 ms, faster than 100.00% of Java online submissions for Most Profitable Path in a Tree.\n //Memory Usage: 185.1 MB, less than 25.00% of Java online submissions for Most Profitable Path in a Tree.\n public int mostProfitablePath(int[][] edges, int bob, int[] amou...
4
0
['Depth-First Search', 'Breadth-First Search', 'Java']
1
most-profitable-path-in-a-tree
Video Explanation (zero to building the solution itself with inutitions)
video-explanation-zero-to-building-the-s-uwdk
https://www.youtube.com/watch?v=QBb8zUX2KlY\n\nClick here if the preview doesn\'t work
codingmohan
NORMAL
2022-11-12T17:37:48.889308+00:00
2022-11-12T17:37:48.889355+00:00
459
false
https://www.youtube.com/watch?v=QBb8zUX2KlY\n\n[Click here if the preview doesn\'t work](https://www.youtube.com/watch?v=QBb8zUX2KlY)
4
0
['C++']
1
most-profitable-path-in-a-tree
✅✅✅ Java visit one level at the time O(N)
java-visit-one-level-at-the-time-on-by-r-66d6
We first build the tree from the root so that each node has its children and parent set\n Then we start from root, and then we visit each level at the time. \n
ricola
NORMAL
2022-11-12T17:06:27.403507+00:00
2022-11-13T10:35:57.218351+00:00
738
false
* We first build the tree from the root so that each node has its children and parent set\n* Then we start from root, and then we visit each level at the time. \n* While Alice goes down one level, Bob goes up one level. If Bob is at the same node, we split the amount. Once Bob visits a node, we set its amount to 0\n* F...
4
0
[]
0
most-profitable-path-in-a-tree
py soltion :(
py-soltion-by-noam971-dbxz
IntuitionApproachComplexity Time complexity: Space complexity: Code
noam971
NORMAL
2025-02-24T14:24:24.601594+00:00
2025-02-24T14:24:24.601594+00:00
119
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code `...
3
0
['Python3']
1
most-profitable-path-in-a-tree
🚀 Most Profitable Path in a Tree | Optimized BFS Approach ✨
most-profitable-path-in-a-tree-optimized-i7zb
IntuitionThe problem involves traversing a tree (represented as an undirected graph) to find the most profitable path for a player, while considering the moveme
Ajay_Prabhu
NORMAL
2025-02-24T14:11:09.770344+00:00
2025-02-24T14:11:09.770344+00:00
134
false
### Intuition The problem involves traversing a tree (represented as an undirected graph) to find the most profitable path for a player, while considering the movement of another player (Bob) who is trying to reach the root (node 0). The goal is to maximize the player's score by collecting points from nodes, while Bob'...
3
0
['Array', 'Tree', 'Breadth-First Search', 'Graph', 'Java']
0
most-profitable-path-in-a-tree
Fast C and Python3 solutions
fast-c-and-python3-solutions-by-lequan-76sy
ApproachConstruct the graph. Coerce graph in to tree. Compute bob's path.Modify amount with respect to bob's path. Traverse the treeand compute the maximum scor
lequan
NORMAL
2025-02-24T05:16:36.166223+00:00
2025-02-24T05:16:36.166223+00:00
149
false
# Approach <!-- Describe your approach to solving the problem. --> Construct the graph. Coerce graph in to tree. Compute bob's path. Modify amount with respect to bob's path. Traverse the tree and compute the maximum score. # Complexity - Time complexity: $$O(n)$$. All traversals are bounded by the number of nodes. ...
3
0
['C', 'Python3']
0
most-profitable-path-in-a-tree
Python | Two DFS | O(n), O(n) | Beats 99%
python-two-dfs-on-on-beats-99-by-2pillow-1yix
CodeComplexity Time complexity: O(n). find_bob_path() and find_alice_path() visit each node once, time is n + n, simplifies to n. Space complexity: O(n). Max
2pillows
NORMAL
2025-02-24T01:55:51.718810+00:00
2025-02-24T06:35:08.884766+00:00
518
false
# Code ```python3 [] class Solution: def mostProfitablePath(self, edges: List[List[int]], bob: int, amount: List[int]) -> int: n = len(amount) graph = [[] for _ in range(n)] # make graph with connected nodes for each node for u, v in edges: graph[u].append(v) graph[v...
3
0
['Depth-First Search', 'Python3']
2
most-profitable-path-in-a-tree
Kotlin || DFS
kotlin-dfs-by-nazmulcuet11-1z03
IntuitionApproachComplexity Time complexity: Space complexity: Code
nazmulcuet11
NORMAL
2025-02-24T01:10:46.582267+00:00
2025-02-24T01:10:46.582267+00:00
58
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code `...
3
0
['Depth-First Search', 'Kotlin']
0
most-profitable-path-in-a-tree
✅ Most Profitable Path in a Tree | JS🔥 | beats 99%🚀 | 🚀highly optimised & easy to understand ✅
most-profitable-path-in-a-tree-js-beats-vi68g
Intuition Here's an optimized JavaScript solution for the problem along with a detailed explanation of the approach This solution efficiently finds the most pro
Naveen_sachan
NORMAL
2025-02-24T00:46:03.475641+00:00
2025-02-24T00:46:03.475641+00:00
533
false
# Intuition - Here's an optimized JavaScript solution for the problem along with a detailed explanation of the approach - This solution efficiently finds the most profitable path while handling simultaneous arrivals of Alice and Bob correctly # Approach > **Graph Construction:** - We first construct an adjacency list r...
3
0
['JavaScript']
1
most-profitable-path-in-a-tree
☑️✅Easy JAVA Solution || DFS Solution Explained Step by Step✅☑️
easy-java-solution-dfs-solution-explaine-rqij
Intuition\nWe will first traverse bob towards 0 and storing the time at which he visited the node and then we will traverse alice towards leaf node.\n Describe
vritant-goyal
NORMAL
2024-02-26T18:12:24.597785+00:00
2024-02-27T16:22:29.289613+00:00
418
false
# Intuition\nWe will first traverse bob towards 0 and storing the time at which he visited the node and then we will traverse alice towards leaf node.\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nExplained Step by Step in Code.\n<!-- Describe your approach to solving the problem. ...
3
0
['Depth-First Search', 'Graph', 'Java']
0
most-profitable-path-in-a-tree
Simple Two traversal DFS C++ Solution
simple-two-traversal-dfs-c-solution-by-d-lh53
Intuition\n Describe your first thoughts on how to solve this problem. \n# Just two traversals one for Bob and other for Alice\n# Approach\n Describe your appro
dhairyarajbabbar
NORMAL
2023-08-14T18:01:15.797618+00:00
2023-08-14T18:01:15.797649+00:00
376
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n# **Just two traversals one for Bob and other for Alice**\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- I first traversed to find the node from where Bob is starting and then marked the half path node vals as 0 a...
3
0
['Tree', 'Depth-First Search', 'C++']
0
most-profitable-path-in-a-tree
C++|| Easiest Solution || Detailed Solution Fully Explained || DFS + BFS || Graph
c-easiest-solution-detailed-solution-ful-15j8
Intuition\n1.First store the time take by bob to reach the node 0 and the nodes in the path mark their time when will bob reach at them while reaching 0\n2.Now
yeah_boi123
NORMAL
2023-05-24T03:47:11.022370+00:00
2023-05-24T03:47:11.022400+00:00
143
false
# Intuition\n1.First store the time take by bob to reach the node 0 and the nodes in the path mark their time when will bob reach at them while reaching 0\n2.Now come to alice start his traversal and check who has first reached at that node alice or bob if alice reached then add the amount to alice if both at same time...
3
0
['Depth-First Search', 'Breadth-First Search', 'Recursion', 'C++']
0
most-profitable-path-in-a-tree
Easy, Intuitive JS Solution | 1 BFS & 1 DFS | Backtracking
easy-intuitive-js-solution-1-bfs-1-dfs-b-5f7b
Easy intuitive JS solution using backtracking\n First determine Bob\'s path to Root\n Do a short BFS and then constrcut the path using prev array\n* Once you ha
loid_forger
NORMAL
2022-11-15T05:16:12.783330+00:00
2022-11-15T05:16:12.783379+00:00
1,126
false
### Easy intuitive JS solution using backtracking\n* First determine Bob\'s path to Root\n* Do a short BFS and then constrcut the path using prev array\n* Once you have Bob\'s step outlined we can just use backtracking to seek out all the paths and find the one with max profits\n```\n/**\n * @param {number[][]} edges\n...
3
0
['Backtracking', 'Depth-First Search', 'Breadth-First Search', 'JavaScript']
0
most-profitable-path-in-a-tree
Python 3, DFS Backtracking with Explanation
python-3-dfs-backtracking-with-explanati-etpf
Since the undirected tree has n nodes and n-1 edges, there is only one path from bob to root 0. The path from bob to root 0 as well as the time Bob reaches each
cuongmng
NORMAL
2022-11-13T15:04:24.205387+00:00
2022-11-13T15:04:24.205447+00:00
546
false
Since the undirected tree has $$n$$ nodes and $$n-1$$ edges, there is only one path from bob to root $$0$$. The path from bob to root 0 as well as the time Bob reaches each node on the path is used to compute the reward (price) Alice gets at each node. \n\nWe will first use a DFS backtracking to determine Bob\'s path. ...
3
0
['Backtracking', 'Depth-First Search', 'Python3']
0
most-profitable-path-in-a-tree
Asked in Intuit Coding Round Recently. See my solution
asked-in-intuit-coding-round-recently-se-yx0f
One month ago, I gave Intuit coding round for FTE. This exact problem had come in it. Wasn\'t able to solve it then. Amazed to see it appear on Leetcode contest
aknov711
NORMAL
2022-11-13T05:57:00.992495+00:00
2022-11-13T05:58:31.120774+00:00
653
false
One month ago, I gave Intuit coding round for FTE. This exact problem had come in it. Wasn\'t able to solve it then. Amazed to see it appear on Leetcode contest. xD! \n```\nclass Solution {\nvoid dfs(int s,int par,vector<int> adj[],int p[]){\n p[s]=par;\n for(auto e:adj[s]){\n if(e==par)\n continu...
3
0
[]
0
most-profitable-path-in-a-tree
easy short efficient clean code
easy-short-efficient-clean-code-by-maver-e6hs
There exists exactly 1 path for Bob to reach 0. FIXED!\nAny Alice path from 0 to a leaf will have some of its prefix common with Bob\'s path. ( 0, a, b, ...)\nS
maverick09
NORMAL
2022-11-12T16:03:29.909642+00:00
2022-11-12T16:15:46.086432+00:00
564
false
There exists exactly 1 path for Bob to reach 0. FIXED!\nAny Alice path from 0 to a leaf will have some of its prefix common with Bob\'s path. ( 0, a, b, ...)\nSince the graph is undirected, therefore apart from this prefix, there cannot be any nod common between Bob\'s path and Alice\'s this path.\nSo all that remains ...
3
0
['Greedy', 'Depth-First Search']
0
most-profitable-path-in-a-tree
Simple DFS clearly Expained
simple-dfs-clearly-expained-by-aadithya1-jk2v
ApproachUse DFS. Try to visit children first and get the maximum value we can get children to root nodes and add root value and return back to parent. When you
aadithya18
NORMAL
2025-02-24T19:17:09.882754+00:00
2025-02-24T19:17:09.882754+00:00
49
false
# Approach <!-- Describe your first thoughts on how to solve this problem. --> Use DFS. Try to visit children first and get the maximum value we can get children to root nodes and add root value and return back to parent. When you see Bob node in between, Remember on which level we encountered bob node. Check the curre...
2
0
['Array', 'Tree', 'Depth-First Search', 'Graph', 'C++']
0
most-profitable-path-in-a-tree
C# Solution for Most Profitable Path In a Tree Problem
c-solution-for-most-profitable-path-in-a-brvm
IntuitionThe problem requires Alice to maximize her net income while moving towards a leaf in a tree, while Bob moves towards the root. Since Bob can affect Ali
Aman_Raj_Sinha
NORMAL
2025-02-24T19:03:45.929020+00:00
2025-02-24T19:03:45.929020+00:00
68
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> The problem requires Alice to maximize her net income while moving towards a leaf in a tree, while Bob moves towards the root. Since Bob can affect Alice’s profit by reaching a node before or at the same time as her, we must carefully track...
2
0
['C#']
0
most-profitable-path-in-a-tree
Alice and Bob || C++ || BFS/DFS.
alice-and-bob-c-bfsdfs-by-rishiinsane-zmob
IntuitionSince alice's val needs to be maximised based on the path that bob chooses hence we try and find a path to node 0 for bob first and store the timestamp
RishiINSANE
NORMAL
2025-02-24T17:37:23.256787+00:00
2025-02-24T17:37:23.256787+00:00
70
false
# Intuition Since alice's val needs to be maximised based on the path that bob chooses hence we try and find a path to node 0 for bob first and store the timestamps accordingly. Then we move alice from node 0 to a leaf node other than 0 such that the val collected by him is maximum based on the path of bob. Alice gains...
2
0
['C++']
0
most-profitable-path-in-a-tree
Functional JavaScript 1-liner recursion 100%
functional-javascript-1-liner-recursion-usfgp
IntuitionBuild graph as adjacency list.Traverse graph starting from root 0 node, keeping track of current depth.Return maximum profit from each subtree.On the w
Serhii_Hrynko
NORMAL
2025-02-24T16:45:56.238461+00:00
2025-02-24T16:46:39.990990+00:00
53
false
# Intuition Build graph as adjacency list. Traverse graph starting from root 0 node, keeping track of current depth. Return maximum profit from each subtree. On the way back to root, move Bob to parent node and keep track of number of steps taken by Bob. Once Bob took as many steps as Alice, ignore Bob. ``` mostProf...
2
0
['JavaScript']
0
most-profitable-path-in-a-tree
dfs_alice + dfs_bob :)
dfs_alice-dsf_bob-by-tavvfikk-i0ea
IntuitionApproachComplexity Time complexity: Space complexity: Code
tavvfikk
NORMAL
2025-02-24T16:24:24.631114+00:00
2025-02-24T16:31:52.257516+00:00
48
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code `...
2
0
['Depth-First Search', 'C++']
0
most-profitable-path-in-a-tree
✅✅ Using DFS || Simple Traversal ||Easy Solution
using-dfs-simple-traversal-easy-solution-wg6d
IntuitionAlice and Bob start at different nodes in a tree, and we need to determine the maximum income Alice can collect while considering Bob's path. Bob moves
Karan_Aggarwal
NORMAL
2025-02-24T15:57:37.724192+00:00
2025-02-24T15:57:37.724192+00:00
12
false
# Intuition Alice and Bob start at different nodes in a tree, and we need to determine the maximum income Alice can collect while considering Bob's path. Bob moves optimally toward node 0, and Alice can collect full, half, or no amount from each node based on whether Bob reached it first, at the same time, or later. #...
2
0
['Array', 'Tree', 'Depth-First Search', 'Graph', 'C++']
0
most-profitable-path-in-a-tree
Optimal DFS Strategy: Maximizing Alice’s Profit While Tracking Bob’s Path in a Tree
optimal-dfs-strategy-maximizing-alices-p-g6tk
🔼 Please Upvote🔼 Please Upvote🔼 Please Upvote🔼 Please Upvote💡If this helped, don’t forget to upvote! 🚀🔥IntuitionAlice and Bob move through the tree, collecting
alperensumeroglu
NORMAL
2025-02-24T14:40:21.274066+00:00
2025-02-24T14:40:21.274066+00:00
10
false
**🔼 Please Upvote 🔼 Please Upvote 🔼 Please Upvote 🔼 Please Upvote** **💡If this helped, don’t forget to upvote! 🚀🔥** # Intuition <!-- Describe your first thoughts on how to solve this problem. --> Alice and Bob move through the tree, collecting amounts from nodes. The challenge is to determine the most profitabl...
0
0
['Python3']
0
most-profitable-path-in-a-tree
Step by Step Explained C++ Solution | TC - 0(N) | SC - 0(N) | DFS | Easy
step-by-step-explained-c-solution-tc-0n-wscil
Complexity Time complexity:O(N) Space complexity:O(N) Code
ujjwal141
NORMAL
2025-02-24T14:15:41.413189+00:00
2025-02-24T14:18:40.876498+00:00
87
false
# Complexity - Time complexity: O(N) - Space complexity: O(N) # Code ```cpp [] class Solution { public: // TC - O(N) & SC - O(N) int ans = INT_MIN; // contain answer unordered_map<int, int> mp; // node -> distance bool pathTileZero(int bob, int cnt, unordered_map<int, vector<int>> &ad...
2
0
['Tree', 'Depth-First Search', 'Graph', 'C++']
0
most-profitable-path-in-a-tree
Single DFS solution with explanation | 199~227ms (75~100) | 111.14~117.5 (25~50)
single-dfs-solution-with-explanation-199-zyna
IntuitionWe can rethink the problem by breaking it down into two subproblems: Bob moves from his starting node along the unique path toward the root node. The t
whats2000
NORMAL
2025-02-24T10:12:29.659992+00:00
2025-02-24T10:12:29.659992+00:00
154
false
# Intuition We can rethink the problem by breaking it down into two subproblems: - Bob moves from his starting node along the unique path toward the root node. The time Bob reaches each node can be understood as the shortest distance from Bob's starting node to that node. - Alice starts from the root node and travers...
2
0
['Array', 'Tree', 'Depth-First Search', 'Graph', 'Simulation', 'TypeScript', 'JavaScript']
0
most-profitable-path-in-a-tree
Breadth First Search Approach
breadth-first-search-approach-by-biniyam-6opd
ApproachFirst find bob's path, then iterate through the graph for both alice and bob while tracking which node bob visited before alice reached there. Also don'
biniyamnegasa17
NORMAL
2025-02-24T06:25:35.582454+00:00
2025-02-24T06:25:35.582454+00:00
108
false
# Approach <!-- Describe your approach to solving the problem. --> First find bob's path, then iterate through the graph for both alice and bob while tracking which node bob visited before alice reached there. Also don't forget to track alice's score, then update the maximum value alice has, when you are at a leaf node...
2
0
['Python3']
1
most-profitable-path-in-a-tree
Simple Easy Solution | Cpp | DFS + BFS
simple-easy-solution-cpp-dfs-bfs-by-its_-5vs3
IntuitionFor Bob there will be only one way to reach the root node. So find the bob path and mark the distance/time for reaching each node. Then do the bfs for
its_navneet
NORMAL
2025-02-24T06:23:31.540923+00:00
2025-02-24T06:23:31.540923+00:00
326
false
# Intuition For Bob there will be only one way to reach the root node. So find the bob path and mark the distance/time for reaching each node. Then do the bfs for Alice and get all possible sum to reach the child node. # Complexity - Time complexity: $$O(n)$$ # Code ```cpp [] class Solution { void dfs(int no...
2
0
['C++']
0
most-profitable-path-in-a-tree
[C++] Two DFS
c-two-dfs-by-bora_marian-75am
The key observation for this problem is that both Alice and Bob are moving in a tree. There is one single way for Bob to move from parent to parent till node 0.
bora_marian
NORMAL
2025-02-24T05:49:43.938839+00:00
2025-02-24T05:49:43.938839+00:00
148
false
The key observation for this problem is that both Alice and Bob are moving in a tree. There is one single way for Bob to move from parent to parent till node 0. For Alice we can calculate the way to all nodes using DFS and the amount she can reach the node. Steps: - calculate bob path and add the number of steps ne...
2
0
['C++']
0
most-profitable-path-in-a-tree
Easy code | Must try | Beats 80%
easy-code-must-try-beats-80-by-notaditya-zoj1
IntuitionApproachComplexity Time complexity: Space complexity: Code
NotAditya09
NORMAL
2025-02-24T05:36:50.632701+00:00
2025-02-24T05:36:50.632701+00:00
386
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code `...
2
0
['Java']
0
most-profitable-path-in-a-tree
EASY TO UNDERSTAND | JAVA | Explained With Comments !!!
easy-to-understand-java-explained-with-c-a6rr
Code
Siddharth_Bahuguna
NORMAL
2025-02-24T03:34:48.996132+00:00
2025-02-24T03:34:48.996132+00:00
307
false
# Code ```java [] class Solution { public int mostProfitablePath(int[][] edges, int bob, int[] amount) { int n = amount.length; tree = new ArrayList<>(); bobPath = new HashMap<>(); visited = new boolean[n]; for (int i = 0; i < n; i++) { tree.add(new ArrayList<>(...
2
0
['Java']
0
most-profitable-path-in-a-tree
Easy To Understand C++ Solution || (Using DFS) ✅✅
easy-to-understand-c-solution-using-dfs-4gewm
Code\n\nclass Solution {\npublic:\n int dfs(vector<int> *adj,vector<int> &values,vector<int> &vis,vector<int> &dis,int node,int time){\n vis[node]=1;\
Abhi242
NORMAL
2024-02-26T19:25:48.476662+00:00
2024-02-26T19:25:48.476690+00:00
197
false
# Code\n```\nclass Solution {\npublic:\n int dfs(vector<int> *adj,vector<int> &values,vector<int> &vis,vector<int> &dis,int node,int time){\n vis[node]=1;\n int ans=-1e8;\n int cost=0;\n if(dis[node]>time){\n cost=values[node];\n }else if(dis[node]==time){\n c...
2
0
['Tree', 'Depth-First Search', 'C++']
0
most-profitable-path-in-a-tree
DFS+BFS || C++ ✔✔
dfsbfs-c-by-deepak_5910-khye
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
Deepak_5910
NORMAL
2023-10-19T20:03:58.084053+00:00
2023-10-19T20:03:58.084075+00:00
264
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
2
0
['Depth-First Search', 'Breadth-First Search', 'C++']
1
most-profitable-path-in-a-tree
DFS + BFS || easy beginner friendly solution
dfs-bfs-easy-beginner-friendly-solution-aywq7
we will find the path for bob from his current position to root with time stamp and as it\'s given that it\'s tree so their will be single path so it makes ever
demon_code
NORMAL
2023-08-17T01:47:29.205392+00:00
2023-08-17T01:47:29.205423+00:00
279
false
we will find the path for bob from his current position to root with time stamp and as it\'s given that it\'s tree so their will be single path so it makes everything stright forward.\nwe will use map <node -> time> to store info for easy access later\n\nnow once we are done with bob then come to alice \nwe are gpoing ...
2
0
['Depth-First Search', 'Breadth-First Search', 'C']
0
most-profitable-path-in-a-tree
Simple DFS Solution, bets 97% runtime
simple-dfs-solution-bets-97-runtime-by-v-bphg
Intuition\n1) Calculate the distance of nodes in the path of bob from his initial position. \n2) Make dfs call from 0 to get the maximum value.\n3) Make sure to
vivek_sangwan
NORMAL
2023-04-05T15:36:41.908756+00:00
2023-04-05T15:36:41.908784+00:00
146
false
# Intuition\n1) Calculate the distance of nodes in the path of bob from his initial position. \n2) Make dfs call from 0 to get the maximum value.\n3) Make sure to check for overflow and root node codition\n\nUpvote if you like the solution.\n\n# Code\n```\nclass Solution {\n public int mostProfitablePath(int[][] edg...
2
0
['Tree', 'Depth-First Search', 'Java']
1
most-profitable-path-in-a-tree
C++ Easy/Medium , Explained with diagram
c-easymedium-explained-with-diagram-by-n-sq8x
Intuition\nThere is always a uniue path between two nodes in a tree.\nSo we need to see the path between Alice and Bob , this will be the only path that will be
neembu_mirch
NORMAL
2023-01-02T06:31:37.662429+00:00
2023-01-02T06:31:37.662476+00:00
339
false
# Intuition\nThere is always a uniue path between two nodes in a tree.\nSo we need to see the path between Alice and Bob , this will be the only path that will be affected by movement of both Alice and Bob , rest paths depend only on Alice.\n\n# Approach\nFind the path between Alice and Bob , if this path contain odd n...
2
0
['C++']
0
most-profitable-path-in-a-tree
Tracking Bob
tracking-bob-by-ayushy_78-kgjv
Intuition\nAs Bob is moving toward the root so it\'s definite that there is only one path for Bob. \nWhile Alice a varity of options dfs of the tree.\nWe find t
SelfHelp
NORMAL
2022-11-21T18:26:16.709099+00:00
2022-11-21T18:26:16.709141+00:00
248
false
# Intuition\nAs Bob is moving toward the root so it\'s definite that there is only one path for Bob. \nWhile Alice a varity of options dfs of the tree.\nWe find that Path for Bob then we do dfs for Alice to find the maximum profitable path.\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Appro...
2
0
['Hash Table', 'Backtracking', 'Tree', 'Depth-First Search', 'C++']
1
most-profitable-path-in-a-tree
Python 3 beats 100%
python-3-beats-100-by-mmmichelle27-buqs
\tclass Solution:\n def mostProfitablePath(self, edges: List[List[int]], bob: int, amount: List[int]) -> int:\n adj = defaultdict(list)\n \n
Mmmichelle27
NORMAL
2022-11-13T18:29:03.524866+00:00
2022-11-13T18:29:03.524901+00:00
237
false
\tclass Solution:\n def mostProfitablePath(self, edges: List[List[int]], bob: int, amount: List[int]) -> int:\n adj = defaultdict(list)\n \n for a, b in edges:\n adj[a].append(b)\n adj[b].append(a)\n \n # Update the tree based on Bob\n bRoute = []\n ...
2
0
[]
1
most-profitable-path-in-a-tree
DFS [Fully Explained with Comments]
dfs-fully-explained-with-comments-by-ms9-422x
Approach:\nNOTE: Alice can Move only Down , Bob only Above as per Ques\n1. Move Bob first from node B to node 0 , updating amount to 0(if Bob travels before Ali
MSJi
NORMAL
2022-11-12T21:51:10.180346+00:00
2022-11-12T21:51:10.180381+00:00
314
false
**Approach:**\nNOTE: Alice can Move only Down , Bob only Above as per Ques\n1. Move Bob first from node B to node 0 , updating amount to 0(if Bob travels before Alice , as the gate is OPEN for Alice) **or** amount/2 (if both reach simultaneously)\n2. Find Max path sum from node 0 to leaf Node (**answer may or may NOT i...
2
0
['Depth-First Search', 'C']
1
most-profitable-path-in-a-tree
Python | DFS approach
python-dfs-approach-by-divyanshugairola-qgo7
Intuition\n Describe your first thoughts on how to solve this problem. \nAlice can reach to Bob from one path only as no. of edges is n - 1 and it is given that
divyanshugairola
NORMAL
2022-11-12T16:20:21.759594+00:00
2022-11-12T16:20:21.759620+00:00
565
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nAlice can reach to Bob from one path only as no. of edges is n - 1 and it is given that it is a tree.\n\n**First DFS**\nWe will first run a dfs to find the bob and in post order while returning from that path update the amount that will b...
2
0
['Tree', 'Depth-First Search', 'Python', 'Python3']
0
most-profitable-path-in-a-tree
C++ || DFS + BFS || TIME 100% faster O(n), SPACE 100% smaller O(n)
c-dfs-bfs-time-100-faster-on-space-100-s-llvq
First step: Do DFS to find the Bob path (only one possible path) to reach node 0, and save the path in Vpath_B \nSecond step: Do BFS on Alice, while at each st
tangerrine2112
NORMAL
2022-11-12T16:19:01.166354+00:00
2022-11-12T16:19:01.166417+00:00
934
false
First step: Do DFS to find the Bob path (only one possible path) to reach node 0, and save the path in Vpath_B \nSecond step: Do BFS on Alice, while at each step of BFS move Bob a node further by deleting one node from Vpath_B\n\n```\nclass Solution {\nprivate:\n vector<vector<int>> Adj;\n vector<int> visited;\n...
2
0
[]
0
most-profitable-path-in-a-tree
DFS + BT
dfs-bt-by-claytonjwong-htcm
Perform DFS + BT (depth-first-search with back-tracking)\n\n1. Find Bob\'s path to node 0 via DFS + BT\n2. Then DFS + BT all possibilities for Alice\'s path in
claytonjwong
NORMAL
2022-11-12T16:00:26.419730+00:00
2022-11-13T16:03:19.683011+00:00
1,885
false
Perform DFS + BT (depth-first-search with back-tracking)\n\n1. Find Bob\'s path to node `0` via DFS + BT\n2. Then DFS + BT all possibilities for Alice\'s path in parallel with each `i`<sup>th</sup> node of Bob\'s path\n\nThere\'s 3 possibilities for `cost` we can `take` which is accumulated onto what we `have`:\n\n1. i...
2
0
[]
4