Posts

Binary Search to Find Next Greater Element IV

Image
The problem sometimes with BS is to handle the corner cases. In the problem down below, corner cases in my solutions happen when the index k is out of range or when we find an exact match (handled in the return statement). Code is down below, cheers, ACC. Delayed Count of Equal Elements - LeetCode You are given an integer array  nums  of length  n  and an integer  k . For each index  i , define the  delayed count  as the number of indices  j  such that: i + k < j <= n - 1 , and nums[j] == nums[i] Return an array  ans  where  ans[i]  is the  delayed count  of index  i .   Example 1: Input:   nums = [1,2,1,1], k = 1 Output:   [2,0,0,0] Explanation: i nums[i] possible  j nums[j] satisfying nums[j] == nums[i] ans[i] 0 1 [2, 3] [1, 1] [2, 3] 2 1 2 [3] [1] [] 0 2 1 [] [] [] 0 3 1 [] [] [] 0 Thus,  ans = [2, 0, 0, 0] ​​​​​​​. Example 2: Input:   nums = [3,1,3,1], k = 0 Outpu...

Prefix Count: Hashtable

Image
Usually there are two approaches to deal with prefixes (or suffixes) in strings: either using a Trie, or a Hashtable. The former saves space while the latter consumes more but it is an easier implementation. Both have the relative save time complexity. For this problem below, given the small constraints, I decided to go with a Hashtable. No caveats, just follow the instructions given in the problem description. Cheers, ACC. Number of Prefix Connected Groups - LeetCode You are given an array of strings  words  and an integer  k . Two words  a  and  b  at  distinct indices  are  prefix -connected  if  a[0..k-1] == b[0..k-1] . A  connected group  is a set of words such that each pair of words is prefix-connected. Return the  number of connected groups  that contain  at least  two words, formed from the given words. Note: Words with length less than  k  cannot join any group and are ignored. ...