Another Casual Coder
81 FOLLOWERS
This is what I do after the kids go to bed and before Forensic Files.
I casually write code.
Another Casual Coder
5d ago
Priority Queues can be used as a standard NLogN sorting mechanism. Usually it isn't used as sorting due to the extra space required for the heap, but in terms of execution time it is as efficient as MergeSort for the worst case and even better than QuickSort on some cases. If you have a handy implementation of PQueue, don't be afraid of using it for sorting.
Problem below requires a sorting of the elements based on the X-coordinate, followed by a linear scanning counting the number of rectangles. PQueue comes to the rescue here. Code is down below, cheers, ACC.
Minimum Rectangles to Cover P ..read more
Another Casual Coder
1w ago
It has been a while since I had solved a PE problem. This one is simple, difficulty is 5%. As always I cannot share the solution or full code due to the rules of engagement with PE, but I can share the overall approach:
1/ Figure out a way to quickly find the Pisano Period. Hint: AI is your friend, so is Google or Bing
2/ Brute force it up to 1B. Takes a couple of minutes
Cheers,
ACC
#853 Pisano Periods 1 - Project Euler ..read more
Another Casual Coder
1w ago
This problem requires a solution similar to the longest increasing subsequence one. Basically on each step you either reset the current streak or increments it, adding the value to the return value. Code is down below, cheers, ACC.
Count Alternating Subarrays - LeetCode
3101. Count Alternating Subarrays
Medium
1507Add to ListShare
You are given a binary array nums.
We call a subarray alternating if no two adjacent elements in the subarray have the same value.
Return the number of alternating subarrays in nums.
  ..read more
Another Casual Coder
3w ago
This is an interesting problem: the key here is to keep track of the frequency within the Y, frequency outside of the Y, and then at this point we can try "all possibilities". But all possibilities here means actually a constant time. In the code below, you have 3*3*3 = 27 iterations. Just be careful when you see nested loops, they may still be cnstant time. Code is down below, cheers, ACC.
Minimum Operations to Write the Letter Y on a Grid - LeetCode
You are given a 0-indexed n x n grid where n is odd, and grid[r][c] is 0, 1, or 2.
We say tha ..read more
Another Casual Coder
1M ago
You can use LINQ to get the count of instances in one line. Call it N. Then a bit of math: you're looking for combination of N, 2 by 2, which would be the formula N! / (2! * (N-2)!) and if you simplify you get N*(N-1)/2. You also have to add N to account for single-letters strings. Code is down below, cheers, ACC.
Count Substrings Starting and Ending with Given Character - LeetCode
You are given a string s and a character c. Return the total number of substrings of s that start and end with c.
public long CountSubstrings(string s, char c)
{
long n = s.Where(k => k == c).Count ..read more
Another Casual Coder
1M ago
In this problem we need to be careful to not fall into the trap of decrementing each element after each step, otherwise you'll get into an N^2 state (and N=10^5, hence intractable). What can be done is sorting, and then use a decrement variable to keep track of the amount to decrement the happiness to. Leads to nLogn. code is down below, cheers, ACC.
Maximize Happiness of Selected Children - LeetCode
You are given an array happiness of length n, and a positive integer k.
There are n children standing in a queue, where the ith child has hap ..read more
Another Casual Coder
1M ago
Another problem involving grid and hash tables. Relatively simple, just keeping track of the numbers already used, and the mapping letter --> number. Complexity-wise, O(|board| * |pattern|) or in other words, 50^4, or ~6M, which is very tractable. Code is down below, cheers, ACC.
Match Alphanumerical Pattern in Matrix I - LeetCode
You are given a 2D integer matrix board and a 2D character matrix pattern. Where 0 <= board[r][c] <= 9 and each element of pattern is either a digit or a lowercase English letter.
Your task is to find a
submatrix
&nbs ..read more
Another Casual Coder
1M ago
This is a simple one where you don't even need to allocate extra memory - use the grid itself as the DP storage, assuming that you don't need to keep the elements of grid intact. Code is down below, cheers, ACC.
Count Submatrices with Top-Left Element and Sum Less Than k - LeetCode
You are given a 0-indexed integer matrix grid and an integer k.
Return the number of
submatrices
that contain the top-left element of the grid, and have a sum less than or equal to k.
Example 1:
Input: grid = [[7,6,3],[6,6,1]], k = 18
Output ..read more
Another Casual Coder
1M ago
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
25713Add to ListShare
You are given an integer array nums and a positive integer k.
Return the number of subarrays where the maximum element of nums ap ..read more
Another Casual Coder
2M ago
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
945Add to ListShare
You are given two arrays with positive integers arr1 and arr2.
A prefix of a positive integer is an integer formed by one or m ..read more