2025 August 16 Problems

110 views
Skip to first unread message

daryl...@gmail.com

unread,
Aug 16, 2025, 12:45:09 PMAug 16
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.

This week's questions are some pretty basic data structures and last week's hard problem.

15. 3Sum Medium 37.5%

146. LRU Cache Medium 45.7%


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

Anuj Patnaik

unread,
Aug 16, 2025, 1:40:21 PMAug 16
to leetcode-meetup
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
nums.sort()
three_sum_result = []
seen = set()
for i in range(len(nums) - 2):
left = i + 1
right = len(nums) - 1
while left < right:
if nums[i] + nums[left]+ nums[right] == 0:
if tuple([nums[i], nums[left], nums[right]]) not in seen:
three_sum_result.append([nums[i], nums[left], nums[right]])
seen.add(tuple([nums[i], nums[left], nums[right]]))
left += 1
right -= 1
else:
if nums[i] + nums[left]+ nums[right] < 0:
left += 1
else:
right -= 1
return three_sum_result





Gowrima Jayaramu

unread,
Aug 16, 2025, 2:04:21 PMAug 16
to leetcode-meetup
from collections import OrderedDict

class LRUCache:

def __init__(self, capacity: int):
self.capacity = capacity
self.cache = OrderedDict()

def get(self, key: int) -> int:
result = -1

if key in self.cache:
result = self.cache[key]
self.cache.move_to_end(key)

return result

def put(self, key: int, value: int) -> None:

# move to end - most recent
# start - least recent
if key in self.cache:
self.cache[key] = value
self.cache.move_to_end(key)
else:
if len(self.cache) >= self.capacity:
self.cache.popitem(last = False)
self.cache[key] = value




# Your LRUCache object will be instantiated and called as such:
# obj = LRUCache(capacity)
# param_1 = obj.get(key)
# obj.put(key,value)

FloM

unread,
Aug 16, 2025, 2:28:32 PMAug 16
to leetcode-meetup

15. 3Sum Medium
Time: O(n^2), Mem: O(n)
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {

vector<vector<int>> ans{};

unordered_map<int, int> countPerNum{};
unordered_set<int> keys{};

for(int x : nums)
{
++countPerNum[x];
keys.insert(x);
}

// If there are more than one zero, then say there is only one zero.
if (countPerNum.contains(0))
{
// If there are three zeros, then add triplet to answer.
if (countPerNum[0] > 2)
{
ans.push_back({0, 0, 0});
}
countPerNum[0] = 1;
}

// If there are duplicate numbers and there exists their corresponding triplet_number, then add triplet to answer.
for(auto const& [num, count] : countPerNum)
{
if ( (count > 1) && (countPerNum.contains(-(2*num))) )
{
ans.push_back({num, num, -(2*num)});
}
}

// The number a and number b will be different.
for(int a : keys)
{
countPerNum.erase(a);
unordered_set<int> seen{};
for(auto const& [b, count] : countPerNum)
{
seen.insert(b);

int diff = 0 - a - b;
// !seen.contains(diff) takes care of two cases.
// Do not count triplets that contain two b's. They have already been counted above.
// The triplet containing a + b has already been added. If diff equals b, do not add it again.
if (countPerNum.contains(diff) && !seen.contains(diff))
{
ans.push_back({a, b, diff});
}
}
}


return ans;

}
};

FloM

unread,
Aug 16, 2025, 2:34:44 PMAug 16
to leetcode-meetup
146. LRU Cache Medium 45.7%
class LRUCache {

struct Node
{
Node(int k, int v): key{k}, val{v}{};
Node(int k, Node* p, Node* n):key{k}, prev{p}, next{n}{}

int key = -1;
int val = -1;
Node* prev = nullptr;
Node* next = nullptr;
};

public:
LRUCache(int capacity) {
_capacity = capacity;
}
int get(int key) {
if(_nodePerKey.find(key) == _nodePerKey.end())
{
return -1;
}

Node* node = _nodePerKey[key];
int value = node->val;
if(node != _head)
{
removeNode(node);
addNodeToFront(new Node({key, value}));
}
return _nodePerKey[key]->val;
}
void put(int key, int value) {
if(_nodePerKey.find(key) != _nodePerKey.end())
{
removeNode(_nodePerKey[key]);
}
addNodeToFront(new Node(key, value));
if(_nodePerKey.size() > _capacity)
{
removeNode(_tail);
}
}

private:

void addNodeToFront(Node* node)
{
if(_head == nullptr)
{
_head = node;
_tail = node;
}
else
{
Node* oldHead = _head;
node->next = oldHead;
oldHead->prev = node;
_head = node;
}
_nodePerKey.insert({node->key, node});
}

void removeNode(Node* node)
{
Node* oldPrev = node->prev;
Node* oldNext = node->next;
if (oldPrev != nullptr)
{
oldPrev->next = oldNext;
}
if(oldNext != nullptr)
{
oldNext->prev = oldPrev;
}
if(node == _head)
{
_head = oldNext;
}
if(node == _tail)
{
_tail = oldPrev;
}
_nodePerKey.erase(node->key);
delete node;
}

Node* _head = nullptr;
Node* _tail = nullptr;
unordered_map<int, Node*> _nodePerKey{};
int _capacity = -1;
};

/**
* Your LRUCache object will be instantiated and called as such:
* LRUCache* obj = new LRUCache(capacity);
* int param_1 = obj->get(key);
* obj->put(key,value);
*/

FloM

unread,
Aug 16, 2025, 11:13:46 PMAug 16
to leetcode-meetup
I don't think I can do the hard problem. Been working on it for hours and it looks like a lot of accounting. I'm okay if you keep it for next week or pick a new problem.
Good luck to anyone who tries it! : )

Reply all
Reply to author
Forward
0 new messages