2026 February 14 Problems

54 views
Skip to first unread message

daryl...@gmail.com

unread,
Feb 14, 2026, 12:43:34 PMFeb 14
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.


2467. Most Profitable Path in a Tree Medium 67.3%

2306. Naming a Company Hard 46.5%

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

Allen S.

unread,
Feb 14, 2026, 1:33:07 PMFeb 14
to leetcode-meetup
func countPairs(nums []int, k int) int {
valueToIndices := make(map[int][]int) //
res := 0
for i, v := range nums {
if indices, ok := valueToIndices[v]; ok {
for _, index := range indices {
if index * i % k == 0 {
res++
}
}
} else {
valueToIndices[v] = []int{}
}
valueToIndices[v] = append(valueToIndices[v], i)
}
return res
}

FloM

unread,
Feb 14, 2026, 1:36:30 PMFeb 14
to leetcode-meetup
2176. Count Equal and Divisible Pairs in an Array Easy Time: O(n^2), Mem: O(n)
class Solution {
public:
int countPairs(vector<int>& nums, int k) {
unordered_map<int, vector<int>> indicesPerNum;

for(int ii=0; ii<nums.size(); ++ii)
{
if (!indicesPerNum.contains(nums[ii]))
{
indicesPerNum.insert({nums[ii], vector<int>{}});
}

indicesPerNum[nums[ii]].push_back(ii);
}

int count = 0;
for(auto& numIdxes : indicesPerNum)
{
for(int ii=0; ii<numIdxes.second.size(); ++ii)
{
int ltIdx = numIdxes.second[ii];
for(int jj=ii+1; jj<numIdxes.second.size(); ++jj)
{
int rtIdx = numIdxes.second[jj];
if ( (ltIdx * rtIdx) % k == 0)
{
++count;
}
}
}
}

return count;
}
};

Anuj Patnaik

unread,
Feb 14, 2026, 2:08:41 PMFeb 14
to leetcode-meetup
class Solution:
def distinctNames(self, ideas: List[str]) -> int:
prefix_suffix = defaultdict(set)
result = 0
for i in ideas:
suffix = i[1:]
prefix_suffix[i[0]].add(suffix)
prefixes = list(prefix_suffix.keys())
for l in range(len(prefixes)):
for m in range(l+1, len(prefixes)):
suffix_a = prefix_suffix[prefixes[l]]
suffix_b = prefix_suffix[prefixes[m]]
ct = 0
for n in suffix_a:
if n in suffix_b:
ct += 1
result += 2*(len(suffix_a) - ct)*(len(suffix_b) - ct)
return result

Anuj Patnaik

unread,
Feb 14, 2026, 2:10:52 PMFeb 14
to leetcode-meetup
class Solution:
def countPairs(self, nums: List[int], k: int) -> int:
value_to_idx = defaultdict(list)
set_nums = set()
for i in range(len(nums)):
value_to_idx[nums[i]].append(i)
set_nums.add(nums[i])
if len(set_nums) == len(nums):
return 0
result = 0
for j in value_to_idx.values():
print(j)
for r in range(len(j)):
for l in range(r + 1, len(j)):
if (j[r] * j[l]) % k == 0:
print(j[r], j[l])
result += 1
return result






Carlos Green

unread,
Feb 14, 2026, 2:13:47 PMFeb 14
to leetcode-meetup
/**
* @param {number[]} nums
* @param {number} k
* @return {number} Time O(n * m) & Space O(1)
*/
var countPairs = function(nums, k) {
let count = 0

for (let i = 0; i < nums.length; i++) {
for (let j = i + 1; j < nums.length; j++) {
if (nums[i] === nums[j] && (i * j) % k === 0) {
count++
}
}
}
return count
};

FloM

