Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

WORKSHEET 1.1

Student Name: Shreshtha Pal UID: 22BCS80010


Branch: BE-CSE(LEET) Section/Group: 634/A
Semester: 6th Date Of Performance: 16/01/2024
Subject Name: Advance Programming Subject Code: 21CSP-351
Lab - II

1. Aim: To implement the concept of Arrays, Queues, Stack and Linked List.

2. Objective: Understand the problem and find out a better approach to solve a particular
problem.

3. Problem 1: Jump Game II


You are given a 0-indexed array of integers nums of length n. You are initially
positioned at nums[0].
Each element nums[i] represents the maximum length of a forward jump from index i.
In other words, if you are at nums[i], you can jump to any nums[i + j] where:
0 <= j <= nums[i] and
i+j<n
Return the minimum number of jumps to reach nums[n - 1]. The test cases are
generated such that you can reach nums[n - 1].

Code & Output:


class Solution:
def jump(self, nums):
n = len(nums)
if n == 1:
return 0
jumps = 0
current_max = 0
farthest = 0
for i in range(n - 1):
farthest = max(farthest, i + nums[i])

NAME: SHRESHTHA PAL UID: 22BCS80010


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

if i == current_max:
# We need to jump
current_max = farthest
jumps += 1
if current_max >= n - 1:
break # Reached the end
return jumps

4. Problem 2: Remove duplicate from sorted list II


Given the head of a sorted linked list, delete all nodes that have duplicate numbers,
leaving only distinct numbers from the original list. Return the linked list sorted as well.

Code & Output:


class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def deleteDuplicates(self, head):
dummy = ListNode(0)
dummy.next = head
current = dummy

NAME: SHRESHTHA PAL UID: 22BCS80010


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

while current.next and current.next.next:


if current.next.val == current.next.next.val:
duplicate_val = current.next.val
while current.next and current.next.val == duplicate_val:
current.next = current.next.next
else:
current = current.next
return dummy.next
def printList(self, head):
while head:
print(head.val, end=" -> ")
head = head.next
print("None")
def createLinkedList(self, values):
if not values:
return None
head = ListNode(values[0])
current = head

for val in values[1:]:


current.next = ListNode(val)
current = current.next

return head

NAME: SHRESHTHA PAL UID: 22BCS80010


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

5. Learning Outcomes:

 Arrays:
 Memory is allocated in consecutive blocks.
 Efficiency of random access.
 Knowledge of static and dynamic arrays.
 Queue:
 The First-In-First-Out (FIFO) principle applies to queues.
 Queue operations: add, remove, rearrange.
 Stack:
 The Last-In-First-Out (LIFO) principle applies to stacks.
 Push and pop operations in stacks.
 Linked Lists:
 Memory allocation with nodes that is dynamic.
 Lists with one versus two connected pairs.
 Insertion and deletion in linked lists.
 Manipulation, reversal, and traversal.

NAME: SHRESHTHA PAL UID: 22BCS80010

You might also like