We are given a list schedule of employees, which represents the working time for each employee.
Each employee has a list of non-overlapping Intervals, and these intervals are in sorted order.
Return the list of finite intervals representing common, positive-length free time for all employees, also in sorted order.
(Even though we are representing Intervals in the form [x, y], the objects inside are Intervals, not lists or arrays. For example, schedule[0][0].start = 1, schedule[0][0].end = 2, and schedule[0][0][0] is not defined). Also, we wouldn't include intervals like [5, 5] in our answer, as they have zero length.
Example 1:
<pre>Input: schedule = [[[1,2],[5,6]],[[1,3]],[[4,10]]]
Output: [[3,4]]
Explanation: There are a total of three employees, and all common
free time intervals would be [-inf, 1], [3, 4], [10, inf].
We discard any intervals that contain inf as they aren't finite.
</pre>
<div><p>Given an array of integers arr, find the sum of <code>min(b)</code>, where <code>b</code> ranges over every (contiguous) subarray of <code>arr</code>. Since the answer may be large, return the answer <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong>Example 1:</strong></p>
<pre><strong>Input:</strong> arr = [3,1,2,4]
<strong>Output:</strong> 17
<strong>Explanation:</strong>
Subarrays are [3], [1], [2], [4], [3,1], [1,2], [2,4], [3,1,2], [1,2,4], [3,1,2,4].
Minimums are 3, 1, 2, 4, 1, 1, 2, 1, 1, 1.
Sum is 17.
</pre>
defsumSubarrayMins(self, arr: List[int]) -> int: arr.insert(0, -sys.maxsize) arr.append(-sys.maxsize) stack, res = [], 0 for i inrange(len(arr)): while stack and arr[i] < arr[stack[-1]]: prev_idx = stack.pop() res += arr[prev_idx] * (prev_idx - stack[-1]) * (i - prev_idx) res = res % (pow(10, 9) + 7) stack.append(i) return res
You are given a 0-indexed array of n integers arr.
The interval between two elements in arr is defined as the absolute difference between their indices. More formally, the interval between arr[i] and arr[j] is |i - j|.
Return an arrayintervalsof lengthnwhereintervals[i]is the sum of intervals betweenarr[i]and each element inarrwith the same value asarr[i].
defgetDistances(self, arr: List[int]) -> List[int]: val_to_index = collections.defaultdict(list) res = [0] * len(arr) for i inrange(len(arr)): val_to_index[arr[i]].append(i) for val, indices in val_to_index.items(): prefix, suffix = [0], [0] for i inrange(1, len(indices)): prefix.append(prefix[-1] + i * abs(indices[i] - indices[i - 1])) for i inrange(len(indices) - 2, -1, -1): suffix.append(suffix[-1] + (len(indices) - i - 1) * abs(indices[i] - indices[i + 1])) for i inrange(len(indices)): res[indices[i]] = prefix[i] + suffix[len(indices) - i - 1] return res
Design a data structure that will be initialized with a string array, and then it should answer queries of the shortest distance between two different strings from the array.
Implement the WordDistance class:
WordDistance(String[] wordsDict) initializes the object with the strings array wordsDict.
int shortest(String word1, String word2) returns the shortest distance between word1 and word2 in the array wordsDict.
def__init__(self, wordsDict: List[str]): self.word_to_indices = collections.defaultdict(list) for i inrange(len(wordsDict)): self.word_to_indices[wordsDict[i]].append(i)
defshortest(self, word1: str, word2: str) -> int: indices1 = self.word_to_indices[word1] indices2 = self.word_to_indices[word2] i, j, res = 0, 0, float('inf') while i < len(indices1) and j < len(indices2): res = min(res, abs(indices1[i] - indices2[j])) if indices1[i] <= indices2[j]: i += 1 else: j += 1 return res
LFUCache(int capacity) Initializes the object with the capacity of the data structure.
int get(int key) Gets the value of the key if the key exists in the cache. Otherwise, returns -1.
void put(int key, int value) Update the value of the key if present, or inserts the key if not already present. When the cache reaches its capacity, it should invalidate and remove the least frequently used key before inserting a new item. For this problem, when there is a tie (i.e., two or more keys with the same frequency), the least recently usedkey would be invalidated.
To determine the least frequently used key, a use counter is maintained for each key in the cache. The key with the smallest use counter is the least frequently used key.
When a key is first inserted into the cache, its use counter is set to 1 (due to the put operation). The use counter for a key in the cache is incremented either a get or put operation is called on it.
The functions get and put must each run in O(1) average time complexity.
Explanation
// cnt(x) = the use counter for key x
// cache=[] will show the last used order for tiebreakers (leftmost element is most recent)
LFUCache lfu = new LFUCache(2);
lfu.put(1, 1); // cache=[1,_], cnt(1)=1
lfu.put(2, 2); // cache=[2,1], cnt(2)=1, cnt(1)=1
lfu.get(1); // return 1
// cache=[1,2], cnt(2)=1, cnt(1)=2
lfu.put(3, 3); // 2 is the LFU key because cnt(2)=1 is the smallest, invalidate 2.
// cache=[3,1], cnt(3)=1, cnt(1)=2
lfu.get(2); // return -1 (not found)
lfu.get(3); // return 3
// cache=[3,1], cnt(3)=2, cnt(1)=2
lfu.put(4, 4); // Both 1 and 3 have the same cnt, but 1 is LRU, invalidate 1.
// cache=[4,3], cnt(4)=1, cnt(3)=2
lfu.get(1); // return -1 (not found)
lfu.get(3); // return 3
// cache=[3,4], cnt(4)=1, cnt(3)=3
lfu.get(4); // return 4
// cache=[3,4], cnt(4)=2, cnt(3)=3
</pre>
Constraints:
0 <= capacity <= 10<sup>4</sup>
0 <= key <= 10<sup>5</sup>
0 <= value <= 10<sup>9</sup>
At most 2 * 10<sup>5</sup> calls will be made to get and put.
defget_lru_node(self): head = self.freq_to_head[self.min_freq] return head.next
defadd_to_tail(self, node): head = self.freq_to_head[node.freq] predecessor, successor = head.prev, head predecessor.next, successor.prev = node, node node.prev, node.next = predecessor, successor
defget_lfu_node(self): it = self.tail.prev min_freq = it.freq while it != self.head and it.freq == min_freq: it = it.prev # remember prev not next return it.next
defmove_up(self, node): node.freq += 1 it = node.prev self.remove(node) while it != self.head and it.freq < node.freq: # remember <= it = it.prev # remember prev not next predecessor, successor = it, it.next predecessor.next, successor.prev = node, node node.prev, node.next = predecessor, successor
defadd_to_tail(self, node): predecessor, successor = self.tail.prev, self.tail predecessor.next, successor.prev = node, node node.prev, node.next = predecessor, successor # remember