This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
// https://leetcode.com/problems/permutations/ | |
// 46. Permutations | |
public List<List<Integer>> permute(int[] nums) { | |
// init output list | |
List<List<Integer>> output = new LinkedList(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public int[][] intervalIntersection(int[][] A, int[][] B) { | |
// https://leetcode.com/problems/interval-list-intersections/ | |
// 986. Interval List Intersections | |
// Time Complexity: O(M+N), where M, NM,N are the lengths of A and B respectively. | |
// Space Complexity: O(M+N), the maximum size of the answer. | |
List<int[]> ans = new ArrayList(); | |
int i = 0, j = 0; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
/* | |
Complexity Analysis | |
Space Complexity : O(1). Only constant space is used. | |
Time Complexity: O(n). However, the total number of operations are still sub-optimal. The total operations (array writes) that code does is nn (Total number of elements). | |
283. Move Zeroes | |
https://leetcode.com/problems/move-zeroes/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
// https://leetcode.com/problems/exclusive-time-of-functions/ | |
// 636. Exclusive Time of Functions | |
// https://leetcode.com/problems/exclusive-time-of-functions/discuss/153497/Java-solution-using-stack-wrapper-class-and-calculation-when-pop-element-from-the-stack. | |
public int[] exclusiveTime(int n, List<String> logs) { | |
Deque<Log> stack = new ArrayDeque<>(); | |
int[] result = new int[n]; | |
for (String content : logs) { | |
Log log = new Log(content); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
// 253. Meeting Rooms II | |
// https://leetcode.com/problems/meeting-rooms-ii/ | |
// Time Complexity: O(NlogN). | |
// Space Complexity: O(N) because we construct the min-heap and that can contain N elements in the worst case as described above in the time complexity section. Hence, the space complexity is O(N). | |
public int minMeetingRooms(int[][] intervals) { | |
// Check for the base case. If there are no intervals, return 0 | |
if (intervals.length == 0) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
// 252. Meeting Rooms | |
// https://leetcode.com/problems/meeting-rooms/solution/ | |
// Time complexity : O(nlogn). The time complexity is dominated by sorting. Once the array has been sorted, only O(n) time is taken to go through the array and determine if there is any overlap. | |
// Space : O(1). Since no additional space is allocated. | |
Space complexity : O(1)O(1). Since no additional space is allocated. | |
public boolean canAttendMeetings(int[][] intervals) { | |
Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0])); | |
for (int i = 0; i < intervals.length - 1; i++) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
// 56. Merge Intervals | |
// Time complexity :O(nlogn) | |
// Generally we do not consider space required by output or input, when we analyse space requirement. | |
// https://leetcode.com/problems/merge-intervals/solution/ | |
// Space complexity : O(logN) or O(n)) | |
// If we can sort intervals in place, we do not need more than constant additional space, although the sorting itself takes O(logn) space. Otherwise, we must allocate linear space to store a copy of intervals and sort that. | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SparseVector { | |
// https://leetcode.com/problems/dot-product-of-two-sparse-vectors/ | |
// 1570. Dot Product of Two Sparse Vectors | |
/* | |
Let n be the length of the input array and L and L2 be the number of non-zero elements for the two vectors. | |
Time complexity: O(n) for creating the <index, value> pair for non-zero values; O(L+L2) | |
for calculating the dot product. | |
Space complexity: O(L) for creating the <index, value> pairs for non-zero values. O(1) for calculating the dot product. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SparseVector { | |
// https://leetcode.com/problems/dot-product-of-two-sparse-vectors/ | |
/* | |
Let n be the length of the input array and LL be the number of non-zero elements. | |
Time complexity: O(n) for creating the Hash Map; O(L) for calculating the dot product. | |
Space complexity: O(L) for creating the Hash Map, as we only store elements that are non-zero. O(1) for calculating the dot product. | |
*/ | |
int[] nums; | |
SparseVector(int[] nums) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
// 11. Container With Most Water | |
// https://leetcode.com/problems/container-with-most-water/ | |
// Time complexity : O(n). Single pass. | |
// Space complexity : O(1). Constant space is used. | |
public int maxArea(int[] height) { | |
int left = 0; |