Skip to content

Instantly share code, notes, and snippets.

@anil477
anil477 / Permutations.java
Created January 29, 2022 10:42
46. Permutations
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();
@anil477
anil477 / IntervalListIntersections.java
Created January 28, 2022 19:21
986. Interval List Intersections
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;
@anil477
anil477 / MoveZeroes.java
Created January 28, 2022 19:01
283. Move Zeroes
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/
@anil477
anil477 / ExclusiveTimeOfFunctions.java
Last active January 28, 2022 17:59
636. Exclusive Time of Functions
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);
@anil477
anil477 / MeetingRooms2.java
Created January 28, 2022 14:58
253. Meeting Rooms II
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) {
@anil477
anil477 / MeetingRooms.java
Created January 28, 2022 14:13
252. Meeting Rooms
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++) {
@anil477
anil477 / MergeIntervals.java
Created January 28, 2022 14:07
56. Merge Intervals
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.
@anil477
anil477 / DotProductTwoSparseVectors.java
Created January 28, 2022 10:01
1570. Dot Product of Two Sparse Vectors
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.
@anil477
anil477 / DotProductTwoSparseVectors.java
Last active January 28, 2022 10:00
1570. Dot Product of Two Sparse Vectors
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) {
@anil477
anil477 / ContainerWithMostWater.java
Created January 27, 2022 20:25
11. Container With Most Water
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;