Solution #2379 - Mridul/Edited - 08/03/2025#23
Solution #2379 - Mridul/Edited - 08/03/2025#23mrid88 wants to merge 3 commits intoDijkstra-Edu:masterfrom
Conversation
JRS296
left a comment
There was a problem hiding this comment.
Kindly improve the explanation.md files (Primarily the formatting using Markdown, as well as explanation of the Time and Space Complexity)
JRS296
left a comment
There was a problem hiding this comment.
Kindly make the requested changes
| n: The length of the input string blocks. | ||
|
|
||
| b: A counter to keep track of the number of black blocks within a sliding window. | ||
|
|
||
| Max: To store the maximum number of black blocks found in any sliding window of length k. | ||
|
|
||
| This loop counts the number of black blocks ('B') in the first window of length k and assigns it to both b and Max. | ||
|
|
||
| This loop slides the window across the string from index 0 to n - k - 1. | ||
|
|
||
| It decreases the count b if the block at the start of the window is 'B'. | ||
|
|
||
| It increases the count b if the block at the end of the window is 'B'. | ||
|
|
||
| Updates Max to the maximum value between the current Max and b. | ||
|
|
||
| If Max becomes equal to or greater than k, it means we already have k consecutive black blocks, and hence, no recoloring is needed. The function returns 0. | ||
|
|
||
| If the loop completes without finding k consecutive black blocks, the function returns the difference between k and Max, which represents the minimum number of recolors needed. No newline at end of file |
There was a problem hiding this comment.
Can be much better! utilize Markdown
| } | ||
| } | ||
|
|
||
| Max = b; |
There was a problem hiding this comment.
do not use Capital letters for Variable names. This should be 'max'
| @@ -0,0 +1,30 @@ | |||
| class Solution { | |||
| public int minimumRecolors(String blocks, int k) { | |||
| int n = blocks.length(), b = 0, Max; | |||
There was a problem hiding this comment.
Ideally try to have variable names one line after the other, so that it is more readable
| private static boolean[] isPrime = new boolean[1000001]; This boolean array is used to mark prime numbers up to 1000000. The size of the array is 1000001 to include the number 1000000. | ||
|
|
||
| Static Block: | ||
| The static block is used to initialize the isPrime array and precompute the prime numbers. | ||
|
|
||
| Arrays.fill(isPrime, true); This method sets all elements of the isPrime array to true, assuming all numbers are prime initially. | ||
|
|
||
| precompute(); This method is called to mark non-prime numbers in the isPrime array. | ||
|
|
||
| Method: precompute() | ||
| This method uses the Sieve of Eratosthenes algorithm to mark non-prime numbers in the isPrime array. | ||
|
|
||
| isPrime[0] = isPrime[1] = false; Numbers 0 and 1 are not prime. | ||
|
|
||
| A loop is used to iterate from 2 to sqrt(1000000). | ||
|
|
||
| If isPrime[i] is true, all multiples of i are marked as false (not prime) starting from i*i. | ||
|
|
||
| Method: closestPrimes(int left, int right) | ||
| This method finds the closest prime numbers within the range [left, right]. | ||
|
|
||
| int prev = -1, minDiff = Integer.MAX_VALUE; Variables to track the previous prime number and the minimum difference between consecutive primes. | ||
|
|
||
| int num1 = -1, num2 = -1; Variables to store the closest prime numbers. | ||
|
|
||
| A loop iterates through the range [left, right]. | ||
|
|
||
| If isPrime[i] is true, the number i is a prime. | ||
|
|
||
| If prev is not -1 and the difference (i - prev) is less than minDiff, update minDiff and set num1 to prev and num2 to i. | ||
|
|
||
| Update prev to i. | ||
|
|
||
| If no prime numbers are found, return new int[]{-1, -1}. Otherwise, return the closest prime numbers num1 and num2. No newline at end of file |
There was a problem hiding this comment.
You don't have to use the actual implemnentations to explain everything. Instead of private statuc boolean[], you can just say that the static boolean array.
Try to think of how you would explain this to someone during an interview.
added both the files, please check