Skip to content

Instantly share code, notes, and snippets.

View Ifihan's full-sized avatar
🔧
Work in Progress

Ifihanagbara Olusheye Ifihan

🔧
Work in Progress
View GitHub Profile
@Ifihan
Ifihan / main.md
Created January 13, 2025 08:22
Minimum Length of String After Operations

Question

Approach(es)

My first approach I thought of was brute force. The first step was to pick a letter from the string and check its neighbors. For each character, I’d check whether the same character exists both before and after its current position. Next, I will use a list to track the previous and updated versions of the string.

Finally I update the string dynamically. This involves removing the required characters from the string and adjusting the indices to reflect the changes. Then I’d repeat the process for the remaining characters, continuing until no further operations are possible. This approach could have worked (might run into TLE - O(n^2)) but I thought of something better (I think).

So...

@Ifihan
Ifihan / main.md
Last active January 14, 2025 22:19
Find the Prefix Common Array of Two Arrays

Question

Approach

The goal was to calculate the count of common elements between the two permutations (A) and (B) at or before each index (i), and store these counts in the array (c).

In this approach, I used three sets: seen_a, seen_b, and common. The seen_a set kept track of the elements from (A) that had been encountered up to the current index, while seen_b did the same for (B). The common set was used to store elements that were present in both (A) and (B).

For each index (i), I started by adding the current elements (A[i]) and (B[i]) to their sets (seen_a and seen_b). Then, I checked if (A[i]) was already in seen_b or if (B[i]) was already in seen_a. If either condition was true, I added the element to the common set, signifying that it was present in both arrays.

@Ifihan
Ifihan / main.md
Created January 15, 2025 06:48
Minimize XOR

Question

Approach

My approach was to construct (x) by considering the bits of num1 and num2. First, I counted the number of set bits in num2 using Python's bin(num2).count('1').

To minimize the XOR value, I prioritized aligning (x) with the bits of num1 where possible. Starting from the most significant bit, I iterated through all the bits of num1. Whenever I met a set bit (1) in num1 and still needed more set bits in (x), I copied that bit into (x). This helped (x) resemble num1, minimizing their XOR value.

If the required number of set bits was not achieved after aligning with num1, I switched to filling (x) with additional set bits from the least significant bit upward.

@Ifihan
Ifihan / main.md
Created January 16, 2025 21:36
Bitwise XOR of All Pairings

Question

Approach

I began by exploring the properties of XOR operations. I know that XOR is both commutative and associative, which means the order of operations doesn’t matter. Additionally, XORing any number with (0) leaves the number unchanged, and XORing a number with itself results in (0). These properties became the foundation for my approach.

The insight I found is based on how the XOR operations are distributed in this problem. If (nums1) has (m) elements and (nums2) has (n) elements, then each number in (nums1) is XORed with all (n) numbers in (nums2), and each number in (nums2) is XORed with all (m) numbers in (nums1). Consequently, each number in (nums1) will appear (n) times in the XOR operations, and each number in (nums2) will appear (m) times.

This observation led me to an important optimization. If (n) (the length of (nums2)) is even, XORin

@Ifihan
Ifihan / main.md
Created January 17, 2025 22:36
Neighboring Bitwise XOR

Question

Approach

To solve the problem, I started by assuming two possible values for the first element of the original array: 0 and 1. Using the formula:

original[i+1] = derived[i] ⊕ original[i]

I then iteratively computed the rest of the elements in the original array for each assumption. After constructing the array, I verified the circular condition:

@Ifihan
Ifihan / main.md
Created January 18, 2025 22:25
Minimum Cost to Make at Least One Valid Path in a Grid

Question

Approach

Nothing much to see here. I couldn't think of any proper solution (the question is in the hard difficutly range imo) so I checked the editorial.

Everything can be found there. I followed the Dynamic Programming solution

image
@Ifihan
Ifihan / main.md
Created January 19, 2025 22:22
Trapping Rain Water II

Question

Approach

Again, nothing much to see here. I couldn't think of any proper solution (the question is in the hard difficutly range imo) so I checked the editorial.

Everything can be found there.

image
@Ifihan
Ifihan / main.md
Created January 20, 2025 22:54
First Completely Painted Row or Column

Question

Approach

I started with using a hashmap (position) to store the mappings. Then to track the progress of painting, I maintained two counting arrays: one to track the number of painted cells in each row (row_count) and another for each column (col_count). As I iterated through the array, I used the position map to identify the cell to paint and incremented the counts for the respective row and column.

As soon as a row or column becomes fully painted, I returned the current index, as this was guaranteed to be the earliest instance.

Implementation

@Ifihan
Ifihan / main.md
Created January 21, 2025 22:50
Grid Game

Question

Approach

I began by analyzing the rules of the game and the constraints involving two robots navigating a 2xN grid. The key observation was that the first robot, by setting all cells on its path to zero, directly influences the points available for the second robot to collect. Therefore, the first robot's primary goal is to strategically block high-value paths to minimize the points left for the second robot. On the other hand, the second robot will always choose its path to maximize the points it collects from the remaining grid.

I used prefix sums. This allow for fast computation of the total points along any subpath in the grid. With prefix sums, it becomes straightforward to determine the points remaining in the top and bottom rows for any split point where the first robot might switch from the top row to the bottom row. This avoids the need for recalculating sums repeatedly, which is critical given the const

@Ifihan
Ifihan / main.md
Created January 22, 2025 22:18
Map of Highest Peak

Question

Approach

To solve the problem, I used the Breadth-First Search (BFS) approach. I started by initializing a height matrix of the same size as the input, with all cells set to (-1) to represent unvisited cells. All water cells (where isWater[i][j] == 1) were set to a height of 0 and added to a queue, serving as the starting points for height propagation.

Using BFS, I processed each cell in the queue, exploring its four neighbors (north, south, east, west). For each neighbor, if it was within bounds and unvisited, I set its height to the current cell's height plus 1 and added it to the queue. This ensured that the height differences between adjacent cells never exceeded 1. The BFS continued until all reachable cells had been assigned a height.

By starting from water cells and propagating outward, the algorithm naturally maximized the heights of the land cells while satisfying the constraints. Once t