2025 August 9 Problems

67 views
Skip to first unread message

daryl...@gmail.com

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

Anuj Patnaik

unread,
Aug 9, 2025, 1:14:22 PMAug 9
to leetcode-meetup
class Solution:
def minDeletion(self, s: str, k: int) -> int:
counters = {
"a": 0,
"b": 0,
"c": 0,
"d": 0,
"e": 0,
"f": 0,
"g": 0,
"h": 0,
"i": 0,
"j": 0,
"k": 0,
"l": 0,
"m": 0,
"n": 0,
"o": 0,
"p": 0,
"q": 0,
"r": 0,
"s": 0,
"t": 0,
"u": 0,
"v": 0,
"w": 0,
"x": 0,
"y": 0,
"z": 0
}

for i in s:
counters[i] += 1
freq = list(counters.values())
freq.sort()
rev = freq[::-1]
print(rev)
distinct_char = 0
j = 0
while distinct_char < k:
distinct_char += 1
j += 1
return sum(rev[j::])







Anuj Patnaik

unread,
Aug 9, 2025, 1:38:51 PMAug 9
to leetcode-meetup
class Solution:
def maxSumDistinctTriplet(self, x: List[int], y: List[int]) -> int:
possible_indices = dict()
for i in range(len(x)):
if x[i] in possible_indices:
if y[i] > y[possible_indices[x[i]]]:
possible_indices[x[i]] = i
else:
possible_indices[x[i]] = i
max_y = []
for j in possible_indices.values():
max_y.append(y[j])
if len(max_y) < 3:
return -1
else:
max_y.sort()
rev_max_y = max_y[::-1]
return rev_max_y[0] + rev_max_y[1] + rev_max_y[2]








Gowrima Jayaramu

unread,
Aug 9, 2025, 2:27:07 PMAug 9
to leetcode-meetup
from collections import defaultdict

class Solution:
def minDeletion(self, s: str, k: int) -> int:
char_map = defaultdict(int)
for c in s:
char_map[c] += 1
if len(char_map) <= k:
return 0
sorted_items = sorted(char_map.items(), key=lambda item: item[1])
min_del = 0
to_remove = len(sorted_items) - k
for i in range(to_remove):
min_del += sorted_items[i][1]
return min_del

Sourabh Majumdar

unread,
Aug 9, 2025, 2:31:25 PMAug 9
to leetcode-meetup
3572: Max Y sum by picking triplet of distinct X values.
class Solution {
public:
struct Element{
int x_value;
int max_y;
};

struct ElementComparator{
bool operator()(Element& element1, Element& element2) {
if (element1.max_y == element2.max_y) {
return element1.x_value > element2.x_value;
}

return element1.max_y > element2.max_y;
}
};
int maxSumDistinctTriplet(vector<int>& x, vector<int>& y) {

std::unordered_map<int, int> x_to_max_y;

int n = x.size();
for(int index_i = 0; index_i < n; index_i++) {
if (!x_to_max_y.contains(x[index_i])) {
x_to_max_y[x[index_i]] = y[index_i];
continue;
}

// else update
x_to_max_y[x[index_i]] = std::max(
x_to_max_y[x[index_i]],
y[index_i]
);
}

// now place everything in a priority queue
if (x_to_max_y.size() < 3) {
return -1;
}

// othewise place everything in a vector and sort
std::vector<Element> list_of_elements;
for(auto [x_value, max_y] : x_to_max_y) {
list_of_elements.push_back(
Element{
.x_value = x_value,
.max_y = max_y
}
);
}

std::sort(
list_of_elements.begin(),
list_of_elements.end(),
ElementComparator()
);

return (
list_of_elements[0].max_y +
list_of_elements[1].max_y +
list_of_elements[2].max_y
);
}
};

Sourabh Majumdar

unread,
Aug 9, 2025, 2:33:47 PMAug 9
to leetcode-meetup
Minimum Deletions for At most K distinct characters
class Solution {
public:
struct Element{
char character;
int frequency;
};
struct Comparator{
bool operator()(Element& element1, Element& element2) {
if (element1.frequency == element2.frequency) {
return element1.character < element2.character;
}

return element1.frequency > element2.frequency;
}
};
int minDeletion(string s, int k) {
std::unordered_map<char, int> character_frequency;
for(auto character : s) {
character_frequency[character] += 1;
}

std::priority_queue<Element, std::vector<Element>, Comparator> characters_heap;

for(auto [character, frequency] : character_frequency) {

characters_heap.push(
Element{
.character = character,
.frequency = frequency
}
);
}

int number_ops = 0;
while(characters_heap.size() > k) {
Element top_element = characters_heap.top();
characters_heap.pop();
number_ops+= top_element.frequency;
}

return number_ops;
}
};

FloM

unread,
Aug 9, 2025, 2:48:10 PMAug 9
to leetcode-meetup
Time: O(n) number of x values, Memory: O(n)
class Solution {
public:

int maxSumDistinctTriplet(vector<int>& x, vector<int>& y) {

uint32_t numSize = x.size();

unordered_map<uint32_t, unordered_set<uint32_t>> xValsPerY{};
for(uint32_t ii=0; ii<numSize; ++ii)
{
if (!xValsPerY.contains(y[ii]))
{
xValsPerY.insert({y[ii], unordered_set<uint32_t>{}});
}

xValsPerY[y[ii]].insert(x[ii]);
}

vector<uint32_t> sortedY{};
for(auto& p : xValsPerY)
{
sortedY.push_back(p.first);
}

sort(sortedY.begin(), sortedY.end(), std::greater<>());

unordered_set<uint32_t> chosenXValues = xValsPerY[sortedY[0]];
uint32_t sum = (chosenXValues.size() * sortedY[0]);

if (chosenXValues.size() >= 3)
{
return 3 * sortedY[0];
}

for(int ii=1; ii<sortedY.size(); ++ii)
{
unordered_set<uint32_t>& xvaluesInYii = xValsPerY[sortedY[ii]];
for(int xVal : xvaluesInYii)
{
if (!chosenXValues.contains(xVal))
{
sum += sortedY[ii];
chosenXValues.insert(xVal);
if (chosenXValues.size() == 3)
{
return sum;
}
}
}
}

return -1;

}
};
Message has been deleted

FloM

unread,
Aug 9, 2025, 2:53:42 PMAug 9
to leetcode-meetup
class Solution {
public:
int minDeletion(string s, int k) {
unordered_map<char, int> countPerLetter{};
for(char c : s)
{
++countPerLetter[c];
}

vector<int>counts{};
counts.reserve(countPerLetter.size());
for(pair<char, int> lc : countPerLetter)
{
counts.push_back(lc.second);
}

sort(counts.begin(), counts.end());

int toDelete = 0;
int countsSize = counts.size();
for(int ii=0; ii<countsSize-k; ++ii)
{
toDelete += counts[ii];
}

return toDelete;
}
};
Reply all
Reply to author
Forward
0 new messages