2026 January 31 Problems

45 views
Skip to first unread message

daryl...@gmail.com

unread,
Jan 31, 2026, 11:50:24 AM (12 days ago) Jan 31
to leetcode-meetup
Feel free to work on any of the problems you want; we'll have people present their solutions at 11:30.

Please post your solutions to this thread so others can use this as a reference.

For the easy inorder traversal problem, try more sophisticated approaches such as Morris traversal.



3710. Maximum Partition Factor Hard 30.8%

We're going to use the same Google meet link as the last few times: https://meet.google.com/xnn-kifj-jnx?pli=1

Anuj Patnaik

unread,
Jan 31, 2026, 12:22:06 PM (12 days ago) Jan 31
to leetcode-meetup
class Solution:
def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
values = []
if root == None:
return []
else:
return Solution.inorderTraversal(self, root.left) + [root.val] + Solution.inorderTraversal(self, root.right)

Anuj Patnaik

unread,
Jan 31, 2026, 12:23:04 PM (12 days ago) Jan 31
to leetcode-meetup
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
values = []
stack = []
current = root
while current is not None or len(stack) > 0:
while current is not None:
stack.append(current)
current = current.left
current = stack.pop()
values.append(current.val)
current = current.right
return values

Allen S.

unread,
Jan 31, 2026, 1:12:29 PM (12 days ago) Jan 31
to leetcode-meetup
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func inorderTraversal(root *TreeNode) []int {
if root == nil {
return nil
}
l := inorderTraversal(root.Left)
r := inorderTraversal(root.Right)
return append(
append(l, root.Val),
r...,
)
}

On Saturday, January 31, 2026 at 8:50:24 AM UTC-8 daryl...@gmail.com wrote:

Anuj Patnaik

unread,
Jan 31, 2026, 1:43:09 PM (12 days ago) Jan 31
to leetcode-meetup
class Solution:
def maxAmount(self, initialCurrency: str, pairs1: List[List[str]], rates1: List[float], pairs2: List[List[str]], rates2: List[float]) -> float:
day_1_exchange = {initialCurrency: 1.0}
for i in range(len(pairs1)):
for j in range(len(pairs1)):
curr1 = pairs1[j][0]
curr2 = pairs1[j][1]
rate = rates1[j]
if curr1 in day_1_exchange:
value = day_1_exchange[curr1] * rate
if curr2 not in day_1_exchange or value > day_1_exchange[curr2]:
day_1_exchange[curr2] = value
if curr2 in day_1_exchange:
value = day_1_exchange[curr2] / rate
if curr1 not in day_1_exchange or value > day_1_exchange[curr1]:
day_1_exchange[curr1] = value
day_2_exchange = day_1_exchange
for i in range(len(pairs2)):
for j in range(len(pairs2)):
curr1 = pairs2[j][0]
curr2 = pairs2[j][1]
rate = rates2[j]
if curr1 in day_2_exchange:
value = day_2_exchange[curr1] * rate
if curr2 not in day_2_exchange or value > day_2_exchange[curr2]:
day_2_exchange[curr2] = value
if curr2 in day_2_exchange:
value = day_2_exchange[curr2] / rate
if curr1 not in day_2_exchange or value > day_2_exchange[curr1]:
day_2_exchange[curr1] = value
return day_2_exchange[initialCurrency]

Bhupathi Kakarlapudi

unread,
Jan 31, 2026, 1:53:30 PM (12 days ago) Jan 31
to leetcod...@googlegroups.com
class Solution:
def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
result:list = []

def dfs(node: Optional[TreeNode]):
if node is None:
return
dfs(node.left)
result.append(node.val)
dfs(node.right)
return

dfs(root)

return result

--
whatspp group: http://whatsapp.techbayarea.us/
---
You received this message because you are subscribed to the Google Groups "leetcode-meetup" group.
To unsubscribe from this group and stop receiving emails from it, send an email to leetcode-meet...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/leetcode-meetup/20229ae5-0c8f-43dd-a77e-bd6b043f2b44n%40googlegroups.com.

Srihari Tadala

unread,
Jan 31, 2026, 1:55:45 PM (12 days ago) Jan 31
to leetcod...@googlegroups.com
Hi Team, 

Would it be possible to share the meeting recording  after the meeting if have other commitments ? I think it would be really helpful for new grads and mid-level engineers to review how problems are explained, pick up different approaches, and learn from others’ insights especially for interview preparation.

