2025 May 24 Problems

65 views
Skip to first unread message

daryl...@gmail.com

unread,
May 24, 2025, 12:47:21 PMMay 24
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.

Hard problem has been carried over again.





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

Anuj Patnaik

unread,
May 24, 2025, 1:16:38 PMMay 24
to leetcode-meetup
#Time(O(n^2))
#Space:(O(1))
class Solution:
def countStudents(self, students: List[int], sandwiches: List[int]) -> int:
students_with_no_sandwich=0
while len(students) != 0 and students_with_no_sandwich < len(students):
if students[0] == sandwiches[0]:
students.pop(0)
sandwiches.pop(0)
students_with_no_sandwich = 0
else:
students.append(students.pop(0))
students_with_no_sandwich+=1
return len(students)

FloM

unread,
May 24, 2025, 1:20:35 PMMay 24
to leetcode-meetup
Time: O(n), Memory: O(1)
class Solution {
public:
int countStudents(vector<int>& students, vector<int>& sandwiches) {
int squareStudentCount = 0;
int circleStudentCount = 0;

for(int ii=0; ii<students.size(); ++ii)
{
if (students[ii] == 0)
{
++circleStudentCount;
}
else
{
++squareStudentCount;
}
}

int sandwichIdx = 0;
while(sandwichIdx < sandwiches.size())
{
if (sandwiches[sandwichIdx] == 0)
{
if (circleStudentCount > 0)
{
--circleStudentCount;
}
else
{
break;
}
}
else
{
if (squareStudentCount > 0)
{
--squareStudentCount;
}
else
{
break;
}
}

++sandwichIdx;
}

return sandwiches.size() - sandwichIdx;

}
};

Anuj Patnaik

unread,
May 24, 2025, 1:36:13 PMMay 24
to leetcode-meetup
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* swapNodes(ListNode* head, int k) {
struct ListNode *kth_elem_from_left = NULL;
struct ListNode *kth_elem_from_right = NULL;
struct ListNode *curr = head;
int count = 1;
while (count < k) {
curr = curr->next;
count++;
}
kth_elem_from_left = curr;
kth_elem_from_right = head;
curr = curr->next;
while (curr != NULL) {
curr = curr->next;
kth_elem_from_right = kth_elem_from_right->next;
}

int temp = kth_elem_from_left->val;
kth_elem_from_left->val = kth_elem_from_right->val;
kth_elem_from_right->val = temp;
return head;
}
};
//Time O(n)
//Space O(1)

Tarini Pattanaik

unread,
May 24, 2025, 1:52:36 PMMay 24
to leetcod...@googlegroups.com
Time: O(n)
Space O(1)
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode swapNodes(ListNode head, int k) {
ListNode fast = head;
for (int i = 1; i < k; i++) {
fast = fast.next;
}

ListNode tempFirst = fast;

ListNode slow = head;
while (fast != null && fast.next != null) {
fast = fast.next;
slow = slow.next;
}

int temp = tempFirst.val;
tempFirst.val = slow.val;
slow.val = temp;
return head;
}
}

--
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/8e77e0f3-a106-49bd-9864-82ae4f986e16n%40googlegroups.com.

Tarini Pattanaik

unread,
May 24, 2025, 2:14:35 PMMay 24
to leetcod...@googlegroups.com
Time: O(n)
Space: O(n)

class Solution {
public int countStudents(int[] students, int[] sandwiches) {
List<Integer> studentList = Arrays.stream(students)
.boxed()
.collect(Collectors.toList());

List<Integer> sandWichesList = Arrays.stream(sandwiches)
.boxed()
.collect(Collectors.toList());
int i = 0;
while (!studentList.isEmpty() && !sandWichesList.isEmpty()) {

if (studentList.get(0) == sandWichesList.get(0)) {
studentList.remove(0);
sandWichesList.remove(0);
i = 0;
} else {
int val = studentList.get(0);
studentList.remove(0);
studentList.add(val);
i++;
}

if (i == studentList.size()) {
break;
}
}

return studentList.size();
}
}

Allen S.

unread,
May 24, 2025, 2:20:55 PMMay 24
to leetcode-meetup
func swapNodes(head *ListNode, k int) *ListNode {
// get list lenth
// find target nodes
nodeCount := 0
var l, r *ListNode // targets
for runner := head; runner != nil; runner = runner.Next {
nodeCount++
// left target node
if nodeCount == k {
l = runner
}

// right target node
if nodeCount == k {
r = head
}

// move right target
if nodeCount > k {
r = r.Next
}
}

// swap
l.Val, r.Val = r.Val, l.Val
return head
}

FloM

unread,
May 24, 2025, 8:03:48 PMMay 24
to leetcode-meetup
1721. Swapping Nodes in a Linked List Medium 68.4%
Time: O(n) Mem:O(1)
Could not find a real intuitive way of doing this. Could not find a way, where it wouldn't matter if k > listLength/2.
class Solution {
public:

ListNode* swapNodes(ListNode* head, int k) {
// Left-K node is the node k spaces from the head.
// Right-K node is the node k spaces from the tail.

// Add a temporary head node before head. In case
// Left-K node is first node.
ListNode* tempHead = new ListNode(-100);
tempHead->next = head;
head = tempHead;

// Find size of list.
int listNodeCount = 0;
ListNode* cur = head->next;
ListNode* prev = head;
while(cur != nullptr)
{
++listNodeCount;
prev = cur;
cur = cur->next;
}

// Need the Left-K node to be to the left of the Right-K node.
// Say ACount is the number spaces to the Left-K node, and
// say BCount is the number of spaces to the Right-K node.
int ACount = std::min(k, listNodeCount - k + 1);
int BCount = std::max(k, listNodeCount - k + 1);

// If the Left-K node is the same as the Right-K node algorithm
// doesn't work, so if statement. Just return the real head.
if(ACount == BCount)
{
return head->next;
}

// ListNode pointers.
// Set ListNode pointers for left and right pointers.
// A is the left pointer, B is the right pointer.
ListNode* preA = nullptr;
ListNode* A = nullptr;
ListNode* postA = nullptr;

ListNode* preB = nullptr;
ListNode* B = nullptr;
ListNode* postB = nullptr;

int curCount = 1;
cur = head->next;
prev = head;

while(cur != nullptr)
{
if (curCount == ACount)
{
preA = prev;
A = cur;
postA = cur->next;
}
if (curCount == BCount)
{
preB = prev;
B = cur;
postB = cur->next;
break;
}

prev = cur;
cur = cur->next;
++curCount;
}

// Finally algorithm starts.
// Case where A->next == B.
if(A->next == B)
{
postA = A;
preB = B;
}

preA->next = B;
A->next = postB;

B->next = postA;
preB->next = A;

return head->next;

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