unread,
Feb 14, 2026, 2:20:55 PMFeb 14
to leetcode-meetup
2467. Most Profitable Path in a Tree Medium Time: O(n) Mem: O(n)
class Solution {
public:

void dfsCreateDirAdjList(int node, vector<bool>& marked, const vector<vector<int>>& adjList, vector<vector<int>>& dirAdjList)
{
marked[node] = true;
for(const int& adj : adjList[node])
{
if (!marked[adj])
{
dirAdjList[node].push_back(adj);
dfsCreateDirAdjList(adj, marked, adjList, dirAdjList);
}
}
}

void dfsPathToBob(int node, const vector<vector<int>>& dirAdjList, stack<int>& toBob, const int& bob, bool& foundBob)
{
toBob.push(node);
if (node == bob)
{
foundBob = true;
return;
}

for(const int& adj: dirAdjList[node])
{
dfsPathToBob(adj, dirAdjList, toBob, bob, foundBob);
if (foundBob)
{
return;
}
}
toBob.pop();
}

void dfsCheapestPath(int node, const vector<vector<int>>& dirAdjList, int& cost, const vector<int>& bobToZero, int level, const vector<int>& amount, unordered_set<int>& bobSeen, int& maxCost)
{
int curCost = 0;

level = (level < bobToZero.size()) ? level : bobToZero.size()-1;

bobSeen.insert(bobToZero[level]);

if (bobToZero[level] == node)
{
curCost = (amount[node]/2);
}

if (!bobSeen.contains(node))
{
curCost = amount[node];
}

cost += curCost;

if (dirAdjList[node].size() == 0)
{
maxCost = std::max(cost, maxCost);
}
else
{
for(int adj : dirAdjList[node])
{
dfsCheapestPath(adj, dirAdjList, cost, bobToZero, level+1, amount, bobSeen, maxCost);
}
}

if (level < bobToZero.size())
{
bobSeen.erase(bobToZero[level]);
}
cost -= curCost;
}

int mostProfitablePath(vector<vector<int>>& edges, int bob, vector<int>& amount) {
int n = edges.size() + 1;

// Create adjacency list for nodes in tree
vector<vector<int>> adjList = vector<vector<int>>(n, vector<int>{});
for(int ii=0; ii<edges.size(); ++ii)
{
int u = edges[ii][0];
int v = edges[ii][1];
adjList[u].push_back(v);
adjList[v].push_back(u);
}

// Create adjacency list with edges from root to leaves only.
vector<bool> marked(n, false);
vector<vector<int>> dirAdjList(n, vector<int>{});
dfsCreateDirAdjList(0, marked, adjList, dirAdjList);

// Create a vector of nodes from bob's leaf to zero.
vector<int> bobToZero{};
stack<int> toBob{};
bool foundBob = false;
dfsPathToBob(0, dirAdjList, toBob, bob, foundBob);
bobToZero.reserve(toBob.size());
while(!toBob.empty())
{
bobToZero.push_back(toBob.top());
toBob.pop();
}

// Alice walks all paths from root to leaves, while simulating Bob
// walking from Bob's leaf to root.
unordered_set<int> bobSeen;
int maxCost = std::numeric_limits<int>::min();
int cost = 0;

dfsCheapestPath(0, dirAdjList, cost, bobToZero, 0, amount, bobSeen, maxCost);

return maxCost;
}
};

Bhupathi Kakarlapudi

unread,
Feb 14, 2026, 2:22:28 PMFeb 14
to leetcod...@googlegroups.com
class Solution:
def countPairs(self, nums: List[int], k: int) -> int:
pair:int = 0

if len(nums) == len(set(nums)):
return 0

current:list = [i for i in range(len(nums))]

for c in current:
for n in current[c+1:]:
if ((c * n) % k == 0):
if nums[c] == nums[n]:
pair += 1

return pair

--
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/f2bede8f-57f2-4314-8a3a-3f663da58448n%40googlegroups.com.

Anuj Patnaik

unread,
Feb 14, 2026, 2:23:12 PMFeb 14
to leetcode-meetup
class Solution:
def mostProfitablePath(self, edges: List[List[int]], bob: int, amount: List[int]) -> int:
graph = defaultdict(list)
for i in range(len(edges)):
graph[edges[i][0]].append(edges[i][1])
graph[edges[i][1]].append(edges[i][0])
bob_time_to_visit_each_node = [-1] * len(amount)
def bob_dfs(curr, time, visited):
if curr == 0:
bob_time_to_visit_each_node[curr] = time
return True
visited.add(curr)
for i in graph[curr]:
if i in visited:
continue
if bob_dfs(i, time + 1, visited):
bob_time_to_visit_each_node[curr] = time
return True
visited.remove(curr)
return False
bob_dfs(bob, 0, set())
def alice_dfs(curr, time, income, visited):
visited.add(curr)
if bob_time_to_visit_each_node[curr] == -1 or time < bob_time_to_visit_each_node[curr]:
income += amount[curr]
elif time == bob_time_to_visit_each_node[curr]:
income += amount[curr] // 2
is_leaf = True
max_profit = float('-inf')
for j in graph[curr]:
if j in visited:
continue
is_leaf = False
profit = alice_dfs(j, time + 1, income, visited)
max_profit = max(max_profit, profit)
visited.remove(curr)
if is_leaf:
return income
return max_profit

return alice_dfs(0, 0, 0, set())

Rag Mur

unread,
Feb 14, 2026, 2:31:18 PMFeb 14
to leetcode-meetup
2467. Most Profitable Path in a Tree Medium 67.3%


from collections import defaultdict
class Solution:
def mostProfitablePath(self, edges: List[List[int]], bob: int, amount: List[int]) -> int:
INF = float('inf')
path_len = [INF]*len(amount)

graph = defaultdict(list)
for i, j in edges:
graph[i].append(j)
graph[j].append(i)

