Skip to content

All the codes used to solve various competitive coding problems on Leetcode, GeeksForGeeks etc.

Notifications You must be signed in to change notification settings

nehamalcom/Competitive-Coding

Repository files navigation

Competitive-Coding

Maximum Level Sum of a Binary Tree

Given the root of a binary tree, the level of its root is 1, the level of its children is 2, and so on. Return the smallest level x such that the sum of all the values of nodes at level x is maximal. Source: LeetCode https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree/

Maximum Guests at a Party (Maximum Intervals Overlapping Problem)

Consider a big party where N guests came to it and a log register for guest’s entry and exit times was maintained. Find the minimum time at which there were maximum guests at the party. Note that entries in the register are not in any order. Source: GeeksForGeeks https://practice.geeksforgeeks.org/problems/maximum-intervals-overlap5708/1#

Unique Email Addresses

Every valid email consists of a local name and a domain name, separated by the '@' sign. Besides lowercase letters, the email may contain one or more '.' or '+'... Source: LeetCode https://leetcode.com/problems/unique-email-addresses/

Lowest Common Ancestor of a Binary Tree

Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree and return the LCA node (not just the value) Source: LeetCode https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/

Two Sum Less Than K

Given an array nums of integers and integer k, return the maximum sum such that there exists i < j with nums[i] + nums[j] = sum and sum < k. If no i, j exist satisfying this equation, return -1. Source: LeetCode https://leetcode.com/problems/two-sum-less-than-k/

Minimum Number of Days to Make m Bouquets

Given an integer array bloomDay, an integer m and an integer k. We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden. The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet. Return the minimum number of days you need to wait to be able to make m bouquets from the garden. Source: Leetcode https://leetcode.com/problems/minimum-number-of-days-to-make-m-bouquets/

Decode string

Given an encoded string, return its decoded string. The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note that k is guaranteed to be a positive integer. You may assume that the input string is always valid; No extra white spaces, square brackets are well-formed, etc. Source: Leetcode https://leetcode.com/problems/decode-string

Minimum Number of Days to Make m Bouquets from Adjacent Flowers in the Garden

Given an integer array bloomDay, an integer m and an integer k. We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden. The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet. Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make bouquets return -1. Source: Leetcode https://leetcode.com/problems/minimum-number-of-days-to-make-m-bouquets/

Partition into two Equal Subsets

Given a non-empty array nums containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal. Source: Leetcode https://leetcode.com/problems/partition-equal-subset-sum/

Single Row Keyboard

There is a special keyboard with all keys in a single row. Given a string keyboard of length 26 indicating the layout of the keyboard (indexed from 0 to 25). Initially, your finger is at index 0. To type a character, you have to move your finger to the index of the desired character. The time taken to move your finger from index i to index j is |i - j|. You want to type a string word. Write a function to calculate how much time it takes to type it with one finger. Source: Leetcode https://leetcode.com/problems/single-row-keyboard/

Longest Increasing Subsequence

Given an integer array nums, return the length of the longest strictly increasing subsequence. A subsequence is a sequence that can be derived from an array by deleting some or no elements without changing the order of the remaining elements. For example, [3,6,2,7] is a subsequence of the array [0,3,1,6,2,2,7]. Source: Leetcode https://leetcode.com/problems/longest-increasing-subsequence/

Maximum Distance to Person

You are given an array representing a row of seats where seats[i] = 1 represents a person sitting in the ith seat, and seats[i] = 0 represents that the ith seat is empty (0-indexed). There is at least one empty seat, and at least one person sitting. Alex wants to sit in the seat such that the distance between him and the closest person to him is maximized. Return that maximum distance to the closest person. Source: Leetcode https://leetcode.com/problems/maximize-distance-to-closest-person/

Backspace String Compare

Given two strings s and t, return true if they are equal when both are typed into empty text editors. '#' means a backspace character. Note that after backspacing an empty text, the text will continue empty. Source: Leetcode https://leetcode.com/problems/backspace-string-compare/

Cousins in Binary Tree

In a binary tree, the root node is at depth 0, and children of each depth k node are at depth k+1. Two nodes of a binary tree are cousins if they have the same depth, but have different parents. We are given the root of a binary tree with unique values, and the values x and y of two different nodes in the tree. Return true if and only if the nodes corresponding to the values x and y are cousins. Source: Leetcode https://leetcode.com/problems/cousins-in-binary-tree/

k Closest Points

Given an array of points where points[i] = [xi, yi] represents a point on the X-Y plane and an integer k, return the k closest points to the origin (0, 0). The distance between two points on the X-Y plane is the Euclidean distance (i.e., √(x1 - x2)2 + (y1 - y2)2). Source: Leetcode https://leetcode.com/problems/k-closest-points-to-origin/

Maximum Distance to a Person

You are given an array representing a row of seats where seats[i] = 1 represents a person sitting in the ith seat, and seats[i] = 0 represents that the ith seat is empty (0-indexed). There is at least one empty seat, and at least one person sitting. Alex wants to sit in the seat such that the distance between him and the closest person to him is maximized. Return that maximum distance to the closest person. Source: Leetcode https://leetcode.com/problems/maximize-distance-to-closest-person/

Product at Index

Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i]. You must write an algorithm that runs in O(n) time and without using the division operation. Source: Leetcode https://leetcode.com/problems/product-of-array-except-self/

Pascal's Triangle

Given an integer numRows, return the first numRows of Pascal's triangle. In Pascal's triangle, each number is the sum of the two numbers directly above it. Source: Leetcode https://leetcode.com/problems/pascals-triangle/

Majority Element II (using Moore's Voting Algorithm)

Given an integer array of size n, find all elements that appear more than n/3 times. Source: Leetcode https://leetcode.com/problems/majority-element-ii/

Unique Paths (Robot)

A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below). How many possible unique paths are there? Source: Leetcode https://leetcode.com/problems/unique-paths/

Longest Consecutive Sequence

Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence that runs in O(logn) time. Source: Leetcode https://leetcode.com/problems/longest-consecutive-sequence/

Two Pointers

Copy List with Random Pointer

A linked list of length n is given such that each node contains an additional random pointer, which could point to any node in the list, or null. Construct a deep copy of the list. The deep copy should consist of exactly n brand new nodes, where each new node has its value set to the value of its corresponding original node. Both the next and random pointer of the new nodes should point to new nodes in the copied list such that the pointers in the original list and copied list represent the same list state. Source: Leetcode https://leetcode.com/problems/copy-list-with-random-pointer/

Remove Duplicates from Sorted Array

Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same. Source: Leetcode https://leetcode.com/problems/remove-duplicates-from-sorted-array/

Max Consecutive Ones

Given a binary array nums, return the maximum number of consecutive 1's in the array. Source: Leetcode https://leetcode.com/problems/max-consecutive-ones/

About

All the codes used to solve various competitive coding problems on Leetcode, GeeksForGeeks etc.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages