Posts

Showing posts with the label hard

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...

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: ...

1835. Find XOR Sum of All Pairs Bitwise AND

Blog The XOR sum of a list is the bitwise XOR of all its elements. If the list only contains one element, then its XOR sum will be equal to this element. For example, the XOR sum of [1,2,3,4] is equal to 1 XOR 2 XOR 3 XOR 4 = 4 and the XOR sum of [3] is equal to 3 . You are given two 0-indexed arrays arr1 and arr2 that consist only of non-negative integers. Consider the list containing the result of arr1[i] AND arr2[j] (bitwise AND ) for every (i, j) pair where 0 and 0 . Return the XOR sum of the aforementioned list. Example: 1 Input: arr1 = [1,2,3], arr2 = [6,5] Output: 0 Example: 2 Input: arr1 = [12], arr2 = [4] Output: 4 ...

329. Longest Increasing Path in a Matrix

Blog Given an m x n integers matrix , return the length of the longest increasing path in matrix . From each cell, you can either move in four directions: left, right, up, or down. You may not move diagonally or move outside the boundary (i.e., wrap-around is not allowed). Example: 1 Input: matrix = [[9,9,4],[6,6,8],[2,1,1]] Output: 4 Example: 2 Input: matrix = [[3,4,5],[3,2,6],[2,2,1]] Output: 4 Example: 3 Input: matrix = [[1]] Output: 1 Constraints: m == matrix.length n == matrix[i].length 1 0 ...

1819. Number of Different Subsequences GCDs (Leetcode)

Blog You are given an array nums that consists of positive integers. The GCD of a sequence of numbers is defined as the greatest integer that divides all the numbers in the sequence evenly. For example, the GCD of the sequence [4,6,16] is 2 . A subsequence of an array is a sequence that can be formed by removing some elements (possibly none) of the array. For example, [2,5,10] is a subsequence of [1,2,1,2,4,1,5,10] . Return the number of different GCDs among all non-empty subsequences of nums . Example: 1 Input: nums = [6,10,3] Output: 5 Example: 2 Input: nums = [5,15,40,5,6] Output: 7 ...

5707. Maximum Number of Groups Getting Fresh Donuts (Leetcode)

Blog There is a donuts shop that bakes donuts in batches of batchSize . They have a rule where they must serve all of the donuts of a batch before serving any donuts of the next batch. You are given an integer batchSize and an integer array groups , where groups[i] denotes that there is a group of groups[i] customers that will visit the shop. Each customer will get exactly one donut. When a group visits the shop, all customers of the group must be served before serving any of the following groups. A group will be happy if they all get fresh donuts. That is, the first customer of the group does not receive a donut that was left over from the previous group. You can freely rearrange the ordering of the groups. Return the maximum possible number of happy groups after rearranging the groups. Example: 1 Input: b...

1808. Maximize Number of Nice Divisors (Leetcode)

Blog You are given a positive integer primeFactors . You are asked to construct a positive integer n that satisfies the following conditions: The number of prime factors of n (not necessarily distinct) is at most primeFactors . The number of nice divisors of n is maximized. Note that a divisor of n is nice if it is divisible by every prime factor of n . For example, if n = 12 , then its prime factors are [2,2,3] , then 6 and 12 are nice divisors, while 3 and 4 are not. Return the number of nice divisors of n . Since that number can be too large, return it modulo 10 9 + 7. Note that a prime number is a natural number greater than 1 that is not a product of two smaller natural numbers. The prime factors of a number n is a list of prime numbers such that thei...

1803. Count Pairs With XOR in a Range

Blog Given a (0-indexed) integer array nums and two integers low and high , return the number of nice pairs . A nice pair is a pair (i, j) where 0 and low . Example: 1 Input: nums = [1,4,2,7], low = 2, high = 6 Output: 6 Example: 2 Input: nums = [9,8,4,2,1], low = 5, high = 14 Output: 8 Constraints: 1 1 1 Data structure required for solving this problem: To solve this problem we need a data structure called trie. Steps required to solve the problem: First we will add i - 1 elem...