Thanks!



--
whatspp group: http://whatsapp.techbayarea.us/
---
You received this message because you are subscribed to the Google Groups "leetcode-meetup" group.
To unsubscribe from this group and stop receiving emails from it, send an email to leetcode-meet...@googlegroups.com.

vinay

unread,
Jan 31, 2026, 2:16:12 PM (12 days ago) Jan 31
to leetcod...@googlegroups.com
time: o(n)
space: o(h) + o(n)  [stack: O(h) where h = log(n) balanced tree, O(n) skewed tree]

class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        Deque<TreeNode> stack = new ArrayDeque<>();
        while(stack.size() > 0 || root != null){
            while(root != null){
                stack.push(root);
                root = root.left;
            }

            root = stack.pop();
            result.add(root.val);
            root = root.right;
        }

        return result;
    }
}

FloM

unread,
Jan 31, 2026, 2:17:52 PM (12 days ago) Jan 31
to leetcode-meetup
Srihar: No it's not okay to record my voice. I do not give permission for this. I'm not going to be here this meeting, so you have to ask the participants this week. But if they say yes, that does not authorize you to record the next meeting.

-Flo

Jagrut

unread,
Jan 31, 2026, 2:19:34 PM (12 days ago) Jan 31
to leetcode-meetup
Recursive
class Solution_Recursive:
def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
res = []
self._inorder(root, res)
return res

def _inorder(self, node: Optional[TreeNode], res: List[int]) -> None:
if (node):
self._inorder(node.left, res)
res.append(node.val)
self._inorder(node.right, res)

Iterative

class Solution_Iterative:
def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
res = []
stack = []
curr = root

while (curr or stack):
# Keep processing left (L)
while (curr):
stack.append(curr)
curr = curr.left
# curr is null now
curr = stack.pop()
# Store val (N)
res.append(curr.val)
# Now go to right (R)
curr = curr.right

return res
class Solution:
def maxAmount(self, initialCurrency: str, pairs1: List[List[str]], rates1: List[float], pairs2: List[List[str]], rates2: List[float]) -> float:
d1_g = defaultdict(list) # source_currency -> [(target_currency, conversion_rate)]
d2_g = defaultdict(list) # source_currency -> [(target_currency, conversion_rate)]
d1_max = defaultdict(int) # currency -> amount
d2_max = defaultdict(int) # currency -> amount
starting_value = 1

# Populate day 1, 2 graphs
self.populate_graph(pairs1, rates1, d1_g)
self.populate_graph(pairs2, rates2, d2_g)

# Best on day 1
q = [(initialCurrency, starting_value)]
self.find_best_for_day(d1_g, d1_max, q)

# Best for day 2. Seed queue with results of d1_max
q.clear()
for currency, max_amount in d1_max.items():
q.append((currency, max_amount))
self.find_best_for_day(d2_g, d2_max, q)

# Find best for original currency at end of day 2
return max(d2_max[initialCurrency], starting_value)

def find_best_for_day(self, d_g, d_max, q):
precision = 10
seen = set()
while q:
currency, amount = q.pop()
amount = round(amount, precision)
if (currency, amount) not in seen:
seen.add((currency, amount))
# Record the best you can get for this currency
d_max[currency] = max(d_max[currency], amount)
# Find which conversions can happen next
for next_conv in d_g[currency]:
next_conv_currency = next_conv[0]
next_conv_rate = next_conv[1]
converted_amount = next_conv_rate * amount
if (next_conv_currency, converted_amount) not in seen:
q.append((next_conv_currency, converted_amount))

def populate_graph(self, pairs, rates, graph):
for i in range(len(pairs)):
source_currency = pairs[i][0]
target_currency = pairs[i][1]
rate = rates[i]
graph[source_currency].append([target_currency, rate]) # forward
graph[target_currency].append([source_currency, 1 / rate]) # backward


Rag Mur

unread,
Jan 31, 2026, 2:34:31 PM (12 days ago) Jan 31
to leetcode-meetup
class Solution:
def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
traversedNodes = []

def dfs(node):
if node is None:
return
dfs(node.left)
traversedNodes.append(node.val)
dfs(node.right)
dfs(root)
return traversedNodes


Perhaps the worst day. Need to practice some tree questions. 

from collections import defaultdict
class Solution:
def maxAmount(self, initialCurrency: str, pairs1: List[List[str]], rates1: List[float], pairs2: List[List[str]], rates2: List[float]) -> float:
dayOneGraph = defaultdict(list)
dayTwoGraph = defaultdict(list)

