Array
66. Plus One
881. Boats to Save People
448. Find All Numbers Disappeared in an Array
280. Wiggle Sort
2016. Maximum Difference Between Increasing Elements
class Solution {
public:
int maximumDifference(vector<int>& nums) {
int minVal = std::numeric_limits<int>::max();
int maxDiff = -1;
for (int n : nums) {
if (n <= minVal) {
minVal = n;
} else {
maxDiff = std::max(maxDiff, n - minVal);
}
}
return maxDiff;
}
};Boyer-Moore Voting Algorithm. Calculate the candidate and it is vote (count).
vote + 1: when the candidate == current number
vote - 1: when the candidate != current number
If the vote is reduced to 0, reset candidate to the current number.
Same as 169, instead of using one candidate, use two candidates and maintain their count. Output the candidate which has more than length / 3 votes.
Count increments and decrements. Reset when encounter flat.
Sort T: O(nlogn)
Intelligent Sequence Building:
Add all numbers to a set. Loop over all numbers, search if the current number is a start of the sequence.
if (!set.contains(n - 1))If yes, keep search and build the sequence. T: O(n)
Same as Find LinkedList cycle. Use fast and slow pointer to move in the same array. Reset slow and return when they meet again.
Since the array elements in range 1 ≤ a[i] ≤ n (n = size of array). Each element has corresponding pos may be once or twice. We mark the number at visited position negative, if we encounter this negative element twice, then this is a result.
Once all numbers are made positive, if any number is found in range [1,N] then attach -ve sign to the corresponding index. So for 1, 0th element becomes -ve, for 2 it is 1st considering 0 based index.
Use a count to determine the length of the turbulent subarray. Flip count every loop by using count *= -1. Detect if the pattern match the current array or start a new one. Otherwise reset count to 0. For example, [4,2,10,7,8].
When
A[i] > A[i + 1]andcount > 0thencount += 1otherwisecount = 1.When
A[i] < A[i + 1]andcount < 0thencount -= 1otherwisecount = -1.
Maintain the max result by update the absolute value of count.
Use HashMap to store a list of timestamp-website pairs for each user. Sort the list, create pattern for each combination. Use a hashset to remove duplicate and a frequency hashmap to find the most frequent pattern.
Find a starting number and check if there is any numbers which can make a straight from the numbers. The starting point is the smallest number exists.
PriorityQueue
2. Sort + map
1296. Divide Array in Sets of K Consecutive Numbers
3434. Maximum Frequency After Subarray Operation
1752. Check if Array Is Sorted and Rotated
set max/min position and update dynamically
use left index to bound the left range which within the min/max
add to result, make sure no -ve values:
2966. Divide Array Into Arrays With Max Difference
sort and detect
594. Longest Harmonious Subsequence
Last updated
Was this helpful?