Processing math: 100%
[LeetCode] Lemonade Change

860. Lemonade Change

At a lemonade stand, each lemonade costs 5.Customersarestandinginaqueuetobuyfromyouandorderoneatatime(intheorderspecifiedbybills).Eachcustomerwillonlybuyonelemonadeandpaywitheithera5, 10,or20 bill. You must provide the correct change to each customer so that the net transaction is that the customer pays $5.

Note that you do not have any change in hand at first.

Given an integer array bills where bills[i] is the bill the ith customer pays, return true if you can provide every customer with the correct change, or false otherwise.

Read more
[LeetCode] Seat Reservation Manager

1845. Seat Reservation Manager

Design a system that manages the reservation state of n seats that are numbered from 1 to n.

Implement the SeatManager class:

  • SeatManager(int n) Initializes a SeatManager object that will manage n seats numbered from 1 to n. All seats are initially available.
  • int reserve() Fetches the smallest-numbered unreserved seat, reserves it, and returns its number.
  • void unreserve(int seatNumber) Unreserves the seat with the given seatNumber.
Read more
[LeetCode] Number of Longest Increasing Subsequence

673. Number of Longest Increasing Subsequence

Given an integer array nums, return the number of longest increasing subsequences.

Notice that the sequence has to be strictly increasing.

Read more
[LeetCode] Count Pairs in Two Arrays

1885. Count Pairs in Two Arrays

Given two integer arrays nums1 and nums2 of length n, count the pairs of indices (i, j) such that i < j and nums1[i] + nums1[j] > nums2[i] + nums2[j].

Return the number of pairs satisfying the condition.

Read more
[LeetCode] Cutting Ribbons

1891. Cutting Ribbons

You are given an integer array ribbons, where ribbons[i] represents the length of the ith ribbon, and an integer k. You may cut any of the ribbons into any number of segments of positive integer lengths, or perform no cuts at all.

  • For example, if you have a ribbon of length 4, you can:
  • Keep the ribbon of length 4,
  • Cut it into one ribbon of length 3 and one ribbon of length 1,
  • Cut it into two ribbons of length 2,
  • Cut it into one ribbon of length 2 and two ribbons of length 1, or
  • Cut it into four ribbons of length 1.

Your goal is to obtain k ribbons of all the same positive integer length. You are allowed to throw away any excess ribbon as a result of cutting.

Return the maximum possible positive integer length that you can obtain k ribbons of, or 0 if you cannot obtain k ribbons of the same length.

Read more
[LeetCode] Palindrome Removal

1246. Palindrome Removal

Given an integer array arr, in one move you can select a palindromic subarray arr[i], arr[i+1], …, arr[j] where i <= j, and remove that subarray from the given array. Note that after removing a subarray, the elements on the left and on the right of that subarray move to fill the gap left by the removal.

Return the minimum number of moves needed to remove all numbers from the array.

Read more
[LeetCode] Closest Binary Search Tree Value

270. Closest Binary Search Tree Value

Given the root of a binary search tree and a target value, return the value in the BST that is closest to the target.

Read more
[LeetCode] Count Ways to Distribute Candies

1692. Count Ways to Distribute Candies

There are n unique candies (labeled 1 through n) and k bags. You are asked to distribute all the candies into the bags such that every bag has at least one candy.

There can be multiple ways to distribute the candies. Two ways are considered different if the candies in one bag in the first way are not all in the same bag in the second way. The order of the bags and the order of the candies within each bag do not matter.

For example, (1), (2,3) and (2), (1,3) are considered different because candies 2 and 3 in the bag (2,3) in the first way are not in the same bag in the second way (they are split between the bags (2) and (1,3)). However, (1), (2,3) and (3,2), (1) are considered the same because the candies in each bag are all in the same bags in both ways.

Given two integers, n and k, return the number of different ways to distribute the candies. As the answer may be too large, return it modulo 109 + 7.

Read more
[LeetCode] Find the Index of the Large Integer

1533. Find the Index of the Large Integer

We have an integer array arr, where all the integers in arr are equal except for one integer which is larger than the rest of the integers. You will not be given direct access to the array, instead, you will have an API ArrayReader which have the following functions:

  • int compareSub(int l, int r, int x, int y): where 0 <= l, r, x, y < ArrayReader.length(), l <= r and x <= y. The function compares the sum of sub-array arr[l..r] with the sum of the sub-array arr[x..y] and returns:
  • 1 if arr[l]+arr[l+1]+…+arr[r] > arr[x]+arr[x+1]+…+arr[y].
  • 0 if arr[l]+arr[l+1]+…+arr[r] == arr[x]+arr[x+1]+…+arr[y].
  • -1 if arr[l]+arr[l+1]+…+arr[r] < arr[x]+arr[x+1]+…+arr[y].
  • int length(): Returns the size of the array.

You are allowed to call compareSub() 20 times at most. You can assume both functions work in O(1) time.

Return the index of the array arr which has the largest integer.

Read more
[Code Jam 2022 Qualification Round 2022] Twisty Little PassagesRead more