2025 August 23 Problems

90 views
Skip to first unread message

daryl...@gmail.com

unread,
Aug 23, 2025, 12:39:38 PMAug 23
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.

506. Relative Ranks Easy 73.7%



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

Anuj Patnaik

unread,
Aug 23, 2025, 2:03:18 PMAug 23
to leetcode-meetup
import heapq
class Solution:
def kthSmallest(self, matrix: List[List[int]], k: int) -> int:
heap = []
for r in range(len(matrix)):
for c in range(len(matrix[0])):
if len(heap) < k:
heapq.heappush(heap, -matrix[r][c])
elif -matrix[r][c] > heap[0]:
heapq.heappushpop(heap, -matrix[r][c])
return -heap[0]




FloM

unread,
Aug 23, 2025, 2:18:11 PMAug 23
to leetcode-meetup
506. Relative Ranks Easy 73.7%
Time: O(n*log(n) ExtraSpace: O(n)
class Solution {
public:
vector<string> findRelativeRanks(vector<int>& score) {

// make vector of pairs of {score, original index};
vector<pair<int, int>> scoreAndIndexes(score.size(), {0, 0});

for(int ii=0; ii<score.size(); ++ii)
{
scoreAndIndexes[ii] = {score[ii], ii};
}

sort(scoreAndIndexes.begin(),
scoreAndIndexes.end(),
[](const pair<int, int>& a, const pair<int, int>& b){return b.first < a.first;});

// make vector of strings for ranking titles
vector<string> numStrings(score.size(), "");
if (numStrings.size() >= 1)
{
numStrings[0] = "Gold Medal";
}
if (numStrings.size() >= 2)
{
numStrings[1] = "Silver Medal";
}
if (numStrings.size() >= 3)
{
numStrings[2] = "Bronze Medal";
}

for(int ii=3; ii<numStrings.size(); ++ii)
{
numStrings[ii] = to_string(ii+1);
}

// make final answer vector
vector<string> ans(score.size(), "");
for(int ii=0; ii<scoreAndIndexes.size(); ++ii)
{
ans[scoreAndIndexes[ii].second] = numStrings[ii];
}

return ans;
}
};

FloM

unread,
Aug 23, 2025, 2:22:24 PMAug 23
to leetcode-meetup
Time: O(n*n*log(k)) Extra Space: O(k)
class Solution {
public:
int kthSmallest(vector<vector<int>>& matrix, int k) {
// priority queue returns the smallest value first. top() = smallest
priority_queue<int, vector<int>, decltype([](const int& a, const int& b){return b < a;})> pq{};

int n = matrix.size();

// The kthLargest values will always be kept in the priority queue.
// Values smaller than kthLargest will be popped out.
int kthLargest = (n * n) - k + 1;
for(int rr=0; rr<n; ++rr)
{
for(int cc=0; cc<n; ++cc)
{
pq.push(matrix[rr][cc]);
if (pq.size() > kthLargest)
{
pq.pop();
}
}
}

// Top of the priority queue is the kth largest.
return pq.top();

}
};

Anuj Patnaik

unread,
Aug 23, 2025, 2:30:24 PMAug 23
to leetcode-meetup
#Heap Method
import heapq
class Solution:
def findRelativeRanks(self, score: List[int]) -> List[str]:
heap = []
i = 0
while i < len(score):
heapq.heappush(heap, [-score[i], i])
i += 1
result = [""] * len(score)
rank = 1
while len(heap) > 0:
score_index = heapq.heappop(heap)
j = score_index[1]
if rank == 1:
result[j] = "Gold Medal"
elif rank == 2:
result[j] = "Silver Medal"
elif rank == 3:
result[j] = "Bronze Medal"
else:
result[j] = str(rank)
rank += 1
return result
#Sorting Method
class Solution: def findRelativeRanks(self, score: List[int]) -> List[str]: copy_of_scores = score.copy() rank_of_scores = dict() copy_of_scores.sort() rank = 1 results = [] i = len(copy_of_scores) - 1 while i >= 0: rank_of_scores[copy_of_scores[i]] = rank rank += 1 i -= 1 for j in score: if rank_of_scores[j] == 3: results.append("Bronze Medal") elif rank_of_scores[j] == 2: results.append("Silver Medal") elif rank_of_scores[j] == 1: results.append("Gold Medal") else: results.append(str(rank_of_scores[j])) return results








Sourabh Majumdar

unread,
Aug 23, 2025, 2:37:34 PMAug 23
to leetcode-meetup
Relative Ranks.

class Solution {
public:
struct Athelete{
int index;
int score;
};
vector<string> findRelativeRanks(vector<int>& score) {

auto athelete_comparator = [](Athelete& athelete1, Athelete& athelete2) {
return athelete1.score < athelete2.score;
};
std::priority_queue<Athelete, std::vector<Athelete>, decltype(athelete_comparator)> atheletes(athelete_comparator);
for(int index = 0; index < score.size(); index++) {
atheletes.push(
Athelete{
.index = index,
.score = score[index]
}
);
}

std::queue<std::string> awards;
{
awards.push("Gold Medal");
awards.push("Silver Medal");
awards.push("Bronze Medal");
}
std::vector<std::string> answer(score.size(), "");

int rank = 0;
while(!atheletes.empty()) {
rank += 1;
Athelete athelete = atheletes.top();
atheletes.pop();

if (!awards.empty()) {
std::string award = awards.front();
awards.pop();

answer[athelete.index] = award;
continue;
}

// otherwise assign the rank
answer[athelete.index] = std::to_string(rank);
}

return answer;
}

};

Nico Wong

unread,
Aug 23, 2025, 3:36:54 PMAug 23
to leetcode-meetup
class Solution:
    def findRelativeRanks(self, score: List[int]) -> List[str]:
        # sort score array
        # new index + 1 of sorted array is the place number (decreasing order)
        # might use a map if the scores were not unique

        sortedScores = sorted(score, reverse=True)
        valToIndex = {sortedScores[index]: index for index in range(len(sortedScores))}
        ret = []
        for sc in score:
            place = valToIndex[sc] + 1
            str_ = ""
            if place == 1:
                str_ = "Gold Medal"
            elif place == 2:
                str_ = "Silver Medal"
            elif place == 3:
                str_ = "Bronze Medal"
            else:
                str_ = str(place)
            ret.append(str_)

        return ret

        # ~17 minutes

On Saturday, August 23, 2025 at 9:39:38 AM UTC-7 daryl...@gmail.com wrote:

Nico Wong

unread,
Aug 23, 2025, 3:40:08 PMAug 23
to leetcode-meetup
Time: 96%, Space: 58.2%

vinay

unread,
Aug 25, 2025, 1:28:08 AMAug 25
to leetcod...@googlegroups.com
506. Relative Ranks Easy 73.7%
time: O(nlog(n) )
space: O(n)

class Solution {
    public String[] findRelativeRanks(int[] score) {
        PriorityQueue<int[]> pq = new PriorityQueue<>((a,b) -> Integer.compare(b[0], a[0]));
        String[] result = new String[score.length];
        int pos =0;

        for(int i = 0; i < score.length; i++){
            pq.offer(new int[]{score[i], i});
        }

        while(pq.size() > 0){
            int[] cur = pq.poll();
            int rank = cur[0];
            int index = cur[1];
            result[index] = getRank(pos++);
        }

        return result;
    }

    public String getRank(int index){
        switch (index) {
            case 0 -> {return "Gold Medal";}
            case 1 -> {return "Silver Medal";}
            case 2 -> {return "Bronze Medal";}
            default -> {return String.valueOf(index+1);}
        }
    }
}

--
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/3ebb3870-387e-43c9-9466-130b4306f5c7n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages