2022-03-15
1249. Minimum Remove to Make Valid Parentheses
Topic: String, Stack
Difficulty: Medium
Problem:
Given a string s of
Your task is to remove the minimum number of parentheses (
Formally, a parentheses string is valid if and only if:
• It is the empty string, contains only lowercase characters, or
• It can be written as
• It can be written as
Example 1:
Example 2:
Example 3:
Constraints:
•
•
1249. Minimum Remove to Make Valid Parentheses
Topic: String, Stack
Difficulty: Medium
Problem:
Given a string s of
'(' , ')' and lowercase English characters.Your task is to remove the minimum number of parentheses (
'(' or ')', in any positions ) so that the resulting parentheses string is valid and return any valid string.Formally, a parentheses string is valid if and only if:
• It is the empty string, contains only lowercase characters, or
• It can be written as
AB (A concatenated with B), where A and B are valid strings, or• It can be written as
(A), where A is a valid string.Example 1:
Input: s = "lee(t(c)o)de)"
Output: "lee(t(c)o)de"
Explanation: "lee(t(co)de)" , "lee(t(c)ode)" would also be accepted.
Example 2:
Input: s = "a)b(c)d"
Output: "ab(c)d"
Example 3:
Input: s = "))(("
Output: ""
Explanation: An empty string is also valid.
Constraints:
•
1 <= s.length <= 10^5•
s[i] is either'(' , ')', or lowercase English letter.2022-03-16
946. Validate Stack Sequences
Topic: Array, Stack, Simulation
Difficulty: Medium
Problem:
Given two integer arrays
Example 1:
Example 2:
Constraints:
•
•
• All the elements of
•
•
946. Validate Stack Sequences
Topic: Array, Stack, Simulation
Difficulty: Medium
Problem:
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.Example 1:
Input: pushed = [1,2,3,4,5], popped = [4,5,3,2,1]
Output: true
Explanation: We might do the following sequence:
push(1), push(2), push(3), push(4),
pop() -> 4,
push(5),
pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1
Example 2:
Input: pushed = [1,2,3,4,5], popped = [4,3,5,1,2]
Output: false
Explanation: 1 cannot be popped before 2.
Constraints:
•
1 <= pushed.length <= 1000•
0 <= pushed[i] <= 1000• All the elements of
pushed are unique.•
popped.length == pushed.length•
popped is a permutation of pushed.2022-03-17
856. Score of Parentheses
Topic: String, Stack
Difficulty: Medium
Problem:
Given a balanced parentheses string
The score of a balanced parentheses string is based on the following rule:
•
•
•
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
856. Score of Parentheses
Topic: String, Stack
Difficulty: Medium
Problem:
Given a balanced parentheses string
s, return the score of the string.The score of a balanced parentheses string is based on the following rule:
•
"()" has score 1.•
AB has score A + B, where A and B are balanced parentheses strings.•
(A) has score 2 * A, where A is a balanced parentheses string.Example 1:
Input: s = "()"
Output: 1
Example 2:
Input: s = "(())"
Output: 2
Example 3:
Input: s = "()()"
Output: 2
Constraints:
•
2 <= s.length <= 50•
s consists of only '(' and ')'.•
s is a balanced parentheses string.2022-03-18
316. Remove Duplicate Letters
Topic: String, Stack, Greedy, Monotonic Stack
Difficulty: Medium
Problem:
Given a string
Example 1:
Example 2:
Constraints:
•
•
Note: This question is the same as 1081: <https://leetcode.com/problems/smallest-subsequence-of-distinct-characters/>
316. Remove Duplicate Letters
Topic: String, Stack, Greedy, Monotonic Stack
Difficulty: Medium
Problem:
Given a string
s, remove duplicate letters so that every letter appears once and only once. You must make sure your result is the smallest in lexicographical order among all possible results.Example 1:
Input: s = "bcabc"
Output: "abc"
Example 2:
Input: s = "cbacdcbc"
Output: "acdb"
Constraints:
•
1 <= s.length <= 10^4•
s consists of lowercase English letters.Note: This question is the same as 1081: <https://leetcode.com/problems/smallest-subsequence-of-distinct-characters/>
2022-03-19
895. Maximum Frequency Stack
Topic: Hash Table, Stack, Design, Ordered Set
Difficulty: Hard
Problem:
Design a stack-like data structure to push elements to the stack and pop the most frequent element from the stack.
Implement the
•
•
•
• If there is a tie for the most frequent element, the element closest to the stack's top is removed and returned.
Example 1:
Constraints:
•
• At most
• It is guaranteed that there will be at least one element in the stack before calling
895. Maximum Frequency Stack
Topic: Hash Table, Stack, Design, Ordered Set
Difficulty: Hard
Problem:
Design a stack-like data structure to push elements to the stack and pop the most frequent element from the stack.
Implement the
FreqStack class:•
FreqStack() constructs an empty frequency stack.•
void push(int val) pushes an integer val onto the top of the stack.•
int pop() removes and returns the most frequent element in the stack.• If there is a tie for the most frequent element, the element closest to the stack's top is removed and returned.
Example 1:
Input
["FreqStack", "push", "push", "push", "push", "push", "push", "pop", "pop", "pop", "pop"]
[[], [5], [7], [5], [7], [4], [5], [], [], [], []]
Output
[null, null, null, null, null, null, null, 5, 7, 5, 4]
Explanation
FreqStack freqStack = new FreqStack();
freqStack.push(5); // The stack is [5]
freqStack.push(7); // The stack is [5,7]
freqStack.push(5); // The stack is [5,7,5]
freqStack.push(7); // The stack is [5,7,5,7]
freqStack.push(4); // The stack is [5,7,5,7,4]
freqStack.push(5); // The stack is [5,7,5,7,4,5]
freqStack.pop(); // return 5, as 5 is the most frequent. The stack becomes [5,7,5,7,4].
freqStack.pop(); // return 7, as 5 and 7 is the most frequent, but 7 is closest to the top. The stack becomes [5,7,5,4].
freqStack.pop(); // return 5, as 5 is the most frequent. The stack becomes [5,7,4].
freqStack.pop(); // return 4, as 4, 5 and 7 is the most frequent, but 4 is closest to the top. The stack becomes [5,7].
Constraints:
•
0 <= val <= 10^9• At most
2 * 10^4 calls will be made to push and pop.• It is guaranteed that there will be at least one element in the stack before calling
pop.2022-03-20
1007. Minimum Domino Rotations For Equal Row
Topic: Array, Greedy
Difficulty: Medium
Problem:
In a row of dominoes,
We may rotate the
Return the minimum number of rotations so that all the values in
If it cannot be done, return
Example 1:
Image: https://assets.leetcode.com/uploads/2021/05/14/domino.png
Example 2:
Constraints:
•
•
•
1007. Minimum Domino Rotations For Equal Row
Topic: Array, Greedy
Difficulty: Medium
Problem:
In a row of dominoes,
tops[i] and bottoms[i] represent the top and bottom halves of the i^th domino. (A domino is a tile with two numbers from 1 to 6 - one on each half of the tile.)We may rotate the
i^th domino, so that tops[i] and bottoms[i] swap values.Return the minimum number of rotations so that all the values in
tops are the same, or all the values in bottoms are the same.If it cannot be done, return
-1.Example 1:
Image: https://assets.leetcode.com/uploads/2021/05/14/domino.png
Input: tops = [2,1,2,4,2,2], bottoms = [5,2,6,2,3,2]
Output: 2
Explanation:
The first figure represents the dominoes as given by tops and bottoms: before we do any rotations.
If we rotate the second and fourth dominoes, we can make every value in the top row equal to 2, as indicated by the second figure.
Example 2:
Input: tops = [3,5,1,2,3], bottoms = [3,6,3,3,4]
Output: -1
Explanation:
In this case, it is not possible to rotate the dominoes to make one row of values equal.
Constraints:
•
2 <= tops.length <= 2 * 10^4•
bottoms.length == tops.length•
1 <= tops[i], bottoms[i] <= 62022-03-21
763. Partition Labels
Topic: Hash Table, Two Pointers, String, Greedy
Difficulty: Medium
Problem:
You are given a string
Note that the partition is done so that after concatenating all the parts in order, the resultant string should be
Return a list of integers representing the size of these parts.
Example 1:
Example 2:
Constraints:
•
•
763. Partition Labels
Topic: Hash Table, Two Pointers, String, Greedy
Difficulty: Medium
Problem:
You are given a string
s. We want to partition the string into as many parts as possible so that each letter appears in at most one part.Note that the partition is done so that after concatenating all the parts in order, the resultant string should be
s.Return a list of integers representing the size of these parts.
Example 1:
Input: s = "ababcbacadefegdehijhklij"
Output: [9,7,8]
Explanation:
The partition is "ababcbaca", "defegde", "hijhklij".
This is a partition so that each letter appears in at most one part.
A partition like "ababcbacadefegde", "hijhklij" is incorrect, because it splits s into less parts.
Example 2:
Input: s = "eccbbbbdec"
Output: [10]
Constraints:
•
1 <= s.length <= 500•
s consists of lowercase English letters.2022-03-22
1663. Smallest String With A Given Numeric Value
Topic: String, Greedy
Difficulty: Medium
Problem:
The numeric value of a lowercase character is defined as its position
The numeric value of a string consisting of lowercase characters is defined as the sum of its characters' numeric values. For example, the numeric value of the string
You are given two integers
Note that a string
Example 1:
Example 2:
Constraints:
•
•
1663. Smallest String With A Given Numeric Value
Topic: String, Greedy
Difficulty: Medium
Problem:
The numeric value of a lowercase character is defined as its position
(1-indexed) in the alphabet, so the numeric value of a is 1, the numeric value of b is 2, the numeric value of c is 3, and so on.The numeric value of a string consisting of lowercase characters is defined as the sum of its characters' numeric values. For example, the numeric value of the string
"abe" is equal to 1 + 2 + 5 = 8.You are given two integers
n and k. Return the lexicographically smallest string with length equal to n and numeric value equal to k.Note that a string
x is lexicographically smaller than string y if x comes before y in dictionary order, that is, either x is a prefix of y, or if i is the first position such that x[i] != y[i], then x[i] comes before y[i] in alphabetic order.Example 1:
Input: n = 3, k = 27
Output: "aay"
Explanation: The numeric value of the string is 1 + 1 + 25 = 27, and it is the smallest string with such a value and length equal to 3.
Example 2:
Input: n = 5, k = 73
Output: "aaszz"
Constraints:
•
1 <= n <= 10^5•
n <= k <= 26 * n2022-03-23
991. Broken Calculator
Topic: Math, Greedy
Difficulty: Medium
Problem:
There is a broken calculator that has the integer
• multiply the number on display by
• subtract
Given two integers
Example 1:
Example 2:
Example 3:
Constraints:
•
991. Broken Calculator
Topic: Math, Greedy
Difficulty: Medium
Problem:
There is a broken calculator that has the integer
startValue on its display initially. In one operation, you can:• multiply the number on display by
2, or• subtract
1 from the number on display.Given two integers
startValue and target, return the minimum number of operations needed to display target on the calculator.Example 1:
Input: startValue = 2, target = 3
Output: 2
Explanation: Use double operation and then decrement operation {2 -> 4 -> 3}.
Example 2:
Input: startValue = 5, target = 8
Output: 2
Explanation: Use decrement and then double {5 -> 4 -> 8}.
Example 3:
Input: startValue = 3, target = 10
Output: 3
Explanation: Use double, decrement and double {3 -> 6 -> 5 -> 10}.
Constraints:
•
1 <= x, y <= 10^92022-03-24
881. Boats to Save People
Topic: Array, Two Pointers, Greedy, Sorting
Difficulty: Medium
Problem:
You are given an array
Return the minimum number of boats to carry every given person.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
881. Boats to Save People
Topic: Array, Two Pointers, Greedy, Sorting
Difficulty: Medium
Problem:
You are given an array
people where people[i] is the weight of the i^th person, and an infinite number of boats where each boat can carry a maximum weight of limit. Each boat carries at most two people at the same time, provided the sum of the weight of those people is at most limit.Return the minimum number of boats to carry every given person.
Example 1:
Input: people = [1,2], limit = 3
Output: 1
Explanation: 1 boat (1, 2)
Example 2:
Input: people = [3,2,2,1], limit = 3
Output: 3
Explanation: 3 boats (1, 2), (2) and (3)
Example 3:
Input: people = [3,5,3,4], limit = 5
Output: 4
Explanation: 4 boats (3), (3), (4), (5)
Constraints:
•
1 <= people.length <= 5 * 10^4•
1 <= people[i] <= limit <= 3 * 10^42022-03-25
1029. Two City Scheduling
Topic: Array, Greedy, Sorting
Difficulty: Medium
Problem:
A company is planning to interview
Return the minimum cost to fly every person to a city such that exactly
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
•
1029. Two City Scheduling
Topic: Array, Greedy, Sorting
Difficulty: Medium
Problem:
A company is planning to interview
2n people. Given the array costs where costs[i] = [aCost_i, bCost_i], the cost of flying the i^th person to city a is aCost_i, and the cost of flying the i^th person to city b is bCost_i.Return the minimum cost to fly every person to a city such that exactly
n people arrive in each city.Example 1:
Input: costs = [[10,20],[30,200],[400,50],[30,20]]
Output: 110
Explanation:
The first person goes to city A for a cost of 10.
The second person goes to city A for a cost of 30.
The third person goes to city B for a cost of 50.
The fourth person goes to city B for a cost of 20.
The total minimum cost is 10 + 30 + 50 + 20 = 110 to have half the people interviewing in each city.
Example 2:
Input: costs = [[259,770],[448,54],[926,667],[184,139],[840,118],[577,469]]
Output: 1859
Example 3:
Input: costs = [[515,563],[451,713],[537,709],[343,819],[855,779],[457,60],[650,359],[631,42]]
Output: 3086
Constraints:
•
2 * n == costs.length•
2 <= costs.length <= 100•
costs.length is even.•
1 <= aCost_i, bCost_i <= 10002022-03-26
704. Binary Search
Topic: Array, Binary Search
Difficulty: Easy
Problem:
Given an array of integers
You must write an algorithm with
Example 1:
Example 2:
Constraints:
•
•
• All the integers in
•
704. Binary Search
Topic: Array, Binary Search
Difficulty: Easy
Problem:
Given an array of integers
nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1.You must write an algorithm with
O(log n) runtime complexity.Example 1:
Input: nums = [-1,0,3,5,9,12], target = 9
Output: 4
Explanation: 9 exists in nums and its index is 4
Example 2:
Input: nums = [-1,0,3,5,9,12], target = 2
Output: -1
Explanation: 2 does not exist in nums so return -1
Constraints:
•
1 <= nums.length <= 10^4•
-10^4 < nums[i], target < 10^4• All the integers in
nums are unique.•
nums is sorted in ascending order.2022-03-27
1337. The K Weakest Rows in a Matrix
Topic: Array, Binary Search, Sorting, Heap (Priority Queue), Matrix
Difficulty: Easy
Problem:
You are given an
A row
• The number of soldiers in row
• Both rows have the same number of soldiers and
Return the indices of the
Example 1:
Example 2:
Constraints:
•
•
•
•
•
1337. The K Weakest Rows in a Matrix
Topic: Array, Binary Search, Sorting, Heap (Priority Queue), Matrix
Difficulty: Easy
Problem:
You are given an
m x n binary matrix mat of 1's (representing soldiers) and 0's (representing civilians). The soldiers are positioned in front of the civilians. That is, all the 1's will appear to the left of all the 0's in each row.A row
i is weaker than a row j if one of the following is true:• The number of soldiers in row
i is less than the number of soldiers in row j.• Both rows have the same number of soldiers and
i < j.Return the indices of the
k weakest rows in the matrix ordered from weakest to strongest.Example 1:
Input: mat =
[[1,1,0,0,0],
[1,1,1,1,0],
[1,0,0,0,0],
[1,1,0,0,0],
[1,1,1,1,1]],
k = 3
Output: [2,0,3]
Explanation:
The number of soldiers in each row is:
- Row 0: 2
- Row 1: 4
- Row 2: 1
- Row 3: 2
- Row 4: 5
The rows ordered from weakest to strongest are [2,0,3,1,4].
Example 2:
Input: mat =
[[1,0,0,0],
[1,1,1,1],
[1,0,0,0],
[1,0,0,0]],
k = 2
Output: [0,2]
Explanation:
The number of soldiers in each row is:
- Row 0: 1
- Row 1: 4
- Row 2: 1
- Row 3: 1
The rows ordered from weakest to strongest are [0,2,3,1].
Constraints:
•
m == mat.length•
n == mat[i].length•
2 <= n, m <= 100•
1 <= k <= m•
matrix[i][j] is either 0 or 1.2022-03-28
81. Search in Rotated Sorted Array II
Topic: Array, Binary Search
Difficulty: Medium
Problem:
There is an integer array
Before being passed to your function,
Given the array
You must decrease the overall operation steps as much as possible.
Example 1:
Example 2:
Constraints:
•
•
•
•
Follow up: This problem is similar to Search in Rotated Sorted Array, but
81. Search in Rotated Sorted Array II
Topic: Array, Binary Search
Difficulty: Medium
Problem:
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.
Example 1:
Input: nums = [2,5,6,0,0,1,2], target = 0
Output: true
Example 2:
Input: nums = [2,5,6,0,0,1,2], target = 3
Output: false
Constraints:
•
1 <= nums.length <= 5000•
-10^4 <= nums[i] <= 10^4•
nums is guaranteed to be rotated at some pivot.•
-10^4 <= target <= 10^4Follow up: This problem is similar to Search in Rotated Sorted Array, but
nums may contain duplicates. Would this affect the runtime complexity? How and why?2022-03-29
287. Find the Duplicate Number
Topic: Array, Two Pointers, Binary Search, Bit Manipulation
Difficulty: Medium
Problem:
Given an array of integers
There is only one repeated number in
You must solve the problem without modifying the array
Example 1:
Example 2:
Constraints:
•
•
•
• All the integers in
Follow up:
• How can we prove that at least one duplicate number must exist in
• Can you solve the problem in linear runtime complexity?
287. Find the Duplicate Number
Topic: Array, Two Pointers, Binary Search, Bit Manipulation
Difficulty: Medium
Problem:
Given an array of integers
nums containing n + 1 integers where each integer is in the range [1, n] inclusive.There is only one repeated number in
nums, return this repeated number.You must solve the problem without modifying the array
nums and uses only constant extra space.Example 1:
Input: nums = [1,3,4,2,2]
Output: 2
Example 2:
Input: nums = [3,1,3,4,2]
Output: 3
Constraints:
•
1 <= n <= 10^5•
nums.length == n + 1•
1 <= nums[i] <= n• All the integers in
nums appear only once except for precisely one integer which appears two or more times.Follow up:
• How can we prove that at least one duplicate number must exist in
nums?• Can you solve the problem in linear runtime complexity?
2022-03-30
74. Search a 2D Matrix
Topic: Array, Binary Search, Matrix
Difficulty: Medium
Problem:
Write an efficient algorithm that searches for a value
• Integers in each row are sorted from left to right.
• The first integer of each row is greater than the last integer of the previous row.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/10/05/mat.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2020/10/05/mat2.jpg
Constraints:
•
•
•
•
74. Search a 2D Matrix
Topic: Array, Binary Search, Matrix
Difficulty: Medium
Problem:
Write an efficient algorithm that searches for a value
target in an m x n integer matrix matrix. This matrix has the following properties:• Integers in each row are sorted from left to right.
• The first integer of each row is greater than the last integer of the previous row.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/10/05/mat.jpg
Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
Output: true
Example 2:
Image: https://assets.leetcode.com/uploads/2020/10/05/mat2.jpg
Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13
Output: false
Constraints:
•
m == matrix.length•
n == matrix[i].length•
1 <= m, n <= 100•
-10^4 <= matrix[i][j], target <= 10^42022-03-31
410. Split Array Largest Sum
Topic: Array, Binary Search, Dynamic Programming, Greedy
Difficulty: Hard
Problem:
Given an array
Write an algorithm to minimize the largest sum among these
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
410. Split Array Largest Sum
Topic: Array, Binary Search, Dynamic Programming, Greedy
Difficulty: Hard
Problem:
Given an array
nums which consists of non-negative integers and an integer m, you can split the array into m non-empty continuous subarrays.Write an algorithm to minimize the largest sum among these
m subarrays.Example 1:
Input: nums = [7,2,5,10,8], m = 2
Output: 18
Explanation:
There are four ways to split nums into two subarrays.
The best way is to split it into [7,2,5] and [10,8],
where the largest sum among the two subarrays is only 18.
Example 2:
Input: nums = [1,2,3,4,5], m = 2
Output: 9
Example 3:
Input: nums = [1,4,4], m = 3
Output: 4
Constraints:
•
1 <= nums.length <= 1000•
0 <= nums[i] <= 10^6•
1 <= m <= min(50, nums.length)2022-04-01
344. Reverse String
Topic: Two Pointers, String, Recursion
Difficulty: Easy
Problem:
Write a function that reverses a string. The input string is given as an array of characters
You must do this by modifying the input array in-place with
Example 1:
Example 2:
Constraints:
•
•
344. Reverse String
Topic: Two Pointers, String, Recursion
Difficulty: Easy
Problem:
Write a function that reverses a string. The input string is given as an array of characters
s.You must do this by modifying the input array in-place with
O(1) extra memory.Example 1:
Input: s = ["h","e","l","l","o"]
Output: ["o","l","l","e","h"]
Example 2:
Input: s = ["H","a","n","n","a","h"]
Output: ["h","a","n","n","a","H"]
Constraints:
•
1 <= s.length <= 10^5•
s[i] is a printable ascii character.2022-04-02
680. Valid Palindrome II
Topic: Two Pointers, String, Greedy
Difficulty: Easy
Problem:
Given a string
Example 1:
Example 2:
Example 3:
Constraints:
•
•
680. Valid Palindrome II
Topic: Two Pointers, String, Greedy
Difficulty: Easy
Problem:
Given a string
s, return true if the s can be palindrome after deleting at most one character from it.Example 1:
Input: s = "aba"
Output: true
Example 2:
Input: s = "abca"
Output: true
Explanation: You could delete the character 'c'.
Example 3:
Input: s = "abc"
Output: false
Constraints:
•
1 <= s.length <= 10^5•
s consists of lowercase English letters.2022-04-03
31. Next Permutation
Topic: Array, Two Pointers
Difficulty: Medium
Problem:
A permutation of an array of integers is an arrangement of its members into a sequence or linear order.
• For example, for
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).
• For example, the next permutation of
• Similarly, the next permutation of
• While the next permutation of
Given an array of integers
The replacement must be in place and use only constant extra memory.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
31. Next Permutation
Topic: Array, Two Pointers
Difficulty: Medium
Problem:
A permutation of an array of integers is an arrangement of its members into a sequence or linear order.
• For example, for
arr = [1,2,3], the following are considered permutations of arr: [1,2,3], [1,3,2], [3,1,2], [2,3,1].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).
• For example, the next permutation of
arr = [1,2,3] is [1,3,2].• Similarly, the next permutation of
arr = [2,3,1] is [3,1,2].• While the next permutation of
arr = [3,2,1] is [1,2,3] because [3,2,1] does not have a lexicographical larger rearrangement.Given an array of integers
nums, find the next permutation of nums.The replacement must be in place and use only constant extra memory.
Example 1:
Input: nums = [1,2,3]
Output: [1,3,2]
Example 2:
Input: nums = [3,2,1]
Output: [1,2,3]
Example 3:
Input: nums = [1,1,5]
Output: [1,5,1]
Constraints:
•
1 <= nums.length <= 100•
0 <= nums[i] <= 100