# construct graph
graphs = []
all_pairs = [pairs1, pairs2]
all_rates = [rates1, rates2]
number_of_days = len(all_pairs)
for pairs, rates in zip(all_pairs, all_rates):
graph = defaultdict(list)
for pair, rate in zip(pairs, rates):
startCurrency, targetCurrency = pair
graph[startCurrency].append([targetCurrency, rate])
graph[targetCurrency].append([startCurrency, 1/rate])
graphs.append(graph)
# Centinel for avoiding many if else checks
graphs.append(defaultdict(list))
INF = float('inf')
mem = defaultdict(lambda: -INF)
# this is the terminal case
mem[(number_of_days-1 , initialCurrency)] = 1
def dfs(graph_idx, startCurrency):
if graph_idx >= number_of_days:
# exceeded the days
return 0

if mem[(graph_idx, startCurrency)] != -INF:
return mem[(graph_idx, startCurrency)]
current_graph = graphs[graph_idx]
next_graph = graphs[graph_idx+1]

current_graph_return = [rate*dfs(graph_idx, target_currency) for target_currency, rate in current_graph[startCurrency]]
next_graph_return = [rate*dfs(graph_idx+1, target_currency) for target_currency, rate in next_graph[startCurrency]]
maxReturn = max(current_graph_return+next_graph_return)
mem[(graph_idx, startCurrency)] = maxReturn
return mem[(graph_idx, startCurrency)]
return dfs(0, initialCurrency)
On Saturday, January 31, 2026 at 10:12:29 AM UTC-8 allen....@gmail.com wrote:

Allen S.

unread,
Jan 31, 2026, 3:43:33 PM (12 days ago) Jan 31
to leetcode-meetup
func maxAmount(
initialCurrency string,
pairs1 [][]string, rates1 []float64,
pairs2 [][]string, rates2 []float64,
) float64 {

pathRates1 := pathRates(initialCurrency, pairs1, rates1)
pathRates2 := pathRates(initialCurrency, pairs2, rates2)

res := 1.0

for currency1, rate1 := range pathRates1 {
if rate2, ok := pathRates2[currency1]; ok {
res = max(res, rate1 / rate2)
}
}

return res
}

type node struct {
currency string
rate float64
}

func pathRates(initialCurrency string, pairs [][]string, rates []float64) map[string]float64 {
res := make(map[string]float64)

q := []node{node{initialCurrency, 1}}
seen := map[string]bool{initialCurrency:true}
for len(q) > 0 {
n := len(q)
for range n {
now := q[0]
q = q[1:]
// find neighbors
for i, pair := range pairs {
if pair[0] == now.currency && !seen[pair[1]] {
seen[pair[1]] = true
pathRate := now.rate * rates[i]
q = append(q, node{pair[1], pathRate})
res[pair[1]] = pathRate
}
if pair[1] == now.currency && !seen[pair[0]] {
seen[pair[0]] = true
pathRate := now.rate / rates[i]
q = append(q, node{pair[0], pathRate})
res[pair[0]] = pathRate
}
}
}
}

return res
}
Message has been deleted

mnj rm

unread,
Feb 4, 2026, 12:18:28 PM (8 days ago) Feb 4
to leetcode-meetup
Attempt 2 : Jagrut's approach with few optimizations
  • Use the ouput itself as a way to keep track of seen . 
  • Seeding the output with intial currency and start value makes the solution extensible to trading for more than 2 days
from collections import defaultdict, deque
class Solution:
def maxAmount(self, initialCurrency: str, pairs1: List[List[str]], rates1: List[float], pairs2: List[List[str]], rates2: List[float]) -> float:
def constructTradeGraph(currencyPairs, rates):
graph = defaultdict(list)
for pair, rate in zip(currencyPairs, rates):
startCurrency, targetCurrency = pair
graph[startCurrency].append([targetCurrency, rate])
graph[targetCurrency].append([startCurrency, 1/rate])
return graph
def getMaxValueForCurrenciesForDay(seedVals, pairs, rates):
'''
Returns max cash possible for all the currency by executing all possible trade path
'''

tradeGraph = constructTradeGraph(pairs, rates)
# stores the max cash possible for all the currency by executing all possible trade path.
output = defaultdict(float, seedVals)
q = deque(seedVals.items())

