2025 June 28 Problems

48 views
Skip to first unread message

daryl...@gmail.com

unread,
Jun 28, 2025, 12:44:26 PMJun 28
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.




Please download and import the following iCalendar (.ics) files to your calendar system.

Allen S.

unread,
Jun 28, 2025, 1:12:18 PMJun 28
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
}

left := inorderTraversal(root.Left)
right := inorderTraversal(root.Right)
return append(append(left, root.Val), right...)
}

Anuj Patnaik

unread,
Jun 28, 2025, 1:12:56 PMJun 28
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]:
answr = []
if not root:
return []
return self.inorderTraversal(root.left) + [root.val] + self.inorderTraversal(root.right)



Anuj Patnaik

unread,
Jun 28, 2025, 1:29:20 PMJun 28
to leetcode-meetup
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def doubleIt(self, head: Optional[ListNode]) -> Optional[ListNode]:
if(head.val == 0 and head.next == None):
return ListNode(0)
p1 = head
firstnum = []
while(p1 != None):
firstnum.append(p1.val)
p1 = p1.next
first_number = reduce(lambda x, y: x * 10 + y, firstnum)
second_number = 2 * first_number
double_number_digits = []
while second_number > 0:
double_number_digits.append(second_number % 10)
second_number = second_number // 10
double_number_digits = double_number_digits[::-1]
head_second_num = ListNode(double_number_digits[0])
p2 = head_second_num
k = 1
while k < len(double_number_digits):
p2.next = ListNode(double_number_digits[k])
k = k + 1
p2 = p2.next
return head_second_num





Allen S.

unread,
Jun 28, 2025, 1:36:35 PMJun 28
to leetcode-meetup
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func doubleIt(head *ListNode) *ListNode {
// prepend node with a zero
if head != nil && head.Val > 4 {
head = &ListNode {0, head}
}

for runner := head; runner != nil; runner = runner.Next {
runner.Val = runner.Val * 2 % 10

if runner.Next != nil && runner.Next.Val > 4 {
runner.Val++
}
}
return head
}

On Saturday, June 28, 2025 at 9:44:26 AM UTC-7 daryl...@gmail.com wrote:

daryl...@gmail.com

unread,
Jun 28, 2025, 1:44:59 PMJun 28
to leetcode-meetup
3412. Find Mirror Score of a String Medium 34.0%

The basic idea is to keep a stack for each letter tracking unmarked instances. When we iterate to a new letter we look at the stack for its mirror counterpart.
If the mirror stack is empty then our current letter is unmatched and unmarked and we append the current index to the corresponding stack.
If the mirror stack isn't empty then we pop off the top entry and use that entry for j in the (i -j).

O(s) time
O(s) memory

class Solution:
def calculateScore(self, s: str) -> int:
def mirror(char):
ind = ord(char) - ord('a')
return chr((25 - ind) + ord('a'))
unmarked = defaultdict(list)
score = 0
for i, char in enumerate(s):
target = mirror(char)
mirrorIndices = unmarked[target]
if mirrorIndices:
j = mirrorIndices.pop()
score += (i - j)
else:
unmarked[char].append(i)
return score
On Saturday, June 28, 2025 at 9:44:26 AM UTC-7 daryl...@gmail.com wrote:

Sourabh Majumdar

unread,
Jun 28, 2025, 1:47:19 PMJun 28
to leetcode-meetup

Doubling a number represented as a linked list.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
int Double(ListNode*& node) {
if (node == nullptr) {
return 0;
}

int carry_over = Double(node->next);
int node_value = 2*node->val;

if (carry_over > 0) {
node_value += carry_over;
}

int digit = node_value % 10;
carry_over = node_value / 10;
node->val = digit;

return carry_over;
}
ListNode* doubleIt(ListNode* head) {
int carry_over = Double(head);
if (carry_over > 0) {
ListNode* new_head = new ListNode;
new_head->val = carry_over;
new_head->next = head;
return new_head;
}


return head;
}
};

Sourabh Majumdar

unread,
Jun 28, 2025, 1:47:57 PMJun 28
to leetcode-meetup
In Order Traversal.
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
void InOrderTraversal(TreeNode* root, std::vector<int>& in_order) {
if (root == nullptr) {
return;
}

InOrderTraversal(root->left, in_order);
in_order.push_back(root->val);
InOrderTraversal(root->right, in_order);
return;
}
vector<int> inorderTraversal(TreeNode* root) {
std::vector<int> in_order;
InOrderTraversal(root, in_order);
return in_order;
}
};

FloM

unread,
Jun 28, 2025, 1:58:33 PMJun 28
to leetcode-meetup
Iterative approach. Time: O(n), Memory: O(n)
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> ans{};

stack<TreeNode*> s{};

TreeNode* node = root;

while( node != nullptr || !s.empty() )
{
if (node != nullptr)
{
s.push(node);
node = node->left;
}
else
{
TreeNode* top = s.top();
s.pop();

ans.push_back(top->val);
node = top->right;
}
}
return ans;

}
};

FloM

