Algorithm: Leetcode Contest 371
From today I will try to participate the Leetcode weekly contest and share my solution here
2933 High-Access Employees
Difficulty: Medium
You are given a 2D 0-indexed array of strings, access_times
, with size n. For each i where 0 <= i <= n - 1, access_times[i][0]
represents the name of an employee, and access_times[i][1]
represents the access time of that employee. All entries in access_times are within the same day.
The access time is represented as four digits using a 24-hour time format, for example, “0800” or “2250”.
An employee is said to be high-access if he has accessed the system three or more times within a one-hour period.
Times with exactly one hour of difference are not considered part of the same one-hour period. For example, “0815” and “0915” are not part of the same one-hour period.
Access times at the start and end of the day are not counted within the same one-hour period. For example, “0005” and “2350” are not part of the same one-hour period.
Return a list that contains the names of high-access employees with any order you want.
Example
Input: access_times = [["a","0549"],["b","0457"],["a","0532"],["a","0621"],["b","0540"]]
Output: ["a"]
Explanation: "a" has three access times in the one-hour period of [05:32, 06:31] which are 05:32, 05:49, and 06:21.
But "b" does not have more than two access times at all.
So the answer is ["a"].
Solution
Introspect
- I was blocked at function
has3TimesIn1Hour
- edge case understanding is not clear
- moving right boundary and updating the windows state is not aligned perfectly
- the ways to traverse map
Map.Entry<String, List<String>> entry: accessTimeMap.entrySet()
- convert list to array
String[] arr = access_times.toArray(new String[0]);
2934 Minimum Operations to Maximize Last Elements in Arrays
You are given two 0-indexed integer arrays, nums1
and nums2
, both having length n.
You are allowed to perform a series of operations (possibly none).
In an operation, you select an index i in the range [0, n - 1] and swap the values of nums1[i]
and nums2[i]
.
Your task is to find the minimum number of operations required to satisfy the following conditions:
nums1[n - 1] is equal to the maximum value among all elements of nums1, i.e., nums1[n - 1] = max(nums1[0], nums1[1], …, nums1[n - 1]). nums2[n - 1] is equal to the maximum value among all elements of nums2, i.e., nums2[n - 1] = max(nums2[0], nums2[1], …, nums2[n - 1]). Return an integer denoting the minimum number of operations needed to meet both conditions, or -1 if it is impossible to satisfy both conditions.
Example
Input: nums1 = [1,2,7], nums2 = [4,5,3]
Output: 1
Explanation: In this example, an operation can be performed using index i = 2.
When nums1[2] and nums2[2] are swapped, nums1 becomes [1,2,3] and nums2 becomes [4,5,7].
Both conditions are now satisfied.
It can be shown that the minimum number of operations needed to be performed is 1.
So, the answer is 1.
Solution
the solution I come up by myself
seems it’s good enough haha, don’t find better solution
Introspect
- don’t try to come up with perfect solution, figure out the most basic and correct version
- when encountering the logic operations, try both way, either remove the invalid cases first, or either confirm valid case first.
- when make assumption, and add another assumption, have to figure out whether the new assumption is valid under previous assumption