2025 May 31 Problems

80 views
Skip to first unread message

daryl...@gmail.com

unread,
May 31, 2025, 1:55:02 AMMay 31
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.

I will be busy this week, so Flo and Allen will be helping to lead the meeting. Defer to them on any instructions for how this week's meeting will be run.




2334. Subarray with Elements Greater Than Varying Threshold Hard 44.4% (Rolled over from previous weeks)

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

Anuj Patnaik

unread,
May 31, 2025, 1:00:12 PMMay 31
to leetcode-meetup
class Solution:
def minimumOperations(self, grid: List[List[int]]) -> int:
numoperations = 0
for i in range(len(grid) - 1):
for j in range(len(grid[0])):
if(grid[i][j] >= grid[i+1][j]):
newval = grid[i][j] + 1
numoperations += grid[i][j] - grid[i+1][j] + 1
grid[i+1][j] = newval
return numoperations
#Time: O(m*n) where m is length of grid. and n is length of the first element of grid
#Space: O(1)

FloM

unread,
May 31, 2025, 1:07:29 PMMay 31
to leetcode-meetup
Time: O(rows * cols) Mem: O(1)

class Solution {
public:
int minimumOperations(vector<vector<int>>& grid) {

int numRows = grid.size();
int numCols = grid[0].size();

int numIncreases = 0;
for(int cc=0; cc<numCols; ++cc)
{
for(int rr=1; rr<numRows; ++rr)
{
if (grid[rr][cc] <= grid[rr-1][cc])
{
int increase = grid[rr-1][cc] - grid[rr][cc] + 1;
numIncreases += increase;
grid[rr][cc] += increase;
}
}
}

return numIncreases;
}
};

Tarini Pattanaik

unread,
May 31, 2025, 1:44:57 PMMay 31
to leetcod...@googlegroups.com
Time complexity O( m x n), where m = number of rows and n = number of columns Space complexity O(1) 3402. Minimum Operations to Make Columns Strictly Increasing class Solution {
public int minimumOperations(int[][] grid) {
int count = 0;
int rows = grid.length;
int cols = grid[0].length;
for (int col = 0; col < cols; col++) {
for (int row = 1; row < rows; row++) {
while (grid[row][col] <= grid[row - 1][col]) {
grid[row][col]++;
count++;
}
}
}
return count;
}
}

--
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/0b4a4388-add4-43b4-a7b2-e145ddbae43cn%40googlegroups.com.

Mitil Roy

unread,
May 31, 2025, 2:27:36 PMMay 31
to leetcod...@googlegroups.com
class Solution {

// TC: O(m*n), m = number of rows, n = number of columns
// SC: O(1) for storing the local variables and count
fun minimumOperations(grid: Array<IntArray>): Int {

val rows = grid.size
if (rows == 0) return 0
val cols = grid[0].size

var numOps = 0
for (c in 0 until cols) {
for (r in 1 until rows) {
val current = grid[r][c]
val above = grid[r-1][c]
if (current > above) continue
numOps += above - current + 1
grid[r][c] = grid[r-1][c] + 1
}
}
return numOps
}
}



--
Mitil

Sagar Patwardhan

unread,
May 31, 2025, 2:37:56 PMMay 31
to leetcod...@googlegroups.com

FloM

unread,
May 31, 2025, 2:38:06 PMMay 31
to leetcode-meetup
We were only successful with the Easy Problem. Anuj did a great presentation. 
We'd like to roll over the two mediums and hard for next week (Jun 7th)

-Flo

FloM

unread,
May 31, 2025, 5:51:17 PMMay 31
to leetcode-meetup
Time: O(n) Mem: O(n) Actually only beats 10% of users. But is O(n), not sure why its running so slow.
class Solution {
public:
int maxFreeTime(int eventTime, int k, vector<int>& startTime, vector<int>& endTime) {

int longestFreeTime = 0;

vector<vector<int>> meetings{};
// Put in fake first meeting that starts at time -1, ends at time 0.
meetings.push_back(vector<int>{-1, 0});
for(int ii=0; ii<startTime.size(); ++ii)
{
longestFreeTime = std::max(longestFreeTime, startTime[ii] - meetings[meetings.size()-1][1]);

meetings.push_back(vector<int>{startTime[ii], endTime[ii]});
}

// Put in fake last meeting that starts at time eventTime, and ends at time eventTime+1
meetings.push_back(vector<int>{eventTime, eventTime+1});
longestFreeTime = std::max(longestFreeTime, meetings[meetings.size()-1][0] - meetings[meetings.size()-2][1]);

// For the first k meetings, the meetingMinutesInRange is the sum of the number of minutes in those meetings.
int meetingMinutesInRange = 0;
int rangeCount = 0;
while(rangeCount < k)
{
meetingMinutesInRange += meetings[rangeCount][1] - meetings[rangeCount][0];
++rangeCount;
}

// range [lo, hi] is range[firstMeeting in range, lastMeeting in range].
int lo = 1;
int hi = lo + k - 1;

// For each iteration there is a range of meetings that can be moved. They are
// the meetings lo to hi in meetings vector.
// The total time that these meetings have is the end of the last range and
// the start of the next range.
// Or the total time is the end_time of meeting[lo-1] to the start_time of meeting[hi+1];
// The total free_time that can be found in the total_time is the total_time minus
// the sum_of_the_minutes_in_the_meeting_range.
// I can move k meetings (there are k meetings in the range). I choose to
// move all the meetings to one side, then the resulting free time is
// total_time minus sum_of_meeting_times.
while(hi < meetings.size()-1)
{
int lastMeetingEnded = meetings[lo-1][1];
int nextMeetingStarts = meetings[hi + 1][0];

meetingMinutesInRange = meetingMinutesInRange -
(meetings[lo-1][1] - meetings[lo-1][0]) +
(meetings[hi][1] - meetings[hi][0]);

int newLongestFreeTime = nextMeetingStarts -
lastMeetingEnded -
meetingMinutesInRange;


longestFreeTime = std::max(longestFreeTime, newLongestFreeTime);

++lo;
++hi;
}

return longestFreeTime;

}
};

Reply all
Reply to author
Forward
0 new messages