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 23, 2025 21:55
Count Servers that Communicate

Question

Approach

To solve the problem, I used an approach based on counting the number of servers in each row and column. First, I traversed the grid to calculate two arrays: row_count and col_count. row_count[i] stores the number of servers in row i, and col_count[j] stores the number of servers in column j.

Next, I performed a second traversal of the grid. For each server (grid[i][j] == 1), I checked if there were other servers in its row or column by evaluating whether row_count[i] > 1 or col_count[j] > 1. If either condition was true, the server was added to the count of communicating servers - res.

Implementation

@Ifihan
Ifihan / main.md
Created January 24, 2025 22:43
Find Eventual Safe States

Question

Approach

My approach was by flipping the perspective of the graph. Instead of working with the original graph, I created a reversed graph, where edges point from their destination back to their source. This allows me to process nodes starting from the terminal ones (nodes with no outgoing edges), which are inherently safe.

Next, I calculated the out-degree for each node in the original graph, which represents the number of edges each node has. Nodes with an out-degree of 0 are terminal nodes, so I added them to a queue to process them first.

From there, I processed the queue. For each node, I marked it as safe and updated its neighbors in the reversed graph. If a neighbor’s out-degree dropped to 0, I added it to the queue since it had no remaining paths leading to unsafe nodes. This way, I worked backward, ensuring only nodes that eventually lead to terminal nodes were mar

@Ifihan
Ifihan / main.md
Created January 25, 2025 22:36
Make Lexicographically Smallest Array by Swapping Elements

Question

Approach

I start my approach by pairing each element with its original index and sorting these pairs based on their values. Next, I form groups of elements where each consecutive pair in the sorted list has a difference within the given limit. These groups represent sets of elements that can be swapped with each other through a chain of allowed operations. For example, if elements A, B, and C are sorted and consecutive in value, and |A-B|limit and |B-C|limit, they form a single group even if |A-C| exceeds the limit indirectly.

For each group, I extract the original indices of its elements and sort these indices. I then assign the sorted values of the group to these sorted indices.

Implementation

@Ifihan
Ifihan / main.md
Created January 26, 2025 22:28
Maximum Employees to Be Invited to a Meeting

Question

Approach

Couldn't solve today's question on my own beause it was really hard (as in "Hard" Difficulty) and I am not too comfortable with graphs. The solution to this can be found in the editorial

image
@Ifihan
Ifihan / main.md
Created January 27, 2025 22:19
Course Schedule IV

Question

Approach

Today's question was one of a kind. I tried my best to understand the question but it was a partial thing. So I ended up using the editorial

image
@Ifihan
Ifihan / main.md
Created January 28, 2025 22:21
Maximum Number of Fish in a Grid

Question

Approach

As usual, it's a graph question and I used the editorial to solve as my graph skills are still poor.

image
@Ifihan
Ifihan / main.md
Created January 29, 2025 18:23
Redundant Connection

Question

Approach

Very interesting uquestion again today (graph). Augmented my thought process with the editorial!

image
@Ifihan
Ifihan / main.md
Created January 30, 2025 21:57
Divide Nodes Into the Maximum Number of Groups

Question

Approach

The difficulty of this question was Hard plus a graph. Couldn't do it so I consulted the editorial

image
@Ifihan
Ifihan / main.md
Created January 31, 2025 22:35
Making A Large Island

Question

Approach

Another "Hard" question today, Leetocde must be testing me at this point. Well, with the editorial, I solved it.

Uploading image.png…

@Ifihan
Ifihan / main.md
Created February 1, 2025 22:10
Special Array I

Question

Approach

1

My first approach was to first check if the length of the array nums was 1. If it was, I will return True. Next, I set up a loop to iterate over every element in the array. Normally, I would expect to compare each pair of adjacent elements—comparing nums[i] with nums[i+1] to ensure that one is even and the other is odd. However, the code I wrote doesn’t actually compare adjacent pairs. Instead, within the loop, I use a condition that checks (nums[i] % 2 == 0 or nums[i] % 2 != 0) for each element. I realized that this condition is always true for any integer because every number is either even or odd. Since I repeat this check twice with an and between them, the overall condition remains always true.

Implementation