def calculate_path(prev_node, node, distance):
if node == 0:
path_len[node] = distance
return True
for neighbor in graph[node]:
if neighbor != prev_node:
if calculate_path(node, neighbor, distance+1):
path_len[node] = distance
return True
return False

calculate_path(-1, bob, 0)
print(path_len)

self.max_profit = -float('inf')

def find_alice_max(curr, prev, distance, current_income):
# Calculate Alice's share at this node
if distance < path_len[curr]:
current_income += amount[curr]
elif distance == path_len[curr]:
current_income += amount[curr] // 2
# Check if it's a leaf node (not root)
if len(graph[curr]) == 1 and curr != 0:
self.max_profit = max(self.max_profit, current_income)
return

for neighbor in graph[curr]:
if neighbor != prev:
find_alice_max(neighbor, curr, distance + 1, current_income)

find_alice_max(0, -1, 0, 0)
return self.max_profit






First Approach I tried memory limit exceeded: Following that I improvised

from collections import defaultdict
class Solution:
def mostProfitablePath(self, edges: List[List[int]], bob: int, amount: List[int]) -> int:
graph = defaultdict(list)
for i, j in edges:
graph[i].append(j)
graph[j].append(i)

# dfs 1, adjust the cost of doors
# nodes = bfs(0)
# [0, 1, 2, 3, 4] = 5//2
# [0, 1, 2, 3] = 4 = 4//2
seen = [0]*(len(edges)+1)
def dfs_path_to_bob(node, path):
if node == bob:
bob_opened_path = path[len(path)//2:]
shared_amount = 0
if len(path) % 2 == 1:
shared_amount = amount[bob_opened_path[0]] // 2
for node in bob_opened_path:
amount[node] = 0
amount[bob_opened_path[0]] = shared_amount
return
# print(graph)
for neighbor in graph[node]:
# print(node, neighbor, seen)
if seen[neighbor] == 0:
seen[neighbor] = 1
dfs_path_to_bob(neighbor, path + [neighbor])
seen[neighbor] = 0

# dfs 1
seen[0] = 1
dfs_path_to_bob(0, [0])
seen[0] = 0
def dfs_heighest_amount(node):
seen[node] = 1
max_amount = amount[node]
unvisited_neighbors = [neighbor for neighbor in graph[node] if seen[neighbor]==0]
if unvisited_neighbors:
max_amount += max(dfs_heighest_amount(unvisited_neighbor) for unvisited_neighbor in unvisited_neighbors)
seen[node] = 0
return max_amount

# dfs 2
return dfs_heighest_amount(0)

On Saturday, February 14, 2026 at 9:43:34 AM UTC-8 daryl...@gmail.com wrote:

Allen S.

unread,
Feb 14, 2026, 6:00:22 PMFeb 14
to leetcode-meetup
func mostProfitablePath(edges [][]int, bob int, amount []int) int {
g := make(map[int][]int) // adj table
for _, e := range edges {
a, b := e[0], e[1]
g[a] = append(g[a], b)
g[b] = append(g[b], a)
}

bobTime := make(map[int]int) // node to time
seen := make(map[int]bool)
const startTime = 0
const alice = 0
res := math.MinInt
bobDFS(g, bobTime, seen, bob, startTime)
// fmt.Printf("Bob's Path with time %v:\n", bobTime)
seen = make(map[int]bool)
aliceDFS(g, amount, bobTime, seen, alice, startTime, 0, &res)
return res
}

func bobDFS(
g map[int][]int,
bobTime map[int]int,
seen map[int]bool,
node, time int,
) bool {
res := node == 0
seen[node] = true
for _, nei := range g[node] {
if !seen[nei] && bobDFS(g, bobTime, seen, nei, time+1) {
res = true
}
}
if res {
bobTime[node] = time
}
return res
}

func aliceDFS(
g map[int][]int,
amount []int,
bobTime map[int]int,
seen map[int]bool,
node, time, curProfit int,
maxProfit *int,
) {
seen[node] = true
if bTime, ok := bobTime[node]; !ok || bTime > time {
curProfit += amount[node]
}

if bobTime[node] == time {
curProfit += amount[node] / 2
}
// fmt.Printf("Alice at Node %d, curProfit %d\n", node, curProfit)

if node != 0 && len(g[node]) == 1 { // leaf
*maxProfit = max(*maxProfit, curProfit)
}
for _, nei := range g[node] {
if !seen[nei] {
aliceDFS(g, amount, bobTime, seen, nei, time+1, curProfit, maxProfit)
}
}
}

/*
- solve it without Bob - O(n)
1) Bob DFS
- Bob DFS and build -> bobTime node and second
- bob's find and mark path to zero
2) Alice DFS
- checkBob()been, is

option1: phases
option2: sync
*/

On Saturday, February 14, 2026 at 9:43:34 AM UTC-8 daryl...@gmail.com wrote:
Reply all
Reply to author
Forward
0 new messages