Posts

Showing posts from December, 2023

Last one of 2023, with a HashTable

Image
For the last one in 2023, a medium-level but actually simple with a brute-force in N^2 (N=50) and use of a HashTable to keep track of the count per special string. Code is down below, Happy New Year! ACC Find Longest Special Substring That Occurs Thrice I - LeetCode 2981. Find Longest Special Substring That Occurs Thrice I Medium 50 2 Add to List Share You are given a string  s  that consists of lowercase English letters. A string is called  special  if it is made up of only a single character. For example, the string  "abc"  is not special, whereas the strings  "ddd" ,  "zz" , and  "f"  are special. Return  the length of the  longest special substring  of  s   which occurs  at least thrice ,  or  -1  if no special substring occurs at least thrice . A  substring  is a contiguous  non-empty  sequence of characters within a string.   Example 1: Input: s = "aaaa" Output: 2 Explanation: The longest special substring which occurs thrice i

Very interesting Tree problem

Image
This is one of the most interesting Tree problems that I have seen in a long time. In spite of the high number of thumbs down, I think this problem exercises multiple concepts of algorithms and data structures. The first point is that it limits the number of nodes in the tree to a very small number (2^5 - 1). With that, at least in my solution, I start by creating the full complete tree. In the process of creating it, have a hash table mapping a key to each node. The key can be calculated as a unique function from {depth, position} to a node. To make it unique, just multiply depth by a large number, and add position. Once you have the map, go thru the input and calculate the same key, find the node and assign the value. At this point there is still a problem where there might be multiple unused nodes. Write a DFS function to remove the unused nodes. Finally, create a function to calculate the sum of all paths, again using DFS. Code is down below, Happy Holidays!!! ACC. Path Sum IV - Le

Sliding Window Technique - Part 11

Image
There are two interesting facts about this variant of the sliding window technique: first, the right pointer always moves in every single iteration, and second, the movement of the left pointer is explicitly constrained by the right pointer. Code is down below, cheers, ACC. Length of Longest Subarray With at Most K Frequency - LeetCode 2958. Length of Longest Subarray With at Most K Frequency Medium 119 4 Add to List Share You are given an integer array  nums  and an integer  k . The  frequency  of an element  x  is the number of times it occurs in an array. An array is called  good  if the frequency of each element in this array is  less than or equal  to  k . Return  the length of the  longest   good  subarray of   nums . A  subarray  is a contiguous non-empty sequence of elements within an array.   Example 1: Input: nums = [1,2,3,1,2,3,1,2], k = 2 Output: 6 Explanation: The longest possible good subarray is [1,2,3,1,2,3] since the values 1, 2, and 3 occur at most twice in this su