Divide Two Integers Problem

Problem Given two integers dividend and divisor, divide two integers without using multiplication, division, and mod operator. The integer division should truncate toward zero, which means losing its fractional part. For example, 8.345 would be truncated to 8, and -2.7335 would be truncated to -2. Return the quotient after dividing dividend by divisor. Note: Assume we are dealing with an environment that could only store integers within the 32-bit signed integer range: [−2^31, 2^31 − 1]. For this problem, if the quotient is strictly greater than 2^31 - 1, then return 2^31 - 1, and if the quotient is strictly less than -2^31, then return -2^31. ...

Sort a Linked List

Problem Given the head of a linked list, return the list after sorting it in ascending order. Examples Example 1: Input: head = [4,2,1,3] Output: [1,2,3,4] Example 2: Input: head = [-1,5,3,4,0] Output: [-1,0,3,4,5] Solution Method 1 - Insertion Sort Insertion Sort on List ...

Dutch National Flag DNF problem

Problem Given an array nums with n objects colored red, white, or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white, and blue. We will use the integers 0, 1, and 2 to represent the color red, white, and blue, respectively. You must solve this problem without using the library’s sort function. By the way this is Dutch National Flag - 🇳🇱. ...

Longest Increasing Subsequence LIS Problem

Problem Given an unsorted array of integers, find the length of longest increasing subsequence. Examples Example 1: Input: A = [10,9,2,5,3,7,101,18] Output: 4 Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4. ...

Kth Smallest Element Using Randomized Selection

Problem Input - array A with n distinct numbers and a number i ∈ {1, 2, …, n} Output : ith order statistic i.e. ith smallest element of the input array Solution We have already seen multiple solutions here - Kth OR Nth Smallest Element in an Array. Here will focus on Linear time or O(n) solution. Finding the ith smallest element may seem difficult at first, but is it possible to find some O(n) or linear time solution. ...

Container With Most Water

Problem You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]). Find two lines that together with the x-axis form a container, such that the container contains the most water. Return the maximum amount of water a container can store. Notice that you may not slant the container. ...

Count Complete Tree Nodes Problem

Problem Given the root of a complete binary tree, return the number of the nodes in the tree. Definition and Properties of Complete Binary Tree Complete Binary Tree Examples Example 1: 1 / \ 2 3 / \ / 4 5 6 ...

Longest Common Subsequence LCS 1 - Get Length

Problem Given two strings text1 and text2, return the length of their longest common subsequence. If there is no common subsequence, return 0. A subsequence of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters. For example, "ace" is a subsequence of "abcde". A common subsequence of two strings is a subsequence that is common to both strings. ...

Longest Common Subsequence LCS 2 - Get Subsequence

Problem Given two sequences, return the longest subsequence present in both of them. Examples Example 1: Input: text1 = "abcde", text2 = "ace" Output: "ace" Explanation: The longest common subsequence is "ace". Example 2: Input: text1 = "abc", text2 = "abc" Output: "abc" Explanation: The longest common subsequence is "abc". ...

Lowest Common Ancestor of a Binary Tree

Problem Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. Definition Lowest Common Ancestor Definition Examples Example 1: graph TD; 6; 6 --- 2; 6 --- 8; 2 --- 0; 2 --- 4; 8 --- 7; 8 --- 9; 4 --- 3; 4 --- 5; style 2 fill:#FF9933 style 8 fill:#FF9933 style 6 fill:#3399FF ...

This site uses cookies to improve your experience on our website. By using and continuing to navigate this website, you accept this. Privacy Policy