Meta LC

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

528. Random Pick with Weight You are given a 0-indexed array of positive integers w where w[i] describes the weight of the ith index. You need to implement the function pickIndex(), which randomly picks an index in the range [0, w.length - 1] (inclusive) and returns it. The probability of picking an index i is w[i] / sum(w). For example, if w = [1, 3], the probability of picking index 0 is 1 / (1 + 3) = 0.25 (i.e., 25%), and the probability of picking index 1 is 3 / (1 + 3) = 0.75 (i.e., 75%).

✅ Use a prefix sum array (prefixSums[i] = sum(w[0] to w[i])). ✅ Generate a random number in [1, totalSum]. ✅ Use binary search (O(log N)) to find the correct index. ✅ Preprocessing: O(N), Query: O(log N), Space: O(N).

680. Valid Palindrome II Given a string s, return true if the s can be palindrome after deleting at most one character from it. Example 1: Input: s = "aba" Output: true Example 2: Input: s = "abca" Output: true Explanation: You could delete the character 'c'. Example 3: Input: s = "abc" Output: false https://leetcode.com/problems/valid-palindrome-ii/description/

✅ Use the two-pointer approach: Initialize two pointers → left = 0, right = s.length() - 1 Move inward while characters match On first mismatch:Try skipping either left (left + 1) or right (right - 1)Check if the remaining substring is a palindrome Helper function isPalindrome(s, left, right) validates a substring efficiently Time Complexity: O(N) Space Complexity: O(1) 🔹 Key Idea: If skipping one character makes it a palindrome, return true; otherwise, return false.

88. Merge Sorted Array You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n, representing the number of elements in nums1 and nums2 respectively. Merge nums1 and nums2 into a single array sorted in non-decreasing order. The final sorted array should not be returned by the function, but instead be stored inside the array nums1. To accommodate this, nums1 has a length of m + n, where the first m elements denote the elements that should be merged, and the last n elements are set to 0 and should be ignored. nums2 has a length of n.

✅ Use three pointers: p1 at m - 1 (last valid element in nums1) p2 at n - 1 (last element in nums2) p at m + n - 1 (last position in nums1) ✅ Compare elements from the end and place the larger one at nums1[p]. ✅ If elements remain in nums2, copy them to nums1. ✅ Time Complexity: O(m + n), Space Complexity: O(1).

215. Kth Largest Element in an Array Given an integer array nums and an integer k, return the kth largest element in the array. Note that it is the kth largest element in the sorted order, not the kth distinct element. Can you solve it without sorting? Example 1: Input: nums = [3,2,1,5,6,4], k = 2 Output: 5 Example 2: Input: nums = [3,2,3,1,2,4,5,5,6], k = 4 Output: 4

✅ Min-Heap (PriorityQueue) → O(N log k), stores k elements ✅ Quickselect (Hoare's Selection Algorithm) → O(N) on average, in-place.

314: Binary Tree Vertical Order Traversal Given the root of a binary tree, return the vertical order traversal of its nodes' values.Nodes should be grouped by column index, starting from the leftmost column to the rightmost. Nodes with the same column index should be ordered top to bottom (level-wise). If multiple nodes are at the same column and depth, order them by insertion order (left to right).

✅ Use BFS with a queue (node, columnIndex). ✅ Use TreeMap<Integer, List<Integer>> to store nodes sorted by column index. ✅ Start from column 0, go left -1, right +1. ✅ Extract sorted columns from TreeMap and return. ✅ Time Complexity: O(N log N), Space Complexity: O(N).


संबंधित स्टडी सेट्स

Chapter 5: Consumer Markets and Consumer Buyer Behavior

View Set

Multiplication to 12 ( with sound )

View Set

Motivation and Attitudes (Khan Academy)

View Set

Chapter 18 The Ovaries and Fallopian Tubes by Penny

View Set

Business Law: Reality of Consent, Capacity to Contract, Illegality

View Set

La biographie de Christiano Ronaldo - Christiano Ronaldo's biography

View Set

INCOME TAX: Chapter 12:Self-Employment

View Set

PrepU Exam 2: Perfusion, Immunity, Metabolism

View Set