Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

ASSIGNMENT 2-DSA

Question 1
Submission Link -https://leetcode.com/problems/invert-binary-tree/submissions/871384676/

Time Complexity-O(n)
Space Complexity-O(1)

Approach-First we will check that the root node is NULL or not and then swap root’s the left and
the right node . We will use the recursive approach here and call the function again for the left
half and right half so that every node’s left and right child gets swapped.
Question 3
Submission link-
https://leetcode.com/problems/validate-binary-search-tree/submissions/831432089/

Time Complexity-O(n)
Space Complexity-O(1)

Approach-First we will check that the root node is NULL or not , if it is NULL then return true.
We will break the problem into two halves. We will make a function to check whether the left
child of a binary search tree is less than the root and the right child is greater than the root.
Update the minimum value in the first half as minimum only and maximum value as root’s value.
In the second half update minimum value as root’s value and maximum value as maximum only.
Question 2
Submission link- https://leetcode.com/problems/lru-cache/submissions/872468270/

Time Complexity-O(1)(for get and put)


Space Complexity-O(1)

Approach-Firstly we will design a class node for holding the data , adding it in a doubly linked
list and start adding a new node in-between two pre-existing nodes.
We take a hashmap for storing the address corresponding to a key. Take the first and last node
of the doubly linked list as head and tail. Initialize doubly linked list in LRU cache().
For the get() function , we will iterate through the map , when found we remove that node from
dll and place it next to the head of the doubly linked list and return its value.
For the put() function we will traverse through the map, if value is already found , we remove the
node from that location and place it after the head node or if it is not there we will make a new
node and then add it to the doubly linked list.

Question 4

Submission link -https://leetcode.com/problems/jump-game-vii/submissions/878779320/

Time Complexity- O(n)


Space Complexity - O(1)

Approach- Firstly we will check the last character , if it is ‘0’ , then return false.Assuming i has a
current position where we are standing and using standard BFS approach with slight
optimization (to avoid TLE). Slight Optimisation is the use of curr_max variable to avoid extra
traversals. max_curr is the max position which has been visited till previous iteration , start next
iteration after max_curr to avoid duplication indices traversals.
Steps followed are: 1- Initially i=0,insert i into the queue.
2- Pop i from the queue front.
3- Iterate from max(i+minJump,max_curr) to (i+maxJump) and put all the 0’s
indices in queue.
4-Update curr_max after complete iteration.

You might also like