Longest Consecutive Sequence Problem

Problem Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Examples Example 1: Input: nums = [100,4,200,1,3,2] Output: 4 Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4. ...

Find the Celebrity Problem

Problem Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may exist one celebrity. The definition of a celebrity is that all the other n - 1 people know him/her but he/she does not know any of them. Now you want to find out who the celebrity is or verify that there is not one. The only thing you are allowed to do is to ask questions like: “Hi, A. Do you know B?” to get information of whether A knows B. You need to find out the celebrity (or verify there is not one) by asking as few questions as possible (in the asymptotic sense). ...

Bitwise AND of Numbers Range Problem

Problem Given two integers left and right that represent the range [left, right], return the bitwise AND of all numbers in this range, inclusive. Examples Example 1: Input: left = 5, right = 7 Output: 4 Example 2: Input: left = 0, right = 0 Output: 0 ...

Knight Probability in Chessboard Problem

Problem On an n x n chessboard, a knight starts at the cell (row, column) and attempts to make exactly k moves. The rows and columns are 0-indexed, so the top-left cell is (0, 0), and the bottom-right cell is (n - 1, n - 1). A chess knight has eight possible moves it can make, as illustrated below. Each move is two cells in a cardinal direction, then one cell in an orthogonal direction. ...

Add Two Numbers represented as Linked List in Forward

Problem What if the digits are stored in regular order instead of reversed order? Example Example 1: Input: l1: 1 -> 0 -> 0 -> 7 l2: 9 -> 3 Output: 1 -> 1 -> 0 -> 0 Explanation: Here are 2 numbers: 1007 and 93. On addition we get 1100. ...

Course Schedule 1 - Is it Possible

Problem There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai. For example, the pair [0, 1], indicates that to take course 0 you have to first take course 1. Return true if you can finish all courses. Otherwise, return false. ...

Course Schedule 2 - Get Ordered Courses

Problem There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai. For example, the pair [0, 1], indicates that to take course 0 you have to first take course 1. Return the ordering of courses you should take to finish all courses. If there are many valid answers, return any of them. If it is impossible to finish all courses, return an empty array. ...

All Nodes Distance K in Binary Tree Problem

Problem Given the root of a binary tree, the value of a target node target, and an integer k, return an array of the values of all nodes that have a distance k from the target node. You can return the answer in any order. Examples graph TB; 3 --- 5 & 1; 5 --- 6 & 2; 1 --- 0 & 8; 2 --- 7 & 4; style 5 fill:#FF9933 style 1 fill:#138808 style 7 fill:#138808 style 4 fill:#138808 ...

Single Number 2 - All elements except one occur thrice

Problem Given an integer array nums where every element appears three times except for one, which appears exactly once. Find the single element and return it. You must implement a solution with a linear runtime complexity and use only constant extra space. Examples Example 1: Input : nums = [1, 2, 4, 3, 3, 2, 2, 3, 1, 1] Output : 4 ...

Kth Largest Element in an Array

Problem Given an integer array nums and an integer k, return the kth largest element in the array. Note that it is the kth largest element in the sorted order, not the kth distinct element. Example 1: Input: nums = [3,2,1,5,6,4], k = 2 Output: 5 Example 2: Input: nums = [3,2,3,1,2,4,5,5,6], k = 4 Output: 4 ...

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