Posts

1879. Minimum XOR Sum of Two Arrays

Blog You are given two integer arrays nums1 and nums2 of length n . The XOR sum of the two integer arrays is (nums1[0] XOR nums2[0]) + (nums1[1] XOR nums2[1]) + ... + (nums1[n - 1] XOR nums2[n - 1]) (0-indexed) . For example, the XOR sum of [1,2,3] and [3,2,1] is equal to (1 XOR 3) + (2 XOR 2) + (3 XOR 1) = 2 + 0 + 2 = 4 . Rearrange the elements of nums2 such that the resulting XOR sum is minimized . Return the XOR sum after the rearrangement. Example: 1 Input: nums1 = [1,2], nums2 = [2,3] Output: 2 Example: 2 Input: nums1 = [1,0,3], nums2 = [5,3,4] Output: 8 Constraints:

1872. Stone Game VIII

Blog Alice and Bob take turns playing a game, with Alice starting first . There are n stones arranged in a row. On each player's turn, while the number of stones is more than one , they will do the following: Choose an integer x > 1 , and remove the leftmost x stones from the row. Add the sum of the removed stones' values to the player's score. Place a new stone , whose value is equal to that sum, on the left side of the row. The game stops when only one stone is left in the row. The score difference between Alice and Bob is (Alice's score - Bob's score) . Alice's goal is to maximize the score difference, and Bob's goal is the minimize the score difference. Given an integer array stones of length n where stones[i] r

1866. Number of Ways to Rearrange Sticks With K Sticks Visible

Blog There are n uniquely-sized sticks whose lengths are integers from 1 to n . You want to arrange the sticks such that exactly k sticks are visible from the left. A stick is visible from the left if there are no longer sticks to the left of it. For example, if the sticks are arranged [1,3,2,5,4] , then the sticks with lengths 1 , 3 , and 5 are visible from the left. Given n and k , return the number of such arrangements. Since the answer may be large, return it modulo 10 9 + 7 . Example: 1 Input: n = 3, k = 2 Output: 3 Example: 2 Input: n = 5, k = 5 Output: 1 Example: 3 Input: n = 20, k = 11 Output: 6

198. House Robber

Blog You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night . Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police . Example: 1 Input: nums = [1,2,3,1] Output: 4 Example: 2 Input: nums = [2,7,9,3,1] Output: 12 Constraints: 1 <= nums.length <= 100 0 <= nums

121. Best Time to Buy and Sell Stock

Blog You are given an array prices where prices[i] is the price of a given stock on the i th day. You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock. Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0 . Example: 1 Input: prices = [7,1,5,3,6,4] Output: 5 Example: 2 Input: [7,6,4,3,1] Output: 0 Constraints: 1 <= prices.length <= 10 ^ 5 0 <= prices[i] <= 10 ^ 4 Important observation: Let&#

123. Best Time to Buy and Sell Stock III

Blog You are given an array prices where prices[i] is the price of a given stock on the i th day. Find the maximum profit you can achieve. You may complete at most two transactions . Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again). Example: 1 Input: prices = [3,3,5,0,0,3,1,4] Output: 6 Example: 2 Input: [1,2,3,4,5] Output: 4 Example: 3 Input: [7,6,4,3,1] Output: 0 Example: 4 Input: [1] Output: 0 Constraints:

96. Unique Binary Search Trees

Blog Given an integer n , return the number of structurally unique BST 's (binary search trees) which has exactly n nodes of unique values from 1 to n . Example: 1 Input: n = 3 Output: 5 Example: 2 Input: n = 1 Output: 1 Constraints: 1 <= n <= 19 Important observation for solving the problem: If we keep K as our root, then, all numbers lower than K will come in left subtree and all numbers greater than 1 will come in right subtree. Also, if we have X ways of forming left subtree and Y ways of forming right subtree, then, total number of trees rooted at K can be calculated as : ways(K) =