You are on page 1of 16

LEET CODE PROBLEMS

Name:Swathi Rani M
Class:II MCA
PROBLEM:1
TWO SUM
ALGORITHM :
STEP 1: Initialize a Dictionary –Create an empty dictionary to store numbers
and their indices.
STEP 2: Iterate through the array-Use a loop to go through each element in the
array.
STEP 3: Calculate Complement-Subtract the current element from the target
sum to find its complement.
STEP 4:Check if complement exists-if the complement is in the dictionary,
return the indices of the current element and its complement.
STEP 5:Return Result-if no solution is found, return an indicator or an empty
list.

PROBLEM:2
ADD TWO NUMBERS
ALGORITHM :

Step 1: Initialize a Dummy Node and Current Pointer

Step 2: Initialize Carry

Step 3: Iterate Through the Linked Lists

Step 4: Extract Digits and Calculate Sum

Step 5: Update Carry and Calculate Digit

Step 6: Create New Node and Move to Next

Step 7: Handle Final Carry

Step 8: Return Result

PROBLEM:3
LONGEST SUBSTRING WITHOUT REPEATING
CHARACTERS

Step 1: Initialize Pointers and Variables

Step 2: Iterate Through the string

Step 3: Check for Repeating Characters

Step 4: Update Dictionary and Longest Length

Step 5: Move Pointers

Step 6: Return Longest Length

PROBLEM:4

MEDIAM OF 2 SORTED ARRAYS

ALGORITHM:

Step 1: Ensure the Smaller Array is the First Array


Step 2: Binary Search on the Smaller Array
Step 3: Calculate Partition Indices
Step 4: Check for Median
Step 5: Calculate Median
PROBLEM:5

LONGEST PALINDROMIC SUBSTRING

PROBLEM:6

CONTAINER WITH MOST WATER

ALGORITHM :

Step 1: Create a Roman-to-Integer Mapping

Step 2: Initialize Variables

Step 3: Iterate Through the Roman Numeral String

Step 4: Check for Subtractive Notation


Step 5:Return result

PROBLEM:7

ROMAN TO INTEGER

ALGORITHM :

Step 1: Create a Roman-to-Integer Mapping

Step 2: Initialize Variables

Step 3: Iterate Through the Roman Numeral String

Step 4: Check for Subtractive Notation

Step 5:Return
PROBLEM:8

Step 1: Create a Roman-to-Integer Mapping

Step 2: Initialize Variables

Step 3: Iterate Through the Roman Numeral String

Step 4: Check for Subtractive Notation

Step 5:Return result

LONGEST COMMON PREFIX

PROBLEM : 9

3 SUM
ALGORITHM :
Step 1: Sort the Array
Step 2: Iterate Through the Array
Step 3: Use Two Pointers
Step 4: Move the Pointers Inward
Step 5: Update Pointers
Step 6: Skip Duplicates
Step 7: Return Result

PROBLEM :10

LETTER COMBINATION OF A PHONE NUMBER

ALGORITHM :
Step 1: Create a Mapping
Step 2: Initialize Variables
Step 3: Backtracking Function
Step 4: Call Backtracking Function
Step 5: Return Result
PROBLEM:11
MERGE K SORTED LISTS

ALGORITHM:
Step 1: Initialize a Min Heap
Step 2: Insert Initial Elements
Step 3: Pop and Append
Step 4: Return Result

PROBLEM:12
REVERSE NODES IN K-GROUPS

ALGORITHM :
Step 1: Count Nodes
Step 2: Determine Number of Reversals
Step 3: Reverse K Nodes
Step 4: Helper Function for Reversal
Step 5: Return Result

PROBLEM :13
VALID PARENTHESES
ALGORITHM :
Step 1: Initialize a Stack
Step 2: Iterate Through the String
Step 3: Check for Opening Parentheses
Step 4: Check for Closing Parentheses
Step 5: Check for Empty Stack
Step 6: Return Result
PROBLEM :14
FIRST MISSING POSITIVE
ALGORITHM :
Step 1: Iterate Through the Array
Step 2: Place Elements in Correct Position
Step 3: Find the Missing Positive
Step 4: Handle Edge Cases
Step 5: Return Result

PROBLEM :15

TRAPPING RAIN WATER


ALGORITHM:
Step 1: Initialize Two Pointers
Step 2: Initialize Variables
Step 3: Traverse the Array
Step 4: Update Maximum Heights
Step 5: Calculate Trapped Water
Step 6: Move Pointers
Step 7: Return Result

PROBLEM :16
N-QUEENS
ALGORITHM :
Step 1: Initialize Board
Step 2: Place Queens (Backtracking)
Step 3: Backtrack on Failure
Step 4: Continue Until Solution
Step 5: Return Solutions
PROBLEM: 17
MINIMUM WINDOWS SUBSTRING
ALGORITHM:
Step 1: Initialize Pointers
Step 2: Initialize Counters
Step 3: Expand the Window
Step 4: Contract the Window
Step 5: Track Minimum Window
Step 6: Continue Until the End
Step 7: Return Result
PROBLEM :18
LARGEST RECTANGLE IN HISTOGRAM

ALGORITHM :
Step 1: Initialize Stack
Step 2: Iterate Through Histogram
Step 3: Process Bars Using Stack
Step 4: Push Current Bar Index
Step 5: Process Remaining Bars
Step 6: Return Result
PROBLEM:19
BINARY TREE MAXIMUM PATH SUM

ALGORITHM :
Step 1: Define a Recursive Function
Step 2: Base Case
Step 3: Recursively Calculate Maximum Path Sum
Step 4: Update Global Maximum
Step 5: Return Maximum Path Sum for Current Subtree
Step 6: Call the Function
Step 7: Return the Global Maximum

PROBLEM :20
SLIDING WINDOW MAXIMUM

ALGORITHM :
Step 1: Initialize Deque
Step 2: Iterate Through the Array
Step 3: Maintain Monotonic Decreasing Order
Step 4: Add Elements to the Deque
Step 5: Remove Out-of-Window Elements
Step 6: Update Maximum
Step 7: Repeat Until the EndStep 8: Return Result
PROBLEM :21
FIND MEDIAN FROM DATA STREAM

ALGORITHM :
Step 1: Use Two Heaps
Step 2: Balance the Heaps
Step 3: Insert Elements
Step 4: Calculate Median
Step 5:Return result
Rough work : -

You might also like