2024 March 16 Problems

49 views
Skip to first unread message

daryl...@gmail.com

unread,
Mar 16, 2024, 12:40:48 PMMar 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.



Please download and import the following iCalendar (.ics) files to your calendar system.
Weekly: https://us04web.zoom.us/meeting/upIqc-6upj8sGt0O2fJXTw2gC4FxRMQ-iqAT/ics?icsToken=98tyKu6uqT8tHNyRthmOR7YAB4-gKO7xiCldjbcNs03jKRhndVHxFbZkKoBSIZXZ

Join Zoom Meeting
https://us04web.zoom.us/j/76747684609?pwd=HOCapO7jjK0rifPlKvV9CxWFn5lLJn.1

Meeting ID: 767 4768 4609
Passcode: 8wf3Qa

Flocela Maldonado

unread,
Mar 16, 2024, 1:19:17 PMMar 16
to leetcode-meetup
Time: O(n*lg(n)) Memory: O(1)
class Solution {
public:
int countPairs(vector<int>& nums, int target) {
sort(nums.begin(), nums.end());

int lt = 0;
int rt = nums.size()-1;

int sum = 0;
while(lt < rt)
{
while(lt < rt && nums[lt] + nums[rt] >= target)
{
--rt;
}
sum += rt - lt;
++lt;
}

return sum;
}
};

vinay

unread,
Mar 16, 2024, 1:52:21 PMMar 16
to leetcod...@googlegroups.com
Getting a memory limit exceeded owing to constraints, but here is one possible approach I could come up with:
time complexity o(nlogn) / dp could be NCp
space o(n*p)
class Solution {
    int[][]  memo = null;

    public int minimizeMax(int[] nums, int p) {
        Arrays.sort(nums);
        memo = new int[nums.length+1][p+1];
        for(int[] row : memo){
            Arrays.fill(row, -1);
        }
        return dp(0,p, nums);
    }

    public int dp(int i, int p, int[] nums){

        if(p == 0){
            return 0;
        }

        if(i >= nums.length-1){
            return (int)1e10;
        }

        if(memo[i][p] != -1){
            return memo[i][p];
        }

        int with = Math.max(Math.abs(nums[i]-nums[i+1]), dp(i+2, p-1,nums));
        int without =  dp(i+1, p, nums);

        return memo[i][p] = Math.min(with, without);
    }
}



--
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 on the web visit https://groups.google.com/d/msgid/leetcode-meetup/7fa1ad8f-87c9-49aa-8e65-48abb5bc69e3n%40googlegroups.com.

Priyank Gupta

unread,
Mar 16, 2024, 4:21:20 PMMar 16
to leetcode-meetup
# time : O(nlog(n))
# space: O(1)
class Solution:
def countPairs(self, nums: List[int], target: int) -> int:
nums = sorted(nums)
start = 0
end = len(nums)-1
res = 0
while(start < end):
if nums[start] + nums[end] >= target:
end -= 1
else:
res += (end-start)
start += 1
return res
Reply all
Reply to author
Forward
0 new messages