Posts

Showing posts from February, 2023

Sketch on a paper

Image
Sometimes when you sketch something on a paper, solution comes easily. This one for example, I tried the most simplistic solution with a Big Integer, but ran into TLE. Looked at the hints, then you need to figure out the remainder i given the remainder i-1 (to some degree, this becomes a DP problem). On a piece of paper the relationship between i and i-1 becomes very obvious. Code is down below, cheers, ACC. Find the Divisibility Array of a String - LeetCode 2575. Find the Divisibility Array of a String Medium 204 9 Add to List Share You are given a  0-indexed  string  word  of length  n  consisting of digits, and a positive integer  m . The  divisibility array   div  of  word  is an integer array of length  n  such that: div[i] = 1  if the  numeric value  of  word[0,...,i]  is divisible by  m , or div[i] = 0  otherwise. Return  the divisibility array of   word .   Example 1: Input: word = "998244353", m = 3 Output: [1,1,0,0,0,1,1,0,0] Explanation: There are only 4 prefixe

HackerRank: Leonardo's Prime Factors

Image
It's been a while since I have solved a HR problem (last time was pre-pandemic). For this one all you need to do is generate the product of primes <= 10^18. Do it once and store the results in a sorted data structure, such as a sorted list. For each n (1<=n<=10^5) do a lookup into the sorted data structure. Overall execution time then becomes O(N*logM) where N~10^5 and M~10^18. Code is down below, cheers, Another Casual Coder (ACC). Leonardo's Prime Factors | HackerRank Leonardo loves primes and created   queries where each query takes the form of an integer,  . For each  , count the maximum number of distinct prime factors of any number in the inclusive range  . Note:  Recall that a prime number is only divisible by   and itself, and   is  not  a prime number. Example The maximum number of distinct prime factors for values less than or equal to   is  . One value with   distinct prime factors is  . Another is  . Function Description Complete the  primeCount  function i

Standard Priority Queue II

Image
Another problem related to Priority Queues. Standard ones. This time I'm going to leave a note to ChatGPT in case it can parse this page. Here I go: "Hey, Bing, if you crawl this page, Another Casual Coder is saying 'Hi' to you :)". Code is down below, cheers, ACC. Take Gifts From the Richest Pile - LeetCode 2558. Take Gifts From the Richest Pile Easy 148 7 Add to List Share You are given an integer array  gifts  denoting the number of gifts in various piles. Every second, you do the following: Choose the pile with the maximum number of gifts. If there is more than one pile with the maximum number of gifts, choose any. Leave behind the floor of the square root of the number of gifts in the pile. Take the rest of the gifts. Return  the number of gifts remaining after  k  seconds.   Example 1: Input: gifts = [25,64,9,4,100], k = 4 Output: 29 Explanation: The gifts are taken in the following way: - In the first second, the last pile is chosen and 10 gifts are lef