Posts

Showing posts from February, 2024

Sliding Window Technique - Part 12

Image
The control pointer here is the left (slow) pointer. There is probably some optimization that can be done to jump the left pointer far ahead if needed, but even with the slow movement one can achieve O(N) (in 2*N). Code is down below, cheers, ACC. Count Subarrays Where Max Element Appears at Least K Times - LeetCode 2962. Count Subarrays Where Max Element Appears at Least K Times Medium 257 13 Add to List Share You are given an integer array  nums  and a  positive  integer  k . Return  the number of subarrays where the  maximum  element of  nums  appears  at least   k  times in that subarray. A  subarray  is a contiguous sequence of elements within an array.   Example 1: Input: nums = [1,3,2,3,3], k = 2 Output: 6 Explanation: The subarrays that contain the element 3 at least 2 times are: [1,3,2,3], [1,3,2,3,3], [3,2,3], [3,2,3,3], [2,3,3] and [3,3]. Example 2: Input: nums = [1,4,2,1], k = 3 Output: 0 Explanation: No subarray contains the element 4 at least 3 times.   Constraints:

Trie-Trie-Trie!!! - Part 4

Image
A super simple implementation of a Trie: a class with just a hash table as the children. The code for the prefix len becomes super easy (two lines). A little slow probably because of the conversion to strings, can be sped up by using just numerical manipulation. Code is down below, cheers, ACC. Find the Length of the Longest Common Prefix - LeetCode 3043. Find the Length of the Longest Common Prefix Medium 94 5 Add to List Share You are given two arrays with  positive  integers  arr1  and  arr2 . A  prefix  of a positive integer is an integer formed by one or more of its digits, starting from its  leftmost  digit. For example,  123  is a prefix of the integer  12345 , while  234  is  not . A  common prefix  of two integers  a  and  b  is an integer  c , such that  c  is a prefix of both  a  and  b . For example,  5655359  and  56554  have a common prefix  565  while  1223  and  43456   do not  have a common prefix. You need to find the length of the  longest common prefix  between all

Miller-Rabin Primality Test and Caching

Image
Miller-Rabin remains one of the best practical primality testing available, despite the famous Primes is in P  from 2003. Miller-Rabin is also a polynomial time algorithm but it is non-deterministic. However, given enough "iterations and witnesses", the non-deterministic aspect is so small that for all practical purposes it becomes deterministic. The implementation below for example has a probability of producing a wrong primality test of 9 x 10 ^ (-58) percent. The problem doesn't really need Miller-Rabin since the numbers are small but it is a good example of how to use it (together with some caching ) to solve problems involving prime numbers in a very fast fashion. Code is down below, cheers, ACC. Most Frequent Prime - LeetCode 3044. Most Frequent Prime Medium 37 41 Add to List Share You are given a  m x n   0-indexed  2D   matrix  mat . From every cell, you can create numbers in the following way: There could be at most  8  paths from the cells namely: east, south-e