diff --git a/Problem1.java b/Problem1.java index 8b137891..995013f7 100644 --- a/Problem1.java +++ b/Problem1.java @@ -1 +1,37 @@ +// Time Complexity : O(log n) because we use Binary Search on the sorted array +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + +// Your code here along with comments explaining your approach in three sentences only +// We use Binary Search to find the first index where the expected value and actual value do not match. +// Before the missing number, nums[i] == i + 1 holds true, and after the missing number this relationship breaks. +// The first mismatching position helps us determine the missing number directly. + +class Solution { + + public int missingNumber(int[] nums) { + + int low = 0; + int high = nums.length - 1; + + while (low <= high) { + + int mid = low + (high - low) / 2; + + // left side is correct + if (nums[mid] == mid + 1) { + low = mid + 1; + } + + // missing number lies on left side + else { + high = mid - 1; + } + } + + // missing number + return low + 1; + } +} \ No newline at end of file diff --git a/Problem2.java b/Problem2.java index 8b137891..b7781c43 100644 --- a/Problem2.java +++ b/Problem2.java @@ -1 +1,123 @@ +// Time Complexity : push -> O(log n), pop -> O(log n), peek -> O(1), size -> O(1) +// Space Complexity : O(n) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + +// Your code here along with comments explaining your approach in three sentences only +// We implement a Min Heap using an ArrayList where the smallest element is always stored at the root. +// push inserts the element at the end and bubbles it upward to maintain heap order, while pop removes the root and heapifies downward. +// peek returns the minimum element at the root, and size returns the total number of elements currently present in the heap. + +class minHeap { + + List heap; + + // Constructor + public minHeap() { + + // initialize heap + heap = new ArrayList<>(); + } + + public void push(int x) { + + // insert element at end + heap.add(x); + + int curr = heap.size() - 1; + + // bubble up + while (curr > 0) { + + int parent = (curr - 1) / 2; + + // maintain min heap property + if (heap.get(parent) > heap.get(curr)) { + + swap(parent, curr); + + curr = parent; + } else { + break; + } + } + } + + public void pop() { + + // heap empty + if (heap.size() == 0) { + return; + } + + int last = heap.get(heap.size() - 1); + + // move last element to root + heap.set(0, last); + + // remove last element + heap.remove(heap.size() - 1); + + // restore heap property + heapify(0); + } + + public int peek() { + + // return minimum element + if (heap.size() == 0) { + return -1; + } + + return heap.get(0); + } + + public int size() { + + // return heap size + return heap.size(); + } + + // heapify downward + private void heapify(int i) { + + int smallest = i; + + int left = 2 * i + 1; + + int right = 2 * i + 2; + + // compare left child + if (left < heap.size() && + heap.get(left) < heap.get(smallest)) { + + smallest = left; + } + + // compare right child + if (right < heap.size() && + heap.get(right) < heap.get(smallest)) { + + smallest = right; + } + + // swap if needed + if (smallest != i) { + + swap(i, smallest); + + heapify(smallest); + } + } + + // helper swap function + private void swap(int i, int j) { + + int temp = heap.get(i); + + heap.set(i, heap.get(j)); + + heap.set(j, temp); + } +}