Given an array of distinct integers arr, find all pairs of elements with the minimum absolute difference of any two elements.
Return a list of pairs in ascending order(with respect to pairs), each pair [a, b] follows
a, b are from arra < b b - a equals to the minimum absolute difference of any two elements in arr
Example 1:
Input: arr = [4,2,1,3] Output: [[1,2],[2,3],[3,4]] Explanation: The minimum absolute difference is 1. List all pairs with difference equal to 1 in ascending order.
Design your implementation of the circular queue. The circular queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle, and the last position is connected back to the first position to make a circle. It is also called “Ring Buffer”.
One of the benefits of the circular queue is that we can make use of the spaces in front of the queue. In a normal queue, once the queue becomes full, we cannot insert the next element even if there is a space in front of the queue. But using the circular queue, we can use the space to store new values.
Implement the MyCircularQueue class:
MyCircularQueue(k) Initializes the object with the size of the queue to be k.
int Front() Gets the front item from the queue. If the queue is empty, return -1. int Rear() Gets the last item from the queue. If the queue is empty, return -1.
boolean enQueue(int value) Inserts an element into the circular queue. Return true if the operation is successful. boolean deQueue() Deletes an element from the circular queue. Return true if the operation is successful.
boolean isEmpty() Checks whether the circular queue is empty or not. boolean isFull() Checks whether the circular queue is full or not.
You must solve the problem without using the built-in queue data structure in your programming language.
def__init__(self, k: int): self.data = [0] * k self.start = 0# starting point of data self.end = 0# ending point + 1 of data self.size = k self.is_full = False
Special binary strings are binary strings with the following two properties:
The number of 0‘s is equal to the number of 1‘s.
Every prefix of the binary string has at least as many 1‘s as 0‘s.
You are given a special binary string s.
A move consists of choosing two consecutive, non-empty, special substrings of s, and swapping them. Two strings are consecutive if the last character of the first string is exactly one index before the first character of the second string.
Return the lexicographically largest resulting string possible after applying the mentioned operations on the string.
Example 1:
Input: s = “11011000” Output: “11100100” Explanation: The strings “10” [occuring at s[1]] and “1100” [at s[3]] are swapped. This is the lexicographically largest string possible after some number of swaps.
Example 2:
Input: s = “10” Output: “10”
Constraints:
1 <= s.length <= 50s[i] is either '0' or '1'. * s is a special binary string.
题目大意:
Special string由0和1组成,数目一样且每个前缀1的数目大于等于0的数目。可以交换其内部的子特别字符串,让其数值最大
defnumSubarraysWithSum(self, nums: List[int], goal: int) -> int: presum = [0] for i inrange(len(nums)): presum.append(presum[-1] + nums[i]) val_to_freq, res = collections.defaultdict(int), 0 for i inrange(len(presum)): if presum[i] - goal in val_to_freq: res += val_to_freq[presum[i] - goal] val_to_freq[presum[i]] += 1#attn return res
算法分析:
时间复杂度为O(n),空间复杂度O(n)
算法II解题思路Sliding Window:
此法虽然空间复杂度较优,但较难想。求字串的和可以考虑用Two pointers两边夹。这里求所有可能性,比如0011100, goal=3, 必须包括前缀0和后缀0,所以用Two pointers最长串的模板
类似于LeetCode 340 Longest Substring with At Most K Distinct Characters,不满足条件为窗口和大于goal,所以满足条件为小于等于goal(类似于at most k)。
所以要用at_most(goal) - at_most(goal - 1)才能得到最后结果。
计算的时候,是i-left+1代表窗口和<=subgoal的以nums[i]为结尾的子数组个数,比如10101, i指向最后一个1, left指向第一个0, res是4个,对应1, 01, 101, 0101
注意事项:
模板满足条件在内循环外,所以计算结果在内循环结束后。
subgoal如果小于0,返回0。比如goal为0,nums=[0,0]
Python代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
defnumSubarraysWithSum(self, nums: List[int], goal: int) -> int: defat_most(subgoal): if subgoal < 0: #attn return0 window_sum, left, res = 0, 0, 0 i = 0 for i inrange(len(nums)): window_sum += nums[i] while window_sum > subgoal: window_sum -= nums[left] left += 1 res += i - left + 1# ending with nums[i] return res
Koko loves to eat bananas. There are n piles of bananas, the i<sup>th</sup> pile has piles[i] bananas. The guards have gone and will come back in h hours.
Koko can decide her bananas-per-hour eating speed of k. Each hour, she chooses some pile of bananas and eats k bananas from that pile. If the pile has less than k bananas, she eats all of them instead and will not eat any more bananas during this hour.
Koko likes to eat slowly but still wants to finish eating all the bananas before the guards return.
Return the minimum integerksuch that she can eat all the bananas withinhhours.
Example 1:
<pre>Input: piles = [3,6,7,11], h = 8
Output: 4
</pre>
Example 2:
<pre>Input: piles = [30,11,23,4,20], h = 5
Output: 30
</pre>
Example 3:
<pre>Input: piles = [30,11,23,4,20], h = 6
Output: 23
</pre>