LeetCode 46. Permutations
Question
Given an array nums
of distinct integers, return all the possible permutations. You can return the answer in any order.
Given an array nums
of distinct integers, return all the possible permutations. You can return the answer in any order.
Given an array of integers arr
, you are initially positioned at the first index of the array.
In one step you can jump from index i
to index:
i + 1
where: i + 1 < arr.length
.i - 1
where: i - 1 >= 0
.j
where: arr[i] == arr[j]
and i != j
.Return the minimum number of steps to reach the last index of the array.
Notice that you can not jump outside of the array at any time.
Example 1:
1 | Input: arr = [100,-23,-23,404,100,23,23,23,3,404] |
Example 2:
1 | Input: arr = [7] |
Example 3:
1 | Input: arr = [7,6,9,6,9,6,9,7] |
Constraints:
1 <= arr.length <= 5 * 104
-108 <= arr[i] <= 108
Source: https://leetcode.com/problems/jump-game-iv/
The idea of using queue size is to achieve layer-order traversal, like LeetCode 102. Binary Tree Level Order Traversal. So we can record the length of current search paths easily.
Repeated visits to nodes with the same value are not helpful in getting the min number of steps to the end. Thus, we can prune the BFS tree. Note that marking those nodes as visited is not enough. We need to clear corresponding key-value pair in the valueIndexMap
. Because as long as these nodes are pushed to the queue, they will increase time cost.
1 | public int minJumps(int[] arr) { |
Given an array of non-negative integers arr
, you are initially positioned at start
index of the array. When you are at index i
, you can jump to i + arr[i]
or i - arr[i]
, check if you can reach to any index with value 0.
Notice that you can not jump outside of the array at any time.
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.
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.
There is an undirected graph with n
nodes, where each node is numbered between 0
and n - 1
. You are given a 2D array graph
, where graph[u]
is an array of nodes that node u
is adjacent to. More formally, for each v
in graph[u]
, there is an undirected edge between node u
and node v
. The graph has the following properties:
graph[u]
does not contain u
).graph[u]
does not contain duplicate values).v
is in graph[u]
, then u
is in graph[v]
(the graph is undirected).u
and v
such that there is no path between them.A graph is bipartite if the nodes can be partitioned into two independent sets A
and B
such that every edge in the graph connects a node in set A
and a node in set B
.
Return true
if and only if it is bipartite.
Given the root
of a binary tree, return the level order traversal of its nodes’ values. (i.e., from left to right, level by level).
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.
We are given a list schedule
of employees, which represents the working time for each employee.
Each employee has a list of non-overlapping Intervals
, and these intervals are in sorted order.
Return the list of finite intervals representing common, positive-length free time for all employees, also in sorted order.
(Even though we are representing Intervals
in the form [x, y]
, the objects inside are Intervals
, not lists or arrays. For example, schedule[0][0].start = 1
, schedule[0][0].end = 2
, and schedule[0][0][0]
is not defined). Also, we wouldn’t include intervals like [5, 5] in our answer, as they have zero length.
You are given a string s
and an array of strings words
. You should add a closed pair of bold tag <b>
and </b>
to wrap the substrings in s
that exist in words
. If two such substrings overlap, you should wrap them together with only one pair of closed bold-tag. If two substrings wrapped by bold tags are consecutive, you should combine them.
Return s
after adding the bold tags.