while q:
start_currency, start_value = q.popleft()
for end_currency, convertion_rate in tradeGraph[start_currency]:
end_value = start_value * convertion_rate
# this < check ensures that we are not going down any less profitable
# trade path then previously seen.
if output[end_currency] < end_value:
output[end_currency] = end_value
q.append((end_currency, end_value))
return output

# Start with the seed value and get
day1_max_vals = getMaxValueForCurrenciesForDay({initialCurrency: 1.0}, pairs1, rates1)
day2_max_vals = getMaxValueForCurrenciesForDay(day1_max_vals, pairs2, rates2)
return day2_max_vals[initialCurrency]


On Saturday, January 31, 2026 at 8:50:24 AM UTC-8 daryl...@gmail.com wrote:

vinay

unread,
Feb 5, 2026, 1:18:04 AM (7 days ago) Feb 5
to leetcod...@googlegroups.com

BFS with relaxation. Build a graph for each day, track max conversion rates to all currencies after Day 1, then keep relaxing on Day 2's graph and return what you end up with in your starting currency. 
Will explore Bellman-Ford next since it handles the same relaxation idea more formally.  
time: O(V*E)
space: O(V+E)

class Solution {
    public double maxAmount(String initialCurrency, List<List<String>> pairs1, double[] rates1, List<List<String>> pairs2, double[] rates2) {
        Map<String, List<Edge>> day1 = buildGraph(pairs1, rates1);
        Map<String, List<Edge>> day2 = buildGraph(pairs2, rates2);
        Deque<Edge> queue = new ArrayDeque<>();

        Map<String, Double> maxConversions = initConversions(day1.keySet());
        maxConversions.put(initialCurrency, 1.0);

        queue.offer(new Edge(initialCurrency,1.0));

        bfs(maxConversions, day1, queue);

        queue.clear();

        for(Map.Entry<String,Double> entry : maxConversions.entrySet()){
            String currency = entry.getKey();
            double rate = entry.getValue();
            if(rate > 0.0){
                queue.offer(new Edge(currency, rate));
            }
        }

        bfs(maxConversions, day2, queue);

        return maxConversions.getOrDefault(initialCurrency, 1.0);

    }

    public void bfs(Map<String, Double> maxConversions,  Map<String, List<Edge>> day,  Deque<Edge> queue){
        while(!queue.isEmpty()){
            Edge edge = queue.poll();
            String currency = edge.currency;
            double rate = edge.rate;
            for(Edge neighbor : day.getOrDefault(currency, new ArrayList<>())){
                String nCurrency = neighbor.currency;
                double nRate = neighbor.rate;
                if(nRate * rate > maxConversions.getOrDefault(nCurrency, 0.0)){
                    maxConversions.put(nCurrency,nRate*rate);
                    queue.offer(new Edge(nCurrency, nRate*rate));
                }
            }
        }
    }

    public Map<String, Double> initConversions(Set<String> currencies){
        Map<String, Double> maxConversions = new HashMap<>();
        for(String currency : currencies){
            maxConversions.put(currency, 0.0);
        }
        return maxConversions;
    }

    public Map<String, List<Edge>> buildGraph(List<List<String>> pairs, double[] rates){
        Map<String, List<Edge>> graph = new HashMap<>();

        for(int i = 0; i < pairs.size(); i++){
            String source = pairs.get(i).get(0);
            String target = pairs.get(i).get(1);
            double rate = rates[i];
            graph.computeIfAbsent(source, k -> new ArrayList<>()).add(new Edge(target, rate));
            graph.computeIfAbsent(target, k -> new ArrayList<>()).add(new Edge(source, 1.0/rate));
        }
        return graph;
    }


    class Edge{
        String currency;
        double rate;
        public Edge(String cur, double rt){
            currency = cur;
            rate = rt;
        }
    }
}

On Wed, Feb 4, 2026 at 7:33 AM mnj rm <mnj...@gmail.com> wrote:
Attempt 2 : Jagrut's approach with few optimizations
  • Use the ouput itself as a way to keep track of seen . 
  • Seeding the output with intial currency and start value makes the solution extensible to trading for more than 2 days
from collections import defaultdict, deque
class Solution:
def maxAmount(self, initialCurrency: str, pairs1: List[List[str]], rates1: List[float], pairs2: List[List[str]], rates2: List[float]) -> float:
return day2_max_vals[initialCurrency]

Reply all
Reply to author
Forward
0 new messages