Question
Implement pow(x, n), which calculates x
raised to the power n
(i.e., xn
).
1 | class Solution { |
Complexity:
Time complexity: O( logn)
Space complexity: O(logn)
Implement pow(x, n), which calculates x
raised to the power n
(i.e., xn
).
1 | class Solution { |
Complexity:
Time complexity: O( logn)
Space complexity: O(logn)
Given an array nums
of size n
, return the majority element.
The majority element is the element that appears more than ⌊n / 2⌋
times. You may assume that the majority element always exists in the array.
https://leetcode.com/problems/majority-element/
1 | class Solution { |
Complexity:
Time complexity: O( n)
Space complexity: O(1)
A trie (pronounced as “try”) or prefix tree is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. There are various applications of this data structure, such as autocomplete and spellchecker.
Implement the Trie class:
Trie()
Initializes the trie object.void insert(String word)
Inserts the string word
into the trie.boolean search(String word)
Returns true
if the string word
is in the trie (i.e., was inserted before), and false
otherwise.boolean startsWith(String prefix)
Returns true
if there is a previously inserted string word
that has the prefix prefix
, and false
otherwise.https://leetcode.com/problems/implement-trie-prefix-tree/
1 | class Trie { |
Complexity:
Time complexity: O( n)
Space complexity: O(1)
Given an array intervals
where intervals[i] = [li, ri]
represent the interval [li, ri)
, remove all intervals that are covered by another interval in the list.
The interval [a, b)
is covered by the interval [c, d)
if and only if c <= a
and b <= d
.
Return the number of remaining intervals.
https://leetcode.com/problems/remove-covered-intervals/
1 | class Solution { |
Complexity:
Time complexity: O( nlogn)
Space complexity: O(1)
Given an array of intervals intervals
where intervals[i] = [starti, endi]
, return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.
https://leetcode.com/problems/non-overlapping-intervals/
1 | class Solution { |
Complexity:
Time complexity: O( nlogn)
Space complexity: O(1)
A set of real numbers can be represented as the union of several disjoint intervals, where each interval is in the form [a, b)
. A real number x
is in the set if one of its intervals [a, b)
contains x
(i.e. a <= x < b
).
You are given a sorted list of disjoint intervals intervals
representing a set of real numbers as described above, where intervals[i] = [ai, bi]
represents the interval [ai, bi)
. You are also given another interval toBeRemoved
.
Return the set of real numbers with the interval toBeRemoved
removed from intervals
. In other words, return the set of real numbers such that every x
in the set is in intervals
but not in toBeRemoved
. Your answer should be a sorted list of disjoint intervals as described above.
https://leetcode.com/problems/remove-interval/
1 | class Solution { |
Complexity:
Time complexity: O( n)
Space complexity: O(n)
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.
https://leetcode.com/problems/insert-interval/
1 | class Solution { |
Complexity:
Time complexity: O( n)
Space complexity: O(n)
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.
https://leetcode.com/problems/merge-intervals/
1 | class Solution { |
Complexity:
Time complexity: O( nlogn)
Space complexity: O(n)
Given an array of meeting time intervals intervals
where intervals[i] = [starti, endi]
, return the minimum number of conference rooms required.
https://leetcode.com/problems/meeting-rooms-ii/
1 | class Solution { |
Complexity:
Time complexity: O( nlogn)
Space complexity: O(n)
Given an array of meeting time intervals
where intervals[i] = [starti, endi]
, determine if a person could attend all meetings.
https://leetcode.com/problems/meeting-rooms/
1 | class Solution { |
Complexity:
Time complexity: O( nlogn)
Space complexity: O(1)