unread,
Jun 28, 2025, 2:02:18 PMJun 28
to leetcode-meetup
Time: O(n), Memory: O(1)
class Solution {
public:
ListNode* doubleIt(ListNode* head) {
/* Reverse the list. */
// fHead is short for flip head. Flip head
// is the current tail node.
ListNode* fHead = head;
ListNode* cur = fHead->next;

fHead->next = nullptr;

while(cur != nullptr)
{
ListNode* next = cur->next;
cur->next = fHead;
fHead = cur;
cur = next;
}

/* Starting from the ones column (that's fHead)
multiply each node's value by 2 and move the
tens carry number to the next node. */
cur = fHead;
uint32_t carryTens = 0;

while (cur != nullptr)
{
uint32_t curVal = cur->val;
uint32_t newVal = (2 * curVal) + carryTens;

carryTens = newVal / 10;

cur->val = newVal % 10;
cur = cur->next;
}

/* Reverse the list again. */
cur = fHead->next;
fHead->next = nullptr;

while(cur != nullptr)
{
ListNode* next = cur->next;
cur->next = fHead;
fHead = cur;
cur = next;
}

/* Add the last carry tens to the front of the list.*/
if (carryTens != 0)
{
ListNode* top = new ListNode(carryTens);
top->next = fHead;
fHead = top;
}

return fHead;

}
};

Allen S.

unread,
Jun 28, 2025, 2:02:43 PMJun 28
to leetcode-meetup
func calculateScore(s string) int64 {
indices := make([][]int, 26)
var totalScore int64
for i, c := range s {
mirror := 'z'- 'a' - (c - 'a')
if l := len(indices[mirror]); l > 0 {
closestUnmarkedIndex := indices[mirror][l-1]
indices[mirror] = indices[mirror][:l-1] // pop
score := i - closestUnmarkedIndex
totalScore += int64(score)
} else {
indices[c - 'a'] = append(indices[c - 'a'], i)
}
}
return totalScore
}
On Saturday, June 28, 2025 at 9:44:26 AM UTC-7 daryl...@gmail.com wrote:

FloM

unread,
Jun 28, 2025, 2:04:13 PMJun 28
to leetcode-meetup
3412. Find Mirror Score of a String Medium 34.0%
Time: O(n), Memory: O(n), where n is s.size().
class Solution {
public:
long long calculateScore(string s) {
vector<stack<int>> indexesPerLetter(26, stack<int>{});

long long score = 0;

for(uint32_t ii=0; ii<s.size(); ++ii)
{
char letter = s[ii];
uint32_t letterVal = letter - 'a';
uint32_t mirrorVal = 25 - letterVal;

if (!indexesPerLetter[mirrorVal].empty() &&
indexesPerLetter[mirrorVal].top() < ii)
{
score += (ii - indexesPerLetter[mirrorVal].top());
indexesPerLetter[mirrorVal].pop();
}
else
{
indexesPerLetter[s[ii]-'a'].push(ii);
}

}

return score;

}
};

Sourabh Majumdar

unread,
Jun 28, 2025, 2:08:27 PMJun 28
to leetcode-meetup
Find Mirror Score of a string.

class Solution {
public:
char Mirror(char character) {
int character_index = character - 'a';
int mirror_index = (
25 - character_index
);
char mirror_character = (
'a' + mirror_index
);

return mirror_character;
}
long long calculateScore(string s) {
std::unordered_map<char, std::stack<int>> unmarked_characters;
long long running_score = 0;
for(int index = 0; index < s.length(); index++) {
char character = s[index];
char mirror_character = Mirror(character);
if (unmarked_characters.contains(mirror_character)) {
int mirror_index = unmarked_characters[mirror_character].top();
unmarked_characters[mirror_character].pop();

running_score += (long long)(index - mirror_index);

if (unmarked_characters[mirror_character].empty()) {
unmarked_characters.erase(mirror_character);
}

continue;
}

// otherwise place it in the unmarked characters
unmarked_characters[character].push(index);
}

return running_score;
}
};

daryl...@gmail.com

unread,
Jun 28, 2025, 2:16:54 PMJun 28
to leetcode-meetup
Used a stack as an easy way to keep track of nodes for walking backwards. Once the entire list is in the stack we can simply pop nodes off the top the stack and double each nodes value, accounting for carry. If we still have a carry when we finish the stack then can create a new List node with value 1 and stick it to the old head.

O(n) time
O(n) space 

class Solution:
def doubleIt(self, head: Optional[ListNode]) -> Optional[ListNode]:
nodes = []
node = head
while node:
nodes.append(node)
node = node.next
carry = 0
while nodes:
node = nodes.pop()
newCarry = (2 * node.val) // 10
node.val = (2 * node.val) % 10 + carry
carry = newCarry
if carry:
newHead = ListNode(1, head)
head = newHead
return head

On Saturday, June 28, 2025 at 9:44:26 AM UTC-7 daryl...@gmail.com wrote:

Carlos Green

unread,
Jun 28, 2025, 2:17:44 PMJun 28
to leetcode-meetup
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number[]}
*/
var inorderTraversal = function(root) {
const output = []
const inorder = node => {
if (!node) return
inorder(node.left)
output.push(node.val)
inorder(node.right)
}

inorder(root)
return output
};
Reply all
Reply to author
Forward
0 new messages