id
int64 1
2.39k
| title
stringlengths 3
79
| title_slug
stringlengths 3
79
| question_content
stringlengths 38
1.55k
⌀ | tag
stringlengths 4
122
⌀ | level
stringclasses 3
values | success_rate
float64 14.2
95.5
| total_submission
int64 186
13.2M
| total_accepted
int64 153
6.4M
| question_likes
int64 4
31.2k
| question_dislikes
int64 0
11.5k
| question_hints
stringlengths 19
3.98k
⌀ | similar_question_ids
stringlengths 1
51
⌀ |
---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | Two Sum | two-sum | Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order. | Array,Hash Table | Easy | 48.5 | 13,207,990 | 6,403,821 | 31,242 | 988 | A really brute force way would be to search for all possible pairs of numbers but that would be too slow. Again, it's best to try out brute force solutions for just for completeness. It is from these brute force solutions that you can come up with optimizations. So, if we fix one of the numbers, say x, we have to scan the entire array to find the next number y which is value - x where value is the input parameter. Can we change our array somehow so that this search becomes faster? The second train of thought is, without changing the array, can we use additional space somehow? Like maybe a hash map to speed up the search? | 15,18,167,170,560,653,1083,1798,1830,2116,2133,2320 |
2 | Add Two Numbers | add-two-numbers | You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself. | Linked List,Math,Recursion | Medium | 38.5 | 6,987,977 | 2,690,949 | 17,799 | 3,682 | null | 43,67,371,415,445,1031,1774 |
3 | Longest Substring Without Repeating Characters | longest-substring-without-repeating-characters | Given a string s, find the length of the longest substring without repeating characters. | Hash Table,String,Sliding Window | Medium | 33 | 9,621,884 | 3,175,843 | 22,941 | 1,027 | null | 159,340,1034,1813,2209 |
4 | Median of Two Sorted Arrays | median-of-two-sorted-arrays | Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). | Array,Binary Search,Divide and Conquer | Hard | 34 | 3,941,694 | 1,340,565 | 15,987 | 1,964 | null | null |
5 | Longest Palindromic Substring | longest-palindromic-substring | Given a string s, return the longest palindromic substring in s. | String,Dynamic Programming | Medium | 31.8 | 5,618,701 | 1,784,028 | 17,097 | 1,005 | How can we reuse a previously computed palindrome to compute a larger palindrome? If “aba” is a palindrome, is “xabax” a palindrome? Similarly is “xabay” a palindrome? Complexity based hint:
If we use brute-force and check whether for every start and end position a substring is a palindrome we have O(n^2) start - end pairs and O(n) palindromic checks. Can we reduce the time for palindromic checks to O(1) by reusing some previous computation. | 214,266,336,516,647 |
6 | Zigzag Conversion | zigzag-conversion | The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) And then read line by line: "PAHNAPLSIIGYIR" Write the code that will take a string and make this conversion given a number of rows: | String | Medium | 41.4 | 1,804,469 | 747,158 | 3,570 | 8,234 | null | null |
7 | Reverse Integer | reverse-integer | Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0. Assume the environment does not allow you to store 64-bit integers (signed or unsigned). | Math | Medium | 26.7 | 7,589,410 | 2,022,695 | 6,971 | 9,596 | null | 8,190,2238 |
8 | String to Integer (atoi) | string-to-integer-atoi | Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer (similar to C/C++'s atoi function). The algorithm for myAtoi(string s) is as follows: Note: | String | Medium | 16.5 | 5,960,030 | 983,656 | 1,583 | 4,562 | null | 7,65,2168 |
9 | Palindrome Number | palindrome-number | Given an integer x, return true if x is palindrome integer. An integer is a palindrome when it reads the same backward as forward. | Math | Easy | 52.3 | 3,823,313 | 2,000,757 | 5,593 | 2,094 | Beware of overflow when you reverse the integer. | 234,1375 |
10 | Regular Expression Matching | regular-expression-matching | Given an input string s and a pattern p, implement regular expression matching with support for '.' and '*' where: The matching should cover the entire input string (not partial). | String,Dynamic Programming,Recursion | Hard | 28.2 | 2,370,376 | 668,919 | 7,928 | 1,177 | null | 44 |
11 | Container With Most Water | container-with-most-water | 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. | Array,Two Pointers,Greedy | Medium | 53.9 | 2,673,605 | 1,441,824 | 16,437 | 930 | The aim is to maximize the area formed between the vertical lines. The area of any container is calculated using the shorter line as length and the distance between the lines as the width of the rectangle.
Area = length of shorter vertical line * distance between lines
We can definitely get the maximum width container as the outermost lines have the maximum distance between them. However, this container might not be the maximum in size as one of the vertical lines of this container could be really short. Start with the maximum width container and go to a shorter width container if there is a vertical line longer than the current containers shorter line. This way we are compromising on the width but we are looking forward to a longer length container. | 42 |
12 | Integer to Roman | integer-to-roman | Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. For example, 2 is written as II in Roman numeral, just two one's added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: Given an integer, convert it to a roman numeral. | Hash Table,Math,String | Medium | 59.4 | 1,135,654 | 674,026 | 3,029 | 3,953 | null | 13,273 |
13 | Roman to Integer | roman-to-integer | Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. For example, 2 is written as II in Roman numeral, just two one's added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: Given a roman numeral, convert it to an integer. | Hash Table,Math,String | Easy | 57.9 | 2,611,149 | 1,511,737 | 3,805 | 265 | Problem is simpler to solve by working the string from back to front and using a map. | 12 |
14 | Longest Common Prefix | longest-common-prefix | Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string "". | String | Easy | 39.3 | 3,891,029 | 1,529,495 | 7,636 | 2,930 | null | null |
15 | 3Sum | 3sum | Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0. Notice that the solution set must not contain duplicate triplets. | Array,Two Pointers,Sorting | Medium | 31 | 6,090,299 | 1,886,780 | 17,218 | 1,653 | So, we essentially need to find three numbers x, y, and z such that they add up to the given value. If we fix one of the numbers say x, we are left with the two-sum problem at hand! For the two-sum problem, if we fix one of the numbers, say x, we have to scan the entire array to find the next numbery which is value - x where value is the input parameter. Can we change our array somehow so that this search becomes faster? The second train of thought for two-sum is, without changing the array, can we use additional space somehow? Like maybe a hash map to speed up the search? | 1,16,18,259 |
16 | 3Sum Closest | 3sum-closest | Given an integer array nums of length n and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution. | Array,Two Pointers,Sorting | Medium | 47.1 | 1,665,933 | 784,188 | 5,673 | 242 | null | 15,259 |
17 | Letter Combinations of a Phone Number | letter-combinations-of-a-phone-number | Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. Return the answer in any order. A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters. | Hash Table,String,Backtracking | Medium | 53.5 | 2,146,094 | 1,147,569 | 9,594 | 668 | null | 22,39,401 |
18 | 4Sum | 4sum | Given an array nums of n integers, return an array of all the unique quadruplets [nums[a], nums[b], nums[c], nums[d]] such that: You may return the answer in any order. | Array,Two Pointers,Sorting | Medium | 37.2 | 1,528,323 | 568,804 | 6,003 | 685 | null | 1,15,454,2122 |
19 | Remove Nth Node From End of List | remove-nth-node-from-end-of-list | Given the head of a linked list, remove the nth node from the end of the list and return its head. | Linked List,Two Pointers | Medium | 38.2 | 3,510,223 | 1,340,984 | 9,607 | 457 | Maintain two pointers and update one with a delay of n steps. | 528,1618,2216 |
20 | Valid Parentheses | valid-parentheses | Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: | String,Stack | Easy | 40.9 | 5,271,267 | 2,154,237 | 12,523 | 554 | An interesting property about a valid parenthesis expression is that a sub-expression of a valid expression should also be a valid expression. (Not every sub-expression) e.g.
{ { } [ ] [ [ [ ] ] ] } is VALID expression
[ [ [ ] ] ] is VALID sub-expression
{ } [ ] is VALID sub-expression
Can we exploit this recursive structure somehow? What if whenever we encounter a matching pair of parenthesis in the expression, we simply remove it from the expression? This would keep on shortening the expression. e.g.
{ { ( { } ) } }
|_|
{ { ( ) } }
|______|
{ { } }
|__________|
{ }
|________________|
VALID EXPRESSION! The stack data structure can come in handy here in representing this recursive structure of the problem. We can't really process this from the inside out because we don't have an idea about the overall structure. But, the stack can help us process this recursively i.e. from outside to inwards. | 22,32,301,1045,2221 |
21 | Merge Two Sorted Lists | merge-two-sorted-lists | You are given the heads of two sorted linked lists list1 and list2. Merge the two lists in a one sorted list. The list should be made by splicing together the nodes of the first two lists. Return the head of the merged linked list. | Linked List,Recursion | Easy | 60.2 | 3,519,901 | 2,118,031 | 11,753 | 1,070 | null | 23,88,148,244,1774,2071 |
22 | Generate Parentheses | generate-parentheses | Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. | String,Dynamic Programming,Backtracking | Medium | 69.9 | 1,475,969 | 1,032,134 | 12,456 | 483 | null | 17,20,2221 |
23 | Merge k Sorted Lists | merge-k-sorted-lists | You are given an array of k linked-lists lists, each linked-list is sorted in ascending order. Merge all the linked-lists into one sorted linked-list and return it. | Linked List,Divide and Conquer,Heap (Priority Queue),Merge Sort | Hard | 46.9 | 2,627,265 | 1,233,044 | 11,827 | 462 | null | 21,264 |
24 | Swap Nodes in Pairs | swap-nodes-in-pairs | Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list's nodes (i.e., only nodes themselves may be changed.) | Linked List,Recursion | Medium | 58.6 | 1,452,282 | 851,081 | 6,765 | 289 | null | 25,528 |
25 | Reverse Nodes in k-Group | reverse-nodes-in-k-group | Given the head of a linked list, reverse the nodes of the list k at a time, and return the modified list. k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes, in the end, should remain as it is. You may not alter the values in the list's nodes, only nodes themselves may be changed. | Linked List,Recursion | Hard | 50.9 | 983,085 | 500,025 | 6,837 | 485 | null | 24,528,2196 |
26 | Remove Duplicates from Sorted Array | 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. Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array nums. More formally, if there are k elements after removing the duplicates, then the first k elements of nums should hold the final result. It does not matter what you leave beyond the first k elements. Return k after placing the final result in the first k slots of nums. Do not allocate extra space for another array. You must do this by modifying the input array in-place with O(1) extra memory. Custom Judge: The judge will test your solution with the following code: If all assertions pass, then your solution will be accepted. | Array,Two Pointers | Easy | 48.7 | 4,249,259 | 2,071,403 | 6,360 | 9,901 | In this problem, the key point to focus on is the input array being sorted. As far as duplicate elements are concerned, what is their positioning in the array when the given array is sorted? Look at the image above for the answer. If we know the position of one of the elements, do we also know the positioning of all the duplicate elements? We need to modify the array in-place and the size of the final array would potentially be smaller than the size of the input array. So, we ought to use a two-pointer approach here. One, that would keep track of the current element in the original array and another one for just the unique elements. Essentially, once an element is encountered, you simply need to bypass its duplicates and move on to the next unique element. | 27,80 |
27 | Remove Element | remove-element | Given an integer array nums and an integer val, remove all occurrences of val in nums in-place. The relative order of the elements may be changed. Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array nums. More formally, if there are k elements after removing the duplicates, then the first k elements of nums should hold the final result. It does not matter what you leave beyond the first k elements. Return k after placing the final result in the first k slots of nums. Do not allocate extra space for another array. You must do this by modifying the input array in-place with O(1) extra memory. Custom Judge: The judge will test your solution with the following code: If all assertions pass, then your solution will be accepted. | Array,Two Pointers | Easy | 51.1 | 2,423,858 | 1,239,786 | 3,451 | 5,135 | The problem statement clearly asks us to modify the array in-place and it also says that the element beyond the new length of the array can be anything. Given an element, we need to remove all the occurrences of it from the array. We don't technically need to remove that element per-say, right? We can move all the occurrences of this element to the end of the array. Use two pointers! Yet another direction of thought is to consider the elements to be removed as non-existent. In a single pass, if we keep copying the visible elements in-place, that should also solve this problem for us. | 26,203,283 |
28 | Implement strStr() | implement-strstr | Implement strStr(). Given two strings needle and haystack, return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Clarification: What should we return when needle is an empty string? This is a great question to ask during an interview. For the purpose of this problem, we will return 0 when needle is an empty string. This is consistent to C's strstr() and Java's indexOf(). | Two Pointers,String,String Matching | Easy | 35.7 | 3,395,848 | 1,213,333 | 3,863 | 3,591 | null | 214,459 |
29 | Divide Two Integers | divide-two-integers | 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: [−231, 231 − 1]. For this problem, if the quotient is strictly greater than 231 - 1, then return 231 - 1, and if the quotient is strictly less than -231, then return -231. | Math,Bit Manipulation | Medium | 17 | 2,716,082 | 462,278 | 2,666 | 9,333 | null | null |
30 | Substring with Concatenation of All Words | substring-with-concatenation-of-all-words | You are given a string s and an array of strings words of the same length. Return all starting indices of substring(s) in s that is a concatenation of each word in words exactly once, in any order, and without any intervening characters. You can return the answer in any order. | Hash Table,String,Sliding Window | Hard | 28 | 894,344 | 250,616 | 1,893 | 1,828 | null | 76 |
31 | Next Permutation | next-permutation | A permutation of an array of integers is an arrangement of its members into a sequence or linear order. The next permutation of an array of integers is the next lexicographically greater permutation of its integer. More formally, if all the permutations of the array are sorted in one container according to their lexicographical order, then the next permutation of that array is the permutation that follows it in the sorted container. If such arrangement is not possible, the array must be rearranged as the lowest possible order (i.e., sorted in ascending order). Given an array of integers nums, find the next permutation of nums. The replacement must be in place and use only constant extra memory. | Array,Two Pointers | Medium | 36.3 | 2,166,111 | 787,106 | 10,346 | 3,360 | null | 46,47,60,267,1978 |
32 | Longest Valid Parentheses | longest-valid-parentheses | Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. | String,Dynamic Programming,Stack | Hard | 31.2 | 1,518,755 | 474,545 | 7,527 | 256 | null | 20 |
33 | Search in Rotated Sorted Array | search-in-rotated-sorted-array | There is an integer array nums sorted in ascending order (with distinct values). Prior to being passed to your function, nums is possibly rotated at an unknown pivot index k (1 <= k < nums.length) such that the resulting array is [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] (0-indexed). For example, [0,1,2,4,5,6,7] might be rotated at pivot index 3 and become [4,5,6,7,0,1,2]. Given the array nums after the possible rotation and an integer target, return the index of target if it is in nums, or -1 if it is not in nums. You must write an algorithm with O(log n) runtime complexity. | Array,Binary Search | Medium | 37.7 | 3,795,739 | 1,431,906 | 13,769 | 879 | null | 81,153,2273 |
34 | Find First and Last Position of Element in Sorted Array | find-first-and-last-position-of-element-in-sorted-array | Given an array of integers nums sorted in non-decreasing order, find the starting and ending position of a given target value. If target is not found in the array, return [-1, -1]. You must write an algorithm with O(log n) runtime complexity. | Array,Binary Search | Medium | 40 | 2,648,073 | 1,058,127 | 10,191 | 284 | null | 278,2165,2210 |
35 | Search Insert Position | search-insert-position | Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You must write an algorithm with O(log n) runtime complexity. | Array,Binary Search | Easy | 42.3 | 3,247,274 | 1,374,840 | 7,432 | 401 | null | 278 |
36 | Valid Sudoku | valid-sudoku | Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules: Note: | Array,Hash Table,Matrix | Medium | 54.9 | 1,293,488 | 710,020 | 4,857 | 695 | null | 37,2254 |
37 | Sudoku Solver | sudoku-solver | Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy all of the following rules: The '.' character indicates empty cells. | Array,Backtracking,Matrix | Hard | 53.7 | 634,540 | 340,851 | 4,947 | 149 | null | 36,1022 |
38 | Count and Say | count-and-say | The count-and-say sequence is a sequence of digit strings defined by the recursive formula: To determine how you "say" a digit string, split it into the minimal number of groups so that each group is a contiguous section all of the same character. Then for each group, say the number of characters, then say the character. To convert the saying into a digit string, replace the counts with a number and concatenate every saying. For example, the saying and conversion for digit string "3322251": Given a positive integer n, return the nth term of the count-and-say sequence. | String | Medium | 48.6 | 1,265,537 | 614,433 | 1,383 | 3,480 | The following are the terms from n=1 to n=10 of the count-and-say sequence:
1. 1
2. 11
3. 21
4. 1211
5. 111221
6. 312211
7. 13112221
8. 1113213211
9. 31131211131221
10. 13211311123113112211 To generate the nth term, just count and say the n-1th term. | 271,443 |
39 | Combination Sum | combination-sum | Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. You may return the combinations in any order. The same number may be chosen from candidates an unlimited number of times. Two combinations are unique if the frequency of at least one of the chosen numbers is different. It is guaranteed that the number of unique combinations that sum up to target is less than 150 combinations for the given input. | Array,Backtracking | Medium | 65.3 | 1,614,657 | 1,054,189 | 10,510 | 224 | null | 17,40,77,216,254,377 |
40 | Combination Sum II | combination-sum-ii | Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sum to target. Each number in candidates may only be used once in the combination. Note: The solution set must not contain duplicate combinations. | Array,Backtracking | Medium | 52.3 | 1,035,167 | 541,022 | 5,048 | 134 | null | 39 |
41 | First Missing Positive | first-missing-positive | Given an unsorted integer array nums, return the smallest missing positive integer. You must implement an algorithm that runs in O(n) time and uses constant extra space. | Array,Hash Table | Hard | 35.9 | 1,845,590 | 662,571 | 9,261 | 1,303 | Think about how you would solve the problem in non-constant space. Can you apply that logic to the existing space? We don't care about duplicates or non-positive integers Remember that O(2n) = O(n) | 268,287,448,770 |
42 | Trapping Rain Water | trapping-rain-water | Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining. | Array,Two Pointers,Dynamic Programming,Stack,Monotonic Stack | Hard | 56.3 | 1,933,682 | 1,089,492 | 17,879 | 252 | null | 11,238,407,756 |
43 | Multiply Strings | multiply-strings | Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string. Note: You must not use any built-in BigInteger library or convert the inputs to integer directly. | Math,String,Simulation | Medium | 37.6 | 1,334,465 | 502,405 | 4,286 | 1,685 | null | 2,66,67,415 |
44 | Wildcard Matching | wildcard-matching | Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '?' and '*' where: The matching should cover the entire input string (not partial). | String,Dynamic Programming,Greedy,Recursion | Hard | 26.5 | 1,428,005 | 378,580 | 4,548 | 204 | null | 10 |
45 | Jump Game II | jump-game-ii | Given an array of non-negative integers nums, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of jumps. You can assume that you can always reach the last index. | Array,Dynamic Programming,Greedy | Medium | 37 | 1,576,789 | 583,374 | 7,769 | 291 | null | 55,1428,2001 |
46 | Permutations | permutations | Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order. | Array,Backtracking | Medium | 72.1 | 1,572,570 | 1,133,403 | 9,957 | 181 | null | 31,47,60,77 |
47 | Permutations II | permutations-ii | Given a collection of numbers, nums, that might contain duplicates, return all possible unique permutations in any order. | Array,Backtracking | Medium | 54 | 1,105,821 | 596,907 | 4,794 | 90 | null | 31,46,267,1038 |
48 | Rotate Image | rotate-image | You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise). You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation. | Array,Math,Matrix | Medium | 66.4 | 1,295,324 | 860,014 | 8,777 | 473 | null | 2015 |
49 | Group Anagrams | group-anagrams | Given an array of strings strs, group the anagrams together. You can return the answer in any order. An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. | Hash Table,String,Sorting | Medium | 64 | 2,043,076 | 1,307,071 | 8,945 | 308 | null | 242,249 |
50 | Pow(x, n) | powx-n | Implement pow(x, n), which calculates x raised to the power n (i.e., xn). | Math,Recursion | Medium | 32.3 | 2,733,809 | 883,674 | 4,323 | 5,309 | null | 69,372 |
51 | N-Queens | n-queens | The n-queens puzzle is the problem of placing n queens on an n x n chessboard such that no two queens attack each other. Given an integer n, return all distinct solutions to the n-queens puzzle. You may return the answer in any order. Each solution contains a distinct board configuration of the n-queens' placement, where 'Q' and '.' both indicate a queen and an empty space, respectively. | Array,Backtracking | Hard | 57.2 | 625,138 | 357,360 | 5,603 | 152 | null | 52,1043 |
52 | N-Queens II | n-queens-ii | The n-queens puzzle is the problem of placing n queens on an n x n chessboard such that no two queens attack each other. Given an integer n, return the number of distinct solutions to the n-queens puzzle. | Backtracking | Hard | 66.1 | 327,225 | 216,188 | 1,670 | 209 | null | 51 |
53 | Maximum Subarray | maximum-subarray | Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum. A subarray is a contiguous part of an array. | Array,Divide and Conquer,Dynamic Programming | Easy | 49.5 | 4,544,146 | 2,249,144 | 19,877 | 969 | null | 121,152,697,1020,1849,1893 |
54 | Spiral Matrix | spiral-matrix | Given an m x n matrix, return all elements of the matrix in spiral order. | Array,Matrix,Simulation | Medium | 41.2 | 1,729,035 | 713,051 | 6,929 | 818 | Well for some problems, the best way really is to come up with some algorithms for simulation. Basically, you need to simulate what the problem asks us to do. We go boundary by boundary and move inwards. That is the essential operation. First row, last column, last row, first column and then we move inwards by 1 and then repeat. That's all, that is all the simulation that we need. Think about when you want to switch the progress on one of the indexes. If you progress on i out of [i, j], you'd be shifting in the same column. Similarly, by changing values for j, you'd be shifting in the same row.
Also, keep track of the end of a boundary so that you can move inwards and then keep repeating. It's always best to run the simulation on edge cases like a single column or a single row to see if anything breaks or not. | 59,921 |
55 | Jump Game | jump-game | You are given an integer array nums. You are initially positioned at the array's first index, and each element in the array represents your maximum jump length at that position. Return true if you can reach the last index, or false otherwise. | Array,Dynamic Programming,Greedy | Medium | 37.7 | 2,575,216 | 971,160 | 10,627 | 612 | null | 45,1428,2001 |
56 | Merge Intervals | merge-intervals | Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input. | Array,Sorting | Medium | 44.8 | 3,102,911 | 1,390,388 | 13,083 | 513 | null | 57,252,253,495,616,715,761,768,1028,2297,2319 |
57 | Insert Interval | insert-interval | You are given an array of non-overlapping intervals intervals where intervals[i] = [starti, endi] represent the start and the end of the ith interval and intervals is sorted in ascending order by starti. You are also given an interval newInterval = [start, end] that represents the start and end of another interval. Insert newInterval into intervals such that intervals is still sorted in ascending order by starti and intervals still does not have any overlapping intervals (merge overlapping intervals if necessary). Return intervals after the insertion. | Array | Medium | 37.3 | 1,314,450 | 490,838 | 4,631 | 334 | null | 56,715 |
58 | Length of Last Word | length-of-last-word | Given a string s consisting of some words separated by some number of spaces, return the length of the last word in the string. A word is a maximal substring consisting of non-space characters only. | String | Easy | 37.4 | 1,916,299 | 716,989 | 875 | 70 | null | null |
59 | Spiral Matrix II | spiral-matrix-ii | Given a positive integer n, generate an n x n matrix filled with elements from 1 to n2 in spiral order. | Array,Matrix,Simulation | Medium | 64.7 | 549,056 | 355,230 | 3,579 | 180 | null | 54,921 |
60 | Permutation Sequence | permutation-sequence | The set [1, 2, 3, ..., n] contains a total of n! unique permutations. By listing and labeling all of the permutations in order, we get the following sequence for n = 3: Given n and k, return the kth permutation sequence. | Math,Recursion | Hard | 42 | 633,058 | 265,892 | 3,601 | 392 | null | 31,46 |
61 | Rotate List | rotate-list | Given the head of a linked list, rotate the list to the right by k places. | Linked List,Two Pointers | Medium | 34.8 | 1,566,873 | 545,760 | 4,972 | 1,265 | null | 189,725 |
62 | Unique Paths | unique-paths | There is a robot on an m x n grid. The robot is initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in time. Given the two integers m and n, return the number of possible unique paths that the robot can take to reach the bottom-right corner. The test cases are generated so that the answer will be less than or equal to 2 * 109. | Math,Dynamic Programming,Combinatorics | Medium | 59.9 | 1,523,012 | 912,411 | 8,532 | 294 | null | 63,64,174,2192 |
63 | Unique Paths II | unique-paths-ii | You are given an m x n integer array grid. There is a robot initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m-1][n-1]). The robot can only move either down or right at any point in time. An obstacle and space are marked as 1 or 0 respectively in grid. A path that the robot takes cannot include any square that is an obstacle. Return the number of possible unique paths that the robot can take to reach the bottom-right corner. The testcases are generated so that the answer will be less than or equal to 2 * 109. | Array,Dynamic Programming,Matrix | Medium | 37.5 | 1,331,781 | 499,048 | 4,533 | 371 | The robot can only move either down or right. Hence any cell in the first row can only be reached from the cell left to it. However, if any cell has an obstacle, you don't let that cell contribute to any path. So, for the first row, the number of ways will simply be
if obstacleGrid[i][j] is not an obstacle
obstacleGrid[i,j] = obstacleGrid[i,j - 1]
else
obstacleGrid[i,j] = 0
You can do a similar processing for finding out the number of ways of reaching the cells in the first column. For any other cell, we can find out the number of ways of reaching it, by making use of the number of ways of reaching the cell directly above it and the cell to the left of it in the grid. This is because these are the only two directions from which the robot can come to the current cell. Since we are making use of pre-computed values along the iteration, this becomes a dynamic programming problem.
if obstacleGrid[i][j] is not an obstacle
obstacleGrid[i,j] = obstacleGrid[i,j - 1] + obstacleGrid[i - 1][j]
else
obstacleGrid[i,j] = 0 | 62,1022 |
64 | Minimum Path Sum | minimum-path-sum | Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right, which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. | Array,Dynamic Programming,Matrix | Medium | 59.3 | 1,169,975 | 693,295 | 7,136 | 97 | null | 62,174,741,2067,2192 |
65 | Valid Number | valid-number | A valid number can be split up into these components (in order): A decimal number can be split up into these components (in order): An integer can be split up into these components (in order): For example, all the following are valid numbers: ["2", "0089", "-0.1", "+3.14", "4.", "-.9", "2e10", "-90E3", "3e+7", "+6e-1", "53.5e93", "-123.456e789"], while the following are not valid numbers: ["abc", "1a", "1e", "e3", "99e2.5", "--6", "-+3", "95a54e53"]. Given a string s, return true if s is a valid number. | String | Hard | 18.1 | 1,483,253 | 268,485 | 555 | 994 | null | 8 |
66 | Plus One | plus-one | You are given a large integer represented as an integer array digits, where each digits[i] is the ith digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading 0's. Increment the large integer by one and return the resulting array of digits. | Array,Math | Easy | 42.8 | 2,719,690 | 1,162,902 | 4,014 | 4,072 | null | 43,67,369,1031 |
67 | Add Binary | add-binary | Given two binary strings a and b, return their sum as a binary string. | Math,String,Bit Manipulation,Simulation | Easy | 50.4 | 1,625,914 | 819,020 | 4,851 | 533 | null | 2,43,66,1031 |
68 | Text Justification | text-justification | Given an array of strings words and a width maxWidth, format the text such that each line has exactly maxWidth characters and is fully (left and right) justified. You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces ' ' when necessary so that each line has exactly maxWidth characters. Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line does not divide evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right. For the last line of text, it should be left-justified and no extra space is inserted between words. Note: | Array,String,Simulation | Hard | 35 | 689,234 | 241,053 | 1,722 | 2,843 | null | 1714,2260 |
69 | Sqrt(x) | sqrtx | Given a non-negative integer x, compute and return the square root of x. Since the return type is an integer, the decimal digits are truncated, and only the integer part of the result is returned. Note: You are not allowed to use any built-in exponent function or operator, such as pow(x, 0.5) or x ** 0.5. | Math,Binary Search | Easy | 36.4 | 2,776,055 | 1,011,489 | 3,661 | 3,137 | Try exploring all integers. (Credits: @annujoshi) Use the sorted property of integers to reduced the search space. (Credits: @annujoshi) | 50,367 |
70 | Climbing Stairs | climbing-stairs | You are climbing a staircase. It takes n steps to reach the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? | Math,Dynamic Programming,Memoization | Easy | 51 | 2,952,667 | 1,504,735 | 11,310 | 350 | To reach nth step, what could have been your previous steps? (Think about the step sizes) | 747,1013,1236 |
71 | Simplify Path | simplify-path | Given a string path, which is an absolute path (starting with a slash '/') to a file or directory in a Unix-style file system, convert it to the simplified canonical path. In a Unix-style file system, a period '.' refers to the current directory, a double period '..' refers to the directory up a level, and any multiple consecutive slashes (i.e. '//') are treated as a single slash '/'. For this problem, any other format of periods such as '...' are treated as file/directory names. The canonical path should have the following format: Return the simplified canonical path. | String,Stack | Medium | 38.8 | 1,081,568 | 419,168 | 2,314 | 465 | null | null |
72 | Edit Distance | edit-distance | Given two strings word1 and word2, return the minimum number of operations required to convert word1 to word2. You have the following three operations permitted on a word: | String,Dynamic Programming | Hard | 50.7 | 909,629 | 460,956 | 8,166 | 92 | null | 161,583,712,1105,2311 |
73 | Set Matrix Zeroes | set-matrix-zeroes | Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to 0's. You must do it in place. | Array,Hash Table,Matrix | Medium | 48.2 | 1,379,982 | 664,738 | 6,658 | 481 | If any cell of the matrix has a zero we can record its row and column number using additional memory.
But if you don't want to use extra memory then you can manipulate the array instead. i.e. simulating exactly what the question says. Setting cell values to zero on the fly while iterating might lead to discrepancies. What if you use some other integer value as your marker?
There is still a better approach for this problem with 0(1) space. We could have used 2 sets to keep a record of rows/columns which need to be set to zero. But for an O(1) space solution, you can use one of the rows and and one of the columns to keep track of this information. We can use the first cell of every row and column as a flag. This flag would determine whether a row or column has been set to zero. | 289,2244,2259,2314 |
74 | Search a 2D Matrix | search-a-2d-matrix | Write an efficient algorithm that searches for a value target in an m x n integer matrix matrix. This matrix has the following properties: | Array,Binary Search,Matrix | Medium | 44.2 | 1,713,958 | 757,291 | 7,220 | 263 | null | 240 |
75 | Sort Colors | sort-colors | 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. | Array,Two Pointers,Sorting | Medium | 54.6 | 1,791,336 | 978,245 | 9,434 | 408 | A rather straight forward solution is a two-pass algorithm using counting sort. Iterate the array counting number of 0's, 1's, and 2's. Overwrite array with the total number of 0's, then 1's and followed by 2's. | 148,280,324 |
76 | Minimum Window Substring | minimum-window-substring | Given two strings s and t of lengths m and n respectively, return the minimum window substring of s such that every character in t (including duplicates) is included in the window. If there is no such substring, return the empty string "". The testcases will be generated such that the answer is unique. A substring is a contiguous sequence of characters within the string. | Hash Table,String,Sliding Window | Hard | 38.9 | 1,890,669 | 735,482 | 9,964 | 529 | Use two pointers to create a window of letters in S, which would have all the characters from T. Since you have to find the minimum window in S which has all the characters from T, you need to expand and contract the window using the two pointers and keep checking the window for all the characters. This approach is also called Sliding Window Approach.
L ------------------------ R , Suppose this is the window that contains all characters of T
L----------------- R , this is the contracted window. We found a smaller window that still contains all the characters in T
When the window is no longer valid, start expanding again using the right pointer. | 30,209,239,567,632,727 |
77 | Combinations | combinations | Given two integers n and k, return all possible combinations of k numbers out of the range [1, n]. You may return the answer in any order. | Backtracking | Medium | 63.7 | 799,662 | 509,584 | 4,032 | 136 | null | 39,46 |
78 | Subsets | subsets | Given an integer array nums of unique elements, return all possible subsets (the power set). The solution set must not contain duplicate subsets. Return the solution in any order. | Array,Backtracking,Bit Manipulation | Medium | 71.2 | 1,493,029 | 1,063,773 | 9,680 | 153 | null | 90,320,800,2109,2170 |
79 | Word Search | word-search | Given an m x n grid of characters board and a string word, return true if word exists in the grid. The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once. | Array,Backtracking,Matrix | Medium | 39.6 | 2,376,458 | 940,859 | 8,999 | 344 | null | 212 |
80 | Remove Duplicates from Sorted Array II | remove-duplicates-from-sorted-array-ii | Given an integer array nums sorted in non-decreasing order, remove some duplicates in-place such that each unique element appears at most twice. The relative order of the elements should be kept the same. Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array nums. More formally, if there are k elements after removing the duplicates, then the first k elements of nums should hold the final result. It does not matter what you leave beyond the first k elements. Return k after placing the final result in the first k slots of nums. Do not allocate extra space for another array. You must do this by modifying the input array in-place with O(1) extra memory. Custom Judge: The judge will test your solution with the following code: If all assertions pass, then your solution will be accepted. | Array,Two Pointers | Medium | 50.5 | 846,310 | 427,094 | 3,555 | 919 | null | 26 |
81 | Search in Rotated Sorted Array II | search-in-rotated-sorted-array-ii | There is an integer array nums sorted in non-decreasing order (not necessarily with distinct values). Before being passed to your function, nums is rotated at an unknown pivot index k (0 <= k < nums.length) such that the resulting array is [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] (0-indexed). For example, [0,1,2,4,4,4,5,6,6,7] might be rotated at pivot index 5 and become [4,5,6,6,7,0,1,2,4,4]. Given the array nums after the rotation and an integer target, return true if target is in nums, or false if it is not in nums. You must decrease the overall operation steps as much as possible. | Array,Binary Search | Medium | 35.5 | 1,179,147 | 419,141 | 4,331 | 718 | null | 33 |
82 | Remove Duplicates from Sorted List II | remove-duplicates-from-sorted-list-ii | Given the head of a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. Return the linked list sorted as well. | Linked List,Two Pointers | Medium | 44.2 | 1,085,754 | 480,084 | 5,610 | 164 | null | 83,1982 |
83 | Remove Duplicates from Sorted List | remove-duplicates-from-sorted-list | Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well. | Linked List | Easy | 49 | 1,714,235 | 839,133 | 4,634 | 183 | null | 82,1982 |
84 | Largest Rectangle in Histogram | largest-rectangle-in-histogram | Given an array of integers heights representing the histogram's bar height where the width of each bar is 1, return the area of the largest rectangle in the histogram. | Array,Stack,Monotonic Stack | Hard | 40.7 | 1,220,391 | 496,774 | 9,616 | 142 | null | 85,1918 |
85 | Maximal Rectangle | maximal-rectangle | Given a rows x cols binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area. | Array,Dynamic Programming,Stack,Matrix,Monotonic Stack | Hard | 42.7 | 682,660 | 291,357 | 6,471 | 106 | null | 84,221 |
86 | Partition List | partition-list | Given the head of a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in each of the two partitions. | Linked List,Two Pointers | Medium | 47.8 | 703,320 | 335,965 | 3,376 | 474 | null | 2265 |
87 | Scramble String | scramble-string | We can scramble a string s to get a string t using the following algorithm: Given two strings s1 and s2 of the same length, return true if s2 is a scrambled string of s1, otherwise, return false. | String,Dynamic Programming | Hard | 35.6 | 402,343 | 143,122 | 1,406 | 928 | null | null |
88 | Merge Sorted Array | merge-sorted-array | You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n, representing the number of elements in nums1 and nums2 respectively. Merge nums1 and nums2 into a single array sorted in non-decreasing order. The final sorted array should not be returned by the function, but instead be stored inside the array nums1. To accommodate this, nums1 has a length of m + n, where the first m elements denote the elements that should be merged, and the last n elements are set to 0 and should be ignored. nums2 has a length of n. | Array,Two Pointers,Sorting | Easy | 43.7 | 3,069,815 | 1,342,555 | 4,189 | 401 | You can easily solve this problem if you simply think about two elements at a time rather than two arrays. We know that each of the individual arrays is sorted. What we don't know is how they will intertwine. Can we take a local decision and arrive at an optimal solution? If you simply consider one element each at a time from the two arrays and make a decision and proceed accordingly, you will arrive at the optimal solution. | 21,1019,1028 |
89 | Gray Code | gray-code | An n-bit gray code sequence is a sequence of 2n integers where: Given an integer n, return any valid n-bit gray code sequence. | Math,Backtracking,Bit Manipulation | Medium | 55.2 | 418,722 | 231,054 | 1,421 | 2,248 | null | 717 |
90 | Subsets II | subsets-ii | Given an integer array nums that may contain duplicates, return all possible subsets (the power set). The solution set must not contain duplicate subsets. Return the solution in any order. | Array,Backtracking,Bit Manipulation | Medium | 53.1 | 924,788 | 491,045 | 4,830 | 149 | null | 78,2109 |
91 | Decode Ways | decode-ways | A message containing letters from A-Z can be encoded into numbers using the following mapping: To decode an encoded message, all the digits must be grouped then mapped back into letters using the reverse of the mapping above (there may be multiple ways). For example, "11106" can be mapped into: Note that the grouping (1 11 06) is invalid because "06" cannot be mapped into 'F' since "6" is different from "06". Given a string s containing only digits, return the number of ways to decode it. The test cases are generated so that the answer fits in a 32-bit integer. | String,Dynamic Programming | Medium | 30.1 | 2,536,389 | 764,501 | 6,691 | 3,792 | null | 639,2091 |
92 | Reverse Linked List II | reverse-linked-list-ii | Given the head of a singly linked list and two integers left and right where left <= right, reverse the nodes of the list from position left to position right, and return the reversed list. | Linked List | Medium | 43.3 | 1,088,239 | 471,279 | 5,945 | 278 | null | 206 |
93 | Restore IP Addresses | restore-ip-addresses | A valid IP address consists of exactly four integers separated by single dots. Each integer is between 0 and 255 (inclusive) and cannot have leading zeros. Given a string s containing only digits, return all possible valid IP addresses that can be formed by inserting dots into s. You are not allowed to reorder or remove any digits in s. You may return the valid IP addresses in any order. | String,Backtracking | Medium | 41.7 | 705,550 | 294,170 | 2,649 | 624 | null | 752 |
94 | Binary Tree Inorder Traversal | binary-tree-inorder-traversal | Given the root of a binary tree, return the inorder traversal of its nodes' values. | Stack,Tree,Depth-First Search,Binary Tree | Easy | 70.6 | 2,018,990 | 1,425,499 | 7,405 | 335 | null | 98,144,145,173,230,272,285,758,799 |
95 | Unique Binary Search Trees II | unique-binary-search-trees-ii | Given an integer n, return all the structurally unique BST's (binary search trees), which has exactly n nodes of unique values from 1 to n. Return the answer in any order. | Dynamic Programming,Backtracking,Tree,Binary Search Tree,Binary Tree | Medium | 49.3 | 612,378 | 301,936 | 4,623 | 302 | null | 96,241 |
96 | Unique Binary Search Trees | unique-binary-search-trees | Given an integer n, return the number of structurally unique BST's (binary search trees) which has exactly n nodes of unique values from 1 to n. | Math,Dynamic Programming,Tree,Binary Search Tree,Binary Tree | Medium | 58.2 | 815,533 | 474,825 | 7,042 | 284 | null | 95 |
97 | Interleaving String | interleaving-string | Given strings s1, s2, and s3, find whether s3 is formed by an interleaving of s1 and s2. An interleaving of two strings s and t is a configuration where they are divided into non-empty substrings such that: Note: a + b is the concatenation of strings a and b. | String,Dynamic Programming | Medium | 34.7 | 748,031 | 259,515 | 3,784 | 202 | null | null |
98 | Validate Binary Search Tree | validate-binary-search-tree | Given the root of a binary tree, determine if it is a valid binary search tree (BST). A valid BST is defined as follows: | Tree,Depth-First Search,Binary Search Tree,Binary Tree | Medium | 30.5 | 4,483,144 | 1,365,470 | 9,434 | 874 | null | 94,501 |
99 | Recover Binary Search Tree | recover-binary-search-tree | You are given the root of a binary search tree (BST), where the values of exactly two nodes of the tree were swapped by mistake. Recover the tree without changing its structure. | Tree,Depth-First Search,Binary Search Tree,Binary Tree | Medium | 46.3 | 603,807 | 279,687 | 4,150 | 154 | null | null |
100 | Same Tree | same-tree | Given the roots of two binary trees p and q, write a function to check if they are the same or not. Two binary trees are considered the same if they are structurally identical, and the nodes have the same value. | Tree,Depth-First Search,Breadth-First Search,Binary Tree | Easy | 55.5 | 1,816,972 | 1,008,051 | 5,477 | 132 | null | null |