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
935
Orderly Queue
orderly-queue
You are given a string s and an integer k. You can choose one of the first k letters of s and append it at the end of the string.. Return the lexicographically smallest string you could have after applying the mentioned step any number of moves.
Math,String,Sorting
Hard
58.5
43,905
25,691
556
354
null
null
936
RLE Iterator
rle-iterator
We can use run-length encoding (i.e., RLE) to encode a sequence of integers. In a run-length encoded array of even length encoding (0-indexed), for all even i, encoding[i] tells us the number of times that the non-negative integer value encoding[i + 1] is repeated in the sequence. Given a run-length encoded array, design an iterator that iterates through it. Implement the RLEIterator class:
Array,Design,Counting,Iterator
Medium
58.7
86,633
50,855
520
144
null
null
937
Online Stock Span
online-stock-span
Design an algorithm that collects daily price quotes for some stock and returns the span of that stock's price for the current day. The span of the stock's price today is defined as the maximum number of consecutive days (starting from today and going backward) for which the stock price was less than or equal to today's price. Implement the StockSpanner class:
Stack,Design,Monotonic Stack,Data Stream
Medium
63
194,074
122,322
2,574
204
null
739
938
Numbers At Most N Given Digit Set
numbers-at-most-n-given-digit-set
Given an array of digits which is sorted in non-decreasing order. You can write numbers using each digits[i] as many times as we want. For example, if digits = ['1','3','5'], we may write numbers such as '13', '551', and '1351315'. Return the number of positive integers that can be generated that are less than or equal to a given integer n.
Array,Math,Binary Search,Dynamic Programming
Hard
40.9
86,168
35,238
1,041
88
null
null
939
Valid Permutations for DI Sequence
valid-permutations-for-di-sequence
You are given a string s of length n where s[i] is either: A permutation perm of n + 1 integers of all the integers in the range [0, n] is called a valid permutation if for all valid i: Return the number of valid permutations perm. Since the answer may be large, return it modulo 109 + 7.
Dynamic Programming
Hard
57
18,627
10,617
448
35
null
null
940
Fruit Into Baskets
fruit-into-baskets
You are visiting a farm that has a single row of fruit trees arranged from left to right. The trees are represented by an integer array fruits where fruits[i] is the type of fruit the ith tree produces. You want to collect as much fruit as possible. However, the owner has some strict rules that you must follow: Given the integer array fruits, return the maximum number of fruits you can pick.
Array,Hash Table,Sliding Window
Medium
42.8
461,033
197,476
941
72
null
null
941
Sort Array By Parity
sort-array-by-parity
Given an integer array nums, move all the even integers at the beginning of the array followed by all the odd integers. Return any array that satisfies this condition.
Array,Two Pointers,Sorting
Easy
74.8
580,427
434,203
2,633
111
null
2283,2327
942
Super Palindromes
super-palindromes
Let's say a positive integer is a super-palindrome if it is a palindrome, and it is also the square of a palindrome. Given two positive integers left and right represented as strings, return the number of super-palindromes integers in the inclusive range [left, right].
Math,Enumeration
Hard
39.4
54,331
21,385
301
377
null
null
943
Sum of Subarray Minimums
sum-of-subarray-minimums
Given an array of integers arr, find the sum of min(b), where b ranges over every (contiguous) subarray of arr. Since the answer may be large, return the answer modulo 109 + 7.
Array,Dynamic Programming,Stack,Monotonic Stack
Medium
33.8
216,848
73,305
3,363
225
null
2227
944
Smallest Range I
smallest-range-i
You are given an integer array nums and an integer k. In one operation, you can choose any index i where 0 <= i < nums.length and change nums[i] to nums[i] + x where x is an integer from the range [-k, k]. You can apply this operation at most once for each index i. The score of nums is the difference between the maximum and minimum elements in nums. Return the minimum score of nums after applying the mentioned operation at most once for each index in it.
Array,Math
Easy
67.2
88,513
59,504
422
1,601
null
null
945
Snakes and Ladders
snakes-and-ladders
You are given an n x n integer matrix board where the cells are labeled from 1 to n2 in a Boustrophedon style starting from the bottom left of the board (i.e. board[n - 1][0]) and alternating direction each row. You start on square 1 of the board. In each move, starting from square curr, do the following: A board square on row r and column c has a snake or ladder if board[r][c] != -1. The destination of that snake or ladder is board[r][c]. Squares 1 and n2 do not have a snake or ladder. Note that you only take a snake or ladder at most once per move. If the destination to a snake or ladder is the start of another snake or ladder, you do not follow the subsequent snake or ladder. Return the least number of moves required to reach the square n2. If it is not possible to reach the square, return -1.
Array,Breadth-First Search,Matrix
Medium
40.5
179,920
72,801
620
158
null
null
946
Smallest Range II
smallest-range-ii
You are given an integer array nums and an integer k. For each index i where 0 <= i < nums.length, change nums[i] to be either nums[i] + k or nums[i] - k. The score of nums is the difference between the maximum and minimum elements in nums. Return the minimum score of nums after changing the values at each index.
Array,Math,Greedy,Sorting
Medium
32.7
100,920
32,973
1,067
351
null
null
947
Online Election
online-election
You are given two integer arrays persons and times. In an election, the ith vote was cast for persons[i] at time times[i]. For each query at a time t, find the person that was leading the election at time t. Votes cast at time t will count towards our query. In the case of a tie, the most recent vote (among tied candidates) wins. Implement the TopVotedCandidate class:
Array,Hash Table,Binary Search,Design
Medium
51.5
75,347
38,816
673
503
null
1483
948
Sort an Array
sort-an-array
Given an array of integers nums, sort the array in ascending order.
Array,Divide and Conquer,Sorting,Heap (Priority Queue),Merge Sort,Bucket Sort,Radix Sort,Counting Sort
Medium
61.5
416,971
256,327
2,012
517
null
null
949
Cat and Mouse
cat-and-mouse
A game on an undirected graph is played by two players, Mouse and Cat, who alternate turns. The graph is given as follows: graph[a] is a list of all nodes b such that ab is an edge of the graph. The mouse starts at node 1 and goes first, the cat starts at node 2 and goes second, and there is a hole at node 0. During each player's turn, they must travel along one edge of the graph that meets where they are.  For example, if the Mouse is at node 1, it must travel to any node in graph[1]. Additionally, it is not allowed for the Cat to travel to the Hole (node 0.) Then, the game can end in three ways: Given a graph, and assuming both players play optimally, return
Math,Dynamic Programming,Breadth-First Search,Graph,Memoization,Game Theory
Hard
35.8
41,135
14,711
620
110
null
1727
950
X of a Kind in a Deck of Cards
x-of-a-kind-in-a-deck-of-cards
In a deck of cards, each card has an integer written on it. Return true if and only if you can choose X >= 2 such that it is possible to split the entire deck into 1 or more groups of cards, where:
Array,Hash Table,Math,Counting,Number Theory
Easy
33
256,502
84,668
1,235
302
null
null
951
Partition Array into Disjoint Intervals
partition-array-into-disjoint-intervals
Given an integer array nums, partition it into two (contiguous) subarrays left and right so that: Return the length of left after such a partitioning. Test cases are generated such that partitioning exists.
Array
Medium
48.4
135,680
65,661
1,157
59
null
2138
952
Word Subsets
word-subsets
You are given two string arrays words1 and words2. A string b is a subset of string a if every letter in b occurs in a including multiplicity. A string a from words1 is universal if for every string b in words2, b is a subset of a. Return an array of all the universal strings in words1. You may return the answer in any order.
Array,Hash Table,String
Medium
52.7
94,448
49,763
948
126
null
null
953
Reverse Only Letters
reverse-only-letters
Given a string s, reverse the string according to the following rules: Return s after reversing it.
Two Pointers,String
Easy
61
216,213
131,906
1,416
52
This problem is exactly like reversing a normal string except that there are certain characters that we have to simply skip. That should be easy enough to do if you know how to reverse a string using the two-pointer approach.
null
954
Maximum Sum Circular Subarray
maximum-sum-circular-subarray
Given a circular integer array nums of length n, return the maximum possible sum of a non-empty subarray of nums. A circular array means the end of the array connects to the beginning of the array. Formally, the next element of nums[i] is nums[(i + 1) % n] and the previous element of nums[i] is nums[(i - 1 + n) % n]. A subarray may only include each element of the fixed buffer nums at most once. Formally, for a subarray nums[i], nums[i + 1], ..., nums[j], there does not exist i <= k1, k2 <= j with k1 % n == k2 % n.
Array,Divide and Conquer,Dynamic Programming,Queue,Monotonic Queue
Medium
37.1
306,473
113,682
3,034
139
For those of you who are familiar with the Kadane's algorithm, think in terms of that. For the newbies, Kadane's algorithm is used to finding the maximum sum subarray from a given array. This problem is a twist on that idea and it is advisable to read up on that algorithm first before starting this problem. Unless you already have a great algorithm brewing up in your mind in which case, go right ahead! What is an alternate way of representing a circular array so that it appears to be a straight array? Essentially, there are two cases of this problem that we need to take care of. Let's look at the figure below to understand those two cases: The first case can be handled by the good old Kadane's algorithm. However, is there a smarter way of going about handling the second case as well?
null
955
Complete Binary Tree Inserter
complete-binary-tree-inserter
A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible. Design an algorithm to insert a new node to a complete binary tree keeping it complete after the insertion. Implement the CBTInserter class:
Tree,Breadth-First Search,Design,Binary Tree
Medium
64
59,755
38,226
752
82
null
null
956
Number of Music Playlists
number-of-music-playlists
Your music player contains n different songs. You want to listen to goal songs (not necessarily different) during your trip. To avoid boredom, you will create a playlist so that: Given n, goal, and k, return the number of possible playlists that you can create. Since the answer can be very large, return it modulo 109 + 7.
Math,Dynamic Programming,Combinatorics
Hard
49.7
36,753
18,264
714
68
null
null
957
Minimum Add to Make Parentheses Valid
minimum-add-to-make-parentheses-valid
A parentheses string is valid if and only if: You are given a parentheses string s. In one move, you can insert a parenthesis at any position of the string. Return the minimum number of moves required to make s valid.
String,Stack,Greedy
Medium
77.2
261,675
201,912
2,452
141
null
2095
958
Sort Array By Parity II
sort-array-by-parity-ii
Given an array of integers nums, half of the integers in nums are odd, and the other half are even. Sort the array so that whenever nums[i] is odd, i is odd, and whenever nums[i] is even, i is even. Return any answer array that satisfies this condition.
Array,Two Pointers,Sorting
Easy
70.6
257,460
181,873
1,705
70
null
2271,2283,2327
959
3Sum With Multiplicity
3sum-with-multiplicity
Given an integer array arr, and an integer target, return the number of tuples i, j, k such that i < j < k and arr[i] + arr[j] + arr[k] == target. As the answer can be very large, return it modulo 109 + 7.
Array,Hash Table,Two Pointers,Sorting,Counting
Medium
45.4
188,183
85,475
2,186
265
null
null
960
Minimize Malware Spread
minimize-malware-spread
You are given a network of n nodes represented as an n x n adjacency matrix graph, where the ith node is directly connected to the jth node if graph[i][j] == 1. Some nodes initial are initially infected by malware. Whenever two nodes are directly connected, and at least one of those two nodes is infected by malware, both nodes will be infected by malware. This spread of malware will continue until no more nodes can be infected in this manner. Suppose M(initial) is the final number of nodes infected with malware in the entire network after the spread of malware stops. We will remove exactly one node from initial. Return the node that, if removed, would minimize M(initial). If multiple nodes could be removed to minimize M(initial), return such a node with the smallest index. Note that if a node was removed from the initial list of infected nodes, it might still be infected later due to the malware spread.
Array,Depth-First Search,Breadth-First Search,Union Find,Matrix
Hard
41.9
87,853
36,826
607
407
null
null
961
Long Pressed Name
long-pressed-name
Your friend is typing his name into a keyboard. Sometimes, when typing a character c, the key might get long pressed, and the character will be typed 1 or more times. You examine the typed characters of the keyboard. Return True if it is possible that it was your friends name, with some characters (possibly none) being long pressed.
Two Pointers,String
Easy
34.7
264,403
91,843
1,579
240
null
null
962
Flip String to Monotone Increasing
flip-string-to-monotone-increasing
A binary string is monotone increasing if it consists of some number of 0's (possibly none), followed by some number of 1's (also possibly none). You are given a binary string s. You can flip s[i] changing it from 0 to 1 or from 1 to 0. Return the minimum number of flips to make s monotone increasing.
String,Dynamic Programming
Medium
58.7
150,284
88,180
1,907
82
null
null
963
Three Equal Parts
three-equal-parts
You are given an array arr which consists of only zeros and ones, divide the array into three non-empty parts such that all of these parts represent the same binary value. If it is possible, return any [i, j] with i + 1 < j, such that: If it is not possible, return [-1, -1]. Note that the entire part is used when considering what binary value it represents. For example, [1,1,0] represents 6 in decimal, not 3. Also, leading zeros are allowed, so [0,1,1] and [1,1] represent the same value.
Array,Math
Hard
39.4
65,317
25,719
676
103
null
null
964
Minimize Malware Spread II
minimize-malware-spread-ii
You are given a network of n nodes represented as an n x n adjacency matrix graph, where the ith node is directly connected to the jth node if graph[i][j] == 1. Some nodes initial are initially infected by malware. Whenever two nodes are directly connected, and at least one of those two nodes is infected by malware, both nodes will be infected by malware. This spread of malware will continue until no more nodes can be infected in this manner. Suppose M(initial) is the final number of nodes infected with malware in the entire network after the spread of malware stops. We will remove exactly one node from initial, completely removing it and any connections from this node to any other node. Return the node that, if removed, would minimize M(initial). If multiple nodes could be removed to minimize M(initial), return such a node with the smallest index.
Array,Depth-First Search,Breadth-First Search,Union Find,Matrix
Hard
42.2
36,043
15,199
417
69
null
null
965
Unique Email Addresses
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 '+'. If you add periods '.' between some characters in the local name part of an email address, mail sent there will be forwarded to the same address without dots in the local name. Note that this rule does not apply to domain names. If you add a plus '+' in the local name, everything after the first plus sign will be ignored. This allows certain emails to be filtered. Note that this rule does not apply to domain names. It is possible to use both of these rules at the same time. Given an array of strings emails where we send one email to each emails[i], return the number of different addresses that actually receive mails.
Array,Hash Table,String
Easy
67.4
518,797
349,584
1,749
244
null
null
966
Binary Subarrays With Sum
binary-subarrays-with-sum
Given a binary array nums and an integer goal, return the number of non-empty subarrays with a sum goal. A subarray is a contiguous part of the array.
Array,Hash Table,Sliding Window,Prefix Sum
Medium
48.7
92,313
44,969
1,303
44
null
null
967
Minimum Falling Path Sum
minimum-falling-path-sum
Given an n x n array of integers matrix, return the minimum sum of any falling path through matrix. A falling path starts at any element in the first row and chooses the element in the next row that is either directly below or diagonally left/right. Specifically, the next element from position (row, col) will be (row + 1, col - 1), (row + 1, col), or (row + 1, col + 1).
Array,Dynamic Programming,Matrix
Medium
67.3
180,018
121,207
2,215
92
null
1224
968
Beautiful Array
beautiful-array
An array nums of length n is beautiful if: Given the integer n, return any beautiful array nums of length n. There will be at least one valid answer for the given n.
Array,Math,Divide and Conquer
Medium
64.7
55,162
35,682
804
1,201
null
null
969
Number of Recent Calls
number-of-recent-calls
You have a RecentCounter class which counts the number of recent requests within a certain time frame. Implement the RecentCounter class: It is guaranteed that every call to ping uses a strictly larger value of t than the previous call.
Design,Queue,Data Stream
Easy
73.1
164,543
120,276
734
2,429
null
null
971
Shortest Bridge
shortest-bridge
You are given an n x n binary matrix grid where 1 represents land and 0 represents water. An island is a 4-directionally connected group of 1's not connected to any other 1's. There are exactly two islands in grid. You may change 0's to 1's to connect the two islands to form one island. Return the smallest number of 0's you must flip to connect the two islands.
Array,Depth-First Search,Breadth-First Search,Matrix
Medium
52.7
169,542
89,265
2,481
127
null
null
972
Knight Dialer
knight-dialer
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.
Dynamic Programming
Medium
49
156,943
76,849
1,421
347
null
null
973
Stamping The Sequence
stamping-the-sequence
You are given two strings stamp and target. Initially, there is a string s of length target.length with all s[i] == '?'. In one turn, you can place stamp over s and replace every letter in the s with the corresponding letter from stamp. We want to convert s to target using at most 10 * target.length turns. Return an array of the index of the left-most letter being stamped at each turn. If we cannot obtain target from s within 10 * target.length turns, return an empty array.
String,Stack,Greedy,Queue
Hard
53.6
44,732
23,964
531
125
null
null
974
Reorder Data in Log Files
reorder-data-in-log-files
You are given an array of logs. Each log is a space-delimited string of words, where the first word is the identifier. There are two types of logs: Reorder these logs so that: Return the final order of the logs.
Array,String,Sorting
Easy
56
521,107
291,671
1,638
3,857
null
null
975
Range Sum of BST
range-sum-of-bst
Given the root node of a binary search tree and two integers low and high, return the sum of values of all nodes with a value in the inclusive range [low, high].
Tree,Depth-First Search,Binary Search Tree,Binary Tree
Easy
85.1
705,825
600,407
4,038
332
null
null
976
Minimum Area Rectangle
minimum-area-rectangle
You are given an array of points in the X-Y plane points where points[i] = [xi, yi]. Return the minimum area of a rectangle formed from these points, with sides parallel to the X and Y axes. If there is not any such rectangle, return 0.
Array,Hash Table,Math,Geometry,Sorting
Medium
53.5
189,171
101,237
1,426
223
null
null
977
Distinct Subsequences II
distinct-subsequences-ii
Given a string s, return the number of distinct non-empty subsequences of s. Since the answer may be very large, return it modulo 109 + 7.
String,Dynamic Programming
Hard
44.2
60,215
26,643
1,076
27
null
2115
978
Valid Mountain Array
valid-mountain-array
Given an array of integers arr, return true if and only if it is a valid mountain array. Recall that arr is a mountain array if and only if:
Array
Easy
33.8
850,393
287,136
2,100
144
It's very easy to keep track of a monotonically increasing or decreasing ordering of elements. You just need to be able to determine the start of the valley in the mountain and from that point onwards, it should be a valley i.e. no mini-hills after that. Use this information in regards to the values in the array and you will be able to come up with a straightforward solution.
1766
979
DI String Match
di-string-match
A permutation perm of n + 1 integers of all the integers in the range [0, n] can be represented as a string s of length n where: Given a string s, reconstruct the permutation perm and return it. If there are multiple valid permutations perm, return any of them.
Array,Math,Two Pointers,String,Greedy
Easy
75.8
138,777
105,184
1,646
656
null
null
980
Find the Shortest Superstring
find-the-shortest-superstring
Given an array of strings words, return the smallest string that contains each string in words as a substring. If there are multiple valid strings of the smallest length, return any of them. You may assume that no string in words is a substring of another string in words.
Array,String,Dynamic Programming,Bit Manipulation,Bitmask
Hard
45.4
51,494
23,384
1,002
126
null
null
981
Delete Columns to Make Sorted
delete-columns-to-make-sorted
You are given an array of n strings strs, all of the same length. The strings can be arranged such that there is one on each line, making a grid. For example, strs = ["abc", "bce", "cae"] can be arranged as: You want to delete the columns that are not sorted lexicographically. In the above example (0-indexed), columns 0 ('a', 'b', 'c') and 2 ('c', 'e', 'e') are sorted while column 1 ('b', 'c', 'a') is not, so you would delete column 1. Return the number of columns that you will delete.
Array,String
Easy
69.9
94,150
65,827
348
2,003
null
null
982
Minimum Increment to Make Array Unique
minimum-increment-to-make-array-unique
You are given an integer array nums. In one move, you can pick an index i where 0 <= i < nums.length and increment nums[i] by 1. Return the minimum number of moves to make every value in nums unique.
Array,Greedy,Sorting,Counting
Medium
49
106,854
52,368
1,026
43
null
2329
983
Validate Stack Sequences
validate-stack-sequences
Given two integer arrays pushed and popped each with distinct values, return true if this could have been the result of a sequence of push and pop operations on an initially empty stack, or false otherwise.
Array,Stack,Simulation
Medium
67.6
259,252
175,192
3,509
61
null
null
984
Most Stones Removed with Same Row or Column
most-stones-removed-with-same-row-or-column
On a 2D plane, we place n stones at some integer coordinate points. Each coordinate point may have at most one stone. A stone can be removed if it shares either the same row or the same column as another stone that has not been removed. Given an array stones of length n where stones[i] = [xi, yi] represents the location of the ith stone, return the largest possible number of stones that can be removed.
Depth-First Search,Union Find,Graph
Medium
56.3
177,705
100,011
2,367
504
null
null
985
Bag of Tokens
bag-of-tokens
You have an initial power of power, an initial score of 0, and a bag of tokens where tokens[i] is the value of the ith token (0-indexed). Your goal is to maximize your total score by potentially playing each token in one of two ways: Each token may be played at most once and in any order. You do not have to play all the tokens. Return the largest possible score you can achieve after playing any number of tokens.
Array,Two Pointers,Greedy,Sorting
Medium
46.2
84,173
38,926
673
293
null
null
986
Largest Time for Given Digits
largest-time-for-given-digits
Given an array arr of 4 digits, find the latest 24-hour time that can be made using each digit exactly once. 24-hour times are formatted as "HH:MM", where HH is between 00 and 23, and MM is between 00 and 59. The earliest 24-hour time is 00:00, and the latest is 23:59. Return the latest 24-hour time in "HH:MM" format. If no valid time can be made, return an empty string.
String,Enumeration
Medium
35.4
198,990
70,517
544
901
null
null
987
Reveal Cards In Increasing Order
reveal-cards-in-increasing-order
You are given an integer array deck. There is a deck of cards where every card has a unique integer. The integer on the ith card is deck[i]. You can order the deck in any order you want. Initially, all the cards start face down (unrevealed) in one deck. You will do the following steps repeatedly until all cards are revealed: Return an ordering of the deck that would reveal the cards in increasing order. Note that the first entry in the answer is considered to be the top of the deck.
Array,Queue,Sorting,Simulation
Medium
77.1
73,619
56,728
1,804
277
null
null
988
Flip Equivalent Binary Trees
flip-equivalent-binary-trees
For a binary tree T, we can define a flip operation as follows: choose any node, and swap the left and right child subtrees. A binary tree X is flip equivalent to a binary tree Y if and only if we can make X equal to Y after some number of flip operations. Given the roots of two binary trees root1 and root2, return true if the two trees are flip equivalent or false otherwise.
Tree,Depth-First Search,Binary Tree
Medium
66.7
161,831
107,916
1,594
70
null
null
989
Largest Component Size by Common Factor
largest-component-size-by-common-factor
You are given an integer array of unique positive integers nums. Consider the following graph: Return the size of the largest connected component in the graph.
Array,Math,Union Find
Hard
40.2
108,773
43,757
1,235
86
null
2276
990
Verifying an Alien Dictionary
verifying-an-alien-dictionary
In an alien language, surprisingly, they also use English lowercase letters, but possibly in a different order. The order of the alphabet is some permutation of lowercase letters. Given a sequence of words written in the alien language, and the order of the alphabet, return true if and only if the given words are sorted lexicographically in this alien language.
Array,Hash Table,String
Easy
52.5
655,595
344,275
2,739
916
null
null
991
Array of Doubled Pairs
array-of-doubled-pairs
Given an integer array of even length arr, return true if it is possible to reorder arr such that arr[2 * i + 1] = 2 * arr[2 * i] for every 0 <= i < len(arr) / 2, or false otherwise.
Array,Hash Table,Greedy,Sorting
Medium
38.7
181,540
70,184
1,075
127
null
2117
992
Delete Columns to Make Sorted II
delete-columns-to-make-sorted-ii
You are given an array of n strings strs, all of the same length. We may choose any deletion indices, and we delete all the characters in those indices for each string. For example, if we have strs = ["abcdef","uvwxyz"] and deletion indices {0, 2, 3}, then the final array after deletions is ["bef", "vyz"]. Suppose we chose a set of deletion indices answer such that after deletions, the final array has its elements in lexicographic order (i.e., strs[0] <= strs[1] <= strs[2] <= ... <= strs[n - 1]). Return the minimum possible value of answer.length.
Array,String,Greedy
Medium
34.2
44,573
15,248
443
69
null
null
993
Tallest Billboard
tallest-billboard
You are installing a billboard and want it to have the largest height. The billboard will have two steel supports, one on each side. Each steel support must be an equal height. You are given a collection of rods that can be welded together. For example, if you have rods of lengths 1, 2, and 3, you can weld them together to make a support of length 6. Return the largest possible height of your billboard installation. If you cannot support the billboard, return 0.
Array,Dynamic Programming
Hard
39.9
32,430
12,945
651
23
null
2162
994
Prison Cells After N Days
prison-cells-after-n-days
There are 8 prison cells in a row and each cell is either occupied or vacant. Each day, whether the cell is occupied or vacant changes according to the following rules: Note that because the prison is a row, the first and the last cells in the row can't have two adjacent neighbors. You are given an integer array cells where cells[i] == 1 if the ith cell is occupied and cells[i] == 0 if the ith cell is vacant, and you are given an integer n. Return the state of the prison after n days (i.e., n such changes described above).
Array,Hash Table,Math,Bit Manipulation
Medium
39.4
355,942
140,214
1,222
1,513
null
null
998
Check Completeness of a Binary Tree
check-completeness-of-a-binary-tree
Given the root of a binary tree, determine if it is a complete binary tree. In a complete binary tree, every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. It can have between 1 and 2h nodes inclusive at the last level h.
Tree,Breadth-First Search,Binary Tree
Medium
53.6
213,148
114,298
1,891
28
null
null
999
Regions Cut By Slashes
regions-cut-by-slashes
An n x n grid is composed of 1 x 1 squares where each 1 x 1 square consists of a '/', '\', or blank space ' '. These characters divide the square into contiguous regions. Given the grid grid represented as a string array, return the number of regions. Note that backslash characters are escaped, so a '\' is represented as '\\'.
Depth-First Search,Breadth-First Search,Union Find,Graph
Medium
68.6
52,702
36,142
2,035
397
null
null
1,000
Delete Columns to Make Sorted III
delete-columns-to-make-sorted-iii
You are given an array of n strings strs, all of the same length. We may choose any deletion indices, and we delete all the characters in those indices for each string. For example, if we have strs = ["abcdef","uvwxyz"] and deletion indices {0, 2, 3}, then the final array after deletions is ["bef", "vyz"]. Suppose we chose a set of deletion indices answer such that after deletions, the final array has every string (row) in lexicographic order. (i.e., (strs[0][0] <= strs[0][1] <= ... <= strs[0][strs[0].length - 1]), and (strs[1][0] <= strs[1][1] <= ... <= strs[1][strs[1].length - 1]), and so on). Return the minimum possible value of answer.length.
Array,String,Dynamic Programming
Hard
56.5
18,026
10,191
421
11
null
null
1,001
N-Repeated Element in Size 2N Array
n-repeated-element-in-size-2n-array
You are given an integer array nums with the following properties: Return the element that is repeated n times.
Array,Hash Table
Easy
75.4
232,107
175,078
878
294
null
null
1,002
Maximum Width Ramp
maximum-width-ramp
A ramp in an integer array nums is a pair (i, j) for which i < j and nums[i] <= nums[j]. The width of such a ramp is j - i. Given an integer array nums, return the maximum width of a ramp in nums. If there is no ramp in nums, return 0.
Array,Stack,Monotonic Stack
Medium
48.2
68,082
32,790
1,125
35
null
null
1,003
Minimum Area Rectangle II
minimum-area-rectangle-ii
You are given an array of points in the X-Y plane points where points[i] = [xi, yi]. Return the minimum area of any rectangle formed from these points, with sides not necessarily parallel to the X and Y axes. If there is not any such rectangle, return 0. Answers within 10-5 of the actual answer will be accepted.
Array,Math,Geometry
Medium
54.4
41,787
22,753
302
400
null
null
1,004
Least Operators to Express Number
least-operators-to-express-number
Given a single positive integer x, we will write an expression of the form x (op1) x (op2) x (op3) x ... where each operator op1, op2, etc. is either addition, subtraction, multiplication, or division (+, -, *, or /). For example, with x = 3, we might write 3 * 3 / 3 + 3 - 3 which is a value of 3. When writing such an expression, we adhere to the following conventions: We would like to write an expression with the least number of operators such that the expression equals the given target. Return the least number of operators used.
Math,Dynamic Programming
Hard
47.4
16,755
7,946
256
65
null
null
1,005
Univalued Binary Tree
univalued-binary-tree
A binary tree is uni-valued if every node in the tree has the same value. Given the root of a binary tree, return true if the given tree is uni-valued, or false otherwise.
Tree,Depth-First Search,Breadth-First Search,Binary Tree
Easy
68.9
227,655
156,880
1,259
56
null
1609
1,006
Vowel Spellchecker
vowel-spellchecker
Given a wordlist, we want to implement a spellchecker that converts a query word into a correct word. For a given query word, the spell checker handles two categories of spelling mistakes: In addition, the spell checker operates under the following precedence rules: Given some queries, return a list of words answer, where answer[i] is the correct word for query = queries[i].
Array,Hash Table,String
Medium
51.6
68,595
35,410
343
714
null
null
1,007
Numbers With Same Consecutive Differences
numbers-with-same-consecutive-differences
Return all non-negative integers of length n such that the absolute difference between every two consecutive digits is k. Note that every number in the answer must not have leading zeros. For example, 01 has one leading zero and is invalid. You may return the answer in any order.
Backtracking,Breadth-First Search
Medium
47.2
105,324
49,756
860
131
null
null
1,008
Binary Tree Cameras
binary-tree-cameras
You are given the root of a binary tree. We install cameras on the tree nodes where each camera at a node can monitor its parent, itself, and its immediate children. Return the minimum number of cameras needed to monitor all nodes of the tree.
Dynamic Programming,Tree,Depth-First Search,Binary Tree
Hard
41.8
151,382
63,331
2,594
33
null
1021
1,009
Pancake Sorting
pancake-sorting
Given an array of integers arr, sort the array by performing a series of pancake flips. In one pancake flip we do the following steps: For example, if arr = [3,2,1,4] and we performed a pancake flip choosing k = 3, we reverse the sub-array [3,2,1], so arr = [1,2,3,4] after the pancake flip at k = 3. Return an array of the k-values corresponding to a sequence of pancake flips that sort arr. Any valid answer that sorts the array within 10 * arr.length flips will be judged as correct.
Array,Two Pointers,Greedy,Sorting
Medium
69.7
104,899
73,101
1,065
1,226
null
null
1,010
Powerful Integers
powerful-integers
Given three integers x, y, and bound, return a list of all the powerful integers that have a value less than or equal to bound. An integer is powerful if it can be represented as xi + yj for some integers i >= 0 and j >= 0. You may return the answer in any order. In your answer, each value should occur at most once.
Hash Table,Math
Medium
43.6
105,922
46,133
242
62
null
null
1,011
Flip Binary Tree To Match Preorder Traversal
flip-binary-tree-to-match-preorder-traversal
You are given the root of a binary tree with n nodes, where each node is uniquely assigned a value from 1 to n. You are also given a sequence of n values voyage, which is the desired pre-order traversal of the binary tree. Any node in the binary tree can be flipped by swapping its left and right subtrees. For example, flipping node 1 will have the following effect: Flip the smallest number of nodes so that the pre-order traversal of the tree matches voyage. Return a list of the values of all flipped nodes. You may return the answer in any order. If it is impossible to flip the nodes in the tree to make the pre-order traversal match voyage, return the list [-1].
Tree,Depth-First Search,Binary Tree
Medium
49.9
69,777
34,793
705
239
null
null
1,012
Equal Rational Numbers
equal-rational-numbers
Given two strings s and t, each of which represents a non-negative rational number, return true if and only if they represent the same number. The strings may use parentheses to denote the repeating part of the rational number. A rational number can be represented using up to three parts: <IntegerPart>, <NonRepeatingPart>, and a <RepeatingPart>. The number will be represented in one of the following three ways: The repeating portion of a decimal expansion is conventionally denoted within a pair of round brackets. For example:
Math,String
Hard
42.5
12,568
5,339
67
184
null
null
1,013
Fibonacci Number
fibonacci-number
The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is, Given n, calculate F(n).
Math,Dynamic Programming,Recursion,Memoization
Easy
68.2
1,075,067
732,697
3,284
261
null
70,872,905,1236
1,014
K Closest Points to Origin
k-closest-points-to-origin
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). You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in).
Array,Math,Divide and Conquer,Geometry,Sorting,Heap (Priority Queue),Quickselect
Medium
66
1,115,681
736,146
5,350
205
null
215,347,692,1888
1,016
Subarray Sums Divisible by K
subarray-sums-divisible-by-k
Given an integer array nums and an integer k, return the number of non-empty subarrays that have a sum divisible by k. A subarray is a contiguous part of an array.
Array,Hash Table,Prefix Sum
Medium
53.2
174,162
92,620
2,709
133
null
560,1694
1,017
Odd Even Jump
odd-even-jump
You are given an integer array arr. From some starting index, you can make a series of jumps. The (1st, 3rd, 5th, ...) jumps in the series are called odd-numbered jumps, and the (2nd, 4th, 6th, ...) jumps in the series are called even-numbered jumps. Note that the jumps are numbered, not the indices. You may jump forward from index i to index j (with i < j) in the following way: A starting index is good if, starting from that index, you can reach the end of the array (index arr.length - 1) by jumping some number of times (possibly 0 or more than once). Return the number of good starting indices.
Array,Dynamic Programming,Stack,Monotonic Stack,Ordered Set
Hard
38.9
147,425
57,344
1,318
371
null
null
1,018
Largest Perimeter Triangle
largest-perimeter-triangle
Given an integer array nums, return the largest perimeter of a triangle with a non-zero area, formed from three of these lengths. If it is impossible to form any triangle of a non-zero area, return 0.
Array,Math,Greedy,Sorting
Easy
56
137,802
77,188
1,065
119
null
830
1,019
Squares of a Sorted Array
squares-of-a-sorted-array
Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order.
Array,Two Pointers,Sorting
Easy
71.6
1,324,165
948,265
5,114
150
null
88,360
1,020
Longest Turbulent Subarray
longest-turbulent-subarray
Given an integer array arr, return the length of a maximum size turbulent subarray of arr. A subarray is turbulent if the comparison sign flips between each adjacent pair of elements in the subarray. More formally, a subarray [arr[i], arr[i + 1], ..., arr[j]] of arr is said to be turbulent if and only if:
Array,Dynamic Programming,Sliding Window
Medium
47.5
149,840
71,194
1,264
166
null
53
1,021
Distribute Coins in Binary Tree
distribute-coins-in-binary-tree
You are given the root of a binary tree with n nodes where each node in the tree has node.val coins. There are n coins in total throughout the whole tree. In one move, we may choose two adjacent nodes and move one coin from one node to another. A move may be from parent to child, or from child to parent. Return the minimum number of moves required to make every node have exactly one coin.
Tree,Depth-First Search,Binary Tree
Medium
71.5
116,380
83,207
3,561
119
null
863,1008
1,022
Unique Paths III
unique-paths-iii
You are given an m x n integer array grid where grid[i][j] could be: Return the number of 4-directional walks from the starting square to the ending square, that walk over every non-obstacle square exactly once.
Array,Backtracking,Bit Manipulation,Matrix
Hard
79.4
142,480
113,130
2,750
132
null
37,63,212
1,023
Time Based Key-Value Store
time-based-key-value-store
Design a time-based key-value data structure that can store multiple values for the same key at different time stamps and retrieve the key's value at a certain timestamp. Implement the TimeMap class:
Hash Table,String,Binary Search,Design
Medium
52.8
331,082
174,658
2,024
207
null
2161
1,024
Triples with Bitwise AND Equal To Zero
triples-with-bitwise-and-equal-to-zero
Given an integer array nums, return the number of AND triples. An AND triple is a triple of indices (i, j, k) such that:
Array,Hash Table,Bit Manipulation
Hard
57.5
21,159
12,167
239
181
null
null
1,025
Minimum Cost For Tickets
minimum-cost-for-tickets
You have planned some train traveling one year in advance. The days of the year in which you will travel are given as an integer array days. Each day is an integer from 1 to 365. Train tickets are sold in three different ways: The passes allow that many days of consecutive travel. Return the minimum number of dollars you need to travel every day in the given list of days.
Array,Dynamic Programming
Medium
64
235,327
150,705
4,363
72
null
322
1,026
String Without AAA or BBB
string-without-aaa-or-bbb
Given two integers a and b, return any string s such that:
String,Greedy
Medium
42.2
78,678
33,185
486
330
null
null
1,027
Sum of Even Numbers After Queries
sum-of-even-numbers-after-queries
You are given an integer array nums and an array queries where queries[i] = [vali, indexi]. For each query i, first, apply nums[indexi] = nums[indexi] + vali, then print the sum of the even values of nums. Return an integer array answer where answer[i] is the answer to the ith query.
Array,Simulation
Medium
60.9
102,714
62,602
668
212
null
null
1,028
Interval List Intersections
interval-list-intersections
You are given two lists of closed intervals, firstList and secondList, where firstList[i] = [starti, endi] and secondList[j] = [startj, endj]. Each list of intervals is pairwise disjoint and in sorted order. Return the intersection of these two interval lists. A closed interval [a, b] (with a <= b) denotes the set of real numbers x with a <= x <= b. The intersection of two closed intervals is a set of real numbers that are either empty or represented as a closed interval. For example, the intersection of [1, 3] and [2, 4] is [2, 3].
Array,Two Pointers
Medium
71.1
413,025
293,594
4,101
84
null
56,88,761
1,029
Vertical Order Traversal of a Binary Tree
vertical-order-traversal-of-a-binary-tree
Given the root of a binary tree, calculate the vertical order traversal of the binary tree. For each node at position (row, col), its left and right children will be at positions (row + 1, col - 1) and (row + 1, col + 1) respectively. The root of the tree is at (0, 0). The vertical order traversal of a binary tree is a list of top-to-bottom orderings for each column index starting from the leftmost column and ending on the rightmost column. There may be multiple nodes in the same row and same column. In such a case, sort these nodes by their values. Return the vertical order traversal of the binary tree.
Hash Table,Tree,Depth-First Search,Breadth-First Search,Binary Tree
Hard
41.4
520,150
215,521
3,104
3,293
null
null
1,030
Smallest String Starting From Leaf
smallest-string-starting-from-leaf
You are given the root of a binary tree where each node has a value in the range [0, 25] representing the letters 'a' to 'z'. Return the lexicographically smallest string that starts at a leaf of this tree and ends at the root. As a reminder, any shorter prefix of a string is lexicographically smaller. A leaf of a node is a node that has no children.
String,Tree,Depth-First Search,Binary Tree
Medium
48.6
107,393
52,244
1,046
168
null
129,257
1,031
Add to Array-Form of Integer
add-to-array-form-of-integer
The array-form of an integer num is an array representing its digits in left to right order. Given num, the array-form of an integer, and an integer k, return the array-form of the integer num + k.
Array,Math
Easy
45.4
236,136
107,305
1,195
144
null
2,66,67,415
1,032
Satisfiability of Equality Equations
satisfiability-of-equality-equations
You are given an array of strings equations that represent relationships between variables where each string equations[i] is of length 4 and takes one of two different forms: "xi==yi" or "xi!=yi".Here, xi and yi are lowercase letters (not necessarily different) that represent one-letter variable names. Return true if it is possible to assign integers to variable names so as to satisfy all the given equations, or false otherwise.
Array,String,Union Find,Graph
Medium
49.9
97,712
48,792
1,489
16
null
null
1,033
Broken Calculator
broken-calculator
There is a broken calculator that has the integer startValue on its display initially. In one operation, you can: Given two integers startValue and target, return the minimum number of operations needed to display target on the calculator.
Math,Greedy
Medium
54.2
157,662
85,399
2,254
184
null
650
1,034
Subarrays with K Different Integers
subarrays-with-k-different-integers
Given an integer array nums and an integer k, return the number of good subarrays of nums. A good array is an array where the number of different integers in that array is exactly k. A subarray is a contiguous part of an array.
Array,Hash Table,Sliding Window,Counting
Hard
53.3
126,886
67,665
2,888
40
null
3,159,340,2186,2247
1,035
Cousins in Binary Tree
cousins-in-binary-tree
Given the root of a binary tree with unique values and the values of two different nodes of the tree x and y, return true if the nodes corresponding to the values x and y in the tree are cousins, or false otherwise. Two nodes of a binary tree are cousins if they have the same depth with different parents. Note that in a binary tree, the root node is at the depth 0, and children of each depth k node are at the depth k + 1.
Tree,Depth-First Search,Breadth-First Search,Binary Tree
Easy
53.8
380,002
204,495
2,682
138
null
102
1,036
Rotting Oranges
rotting-oranges
You are given an m x n grid where each cell can have one of three values: Every minute, any fresh orange that is 4-directionally adjacent to a rotten orange becomes rotten. Return the minimum number of minutes that must elapse until no cell has a fresh orange. If this is impossible, return -1.
Array,Breadth-First Search,Matrix
Medium
51.7
714,974
369,469
6,363
272
null
286,2206
1,037
Minimum Number of K Consecutive Bit Flips
minimum-number-of-k-consecutive-bit-flips
You are given a binary array nums and an integer k. A k-bit flip is choosing a subarray of length k from nums and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of k-bit flips required so that there is no 0 in the array. If it is not possible, return -1. A subarray is a contiguous part of an array.
Array,Bit Manipulation,Sliding Window,Prefix Sum
Hard
50.7
49,867
25,291
792
49
null
319,2286
1,038
Number of Squareful Arrays
number-of-squareful-arrays
An array is squareful if the sum of every pair of adjacent elements is a perfect square. Given an integer array nums, return the number of permutations of nums that are squareful. Two permutations perm1 and perm2 are different if there is some index i such that perm1[i] != perm2[i].
Array,Math,Dynamic Programming,Backtracking,Bit Manipulation,Bitmask
Hard
49.1
51,178
25,134
657
27
null
47
1,039
Find the Town Judge
find-the-town-judge
In a town, there are n people labeled from 1 to n. There is a rumor that one of these people is secretly the town judge. If the town judge exists, then: You are given an array trust where trust[i] = [ai, bi] representing that the person labeled ai trusts the person labeled bi. Return the label of the town judge if the town judge exists and can be identified, or return -1 otherwise.
Array,Hash Table,Graph
Easy
49.8
542,049
270,102
3,301
253
null
277