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 June 22, 2025 20:56
Minimum Deletions to Make String K-Special

Question

Approach

I count the frequency of each character in the string. Then I sort these frequencies to facilitate comparison. For each possible target frequency f (taken from any of the observed frequencies), I check how many deletions are needed to make all other character frequencies fall within [f, f + k].

Implementation

class Solution:
 def minimumDeletions(self, word: str, k: int) -> int:
@Ifihan
Ifihan / main.md
Created June 22, 2025 20:49
Divide a String Into Groups of Size k

Question

Approach

I first check if the length of the string s is divisible by k. If it’s not, I calculate how many characters are needed to complete the final group and pad the string with the fill character accordingly. Once the string length is a multiple of k, I split it into substrings of length k by slicing it from index 0 to the end in steps of k, and collect all such groups into a list to return.

Implementation

class Solution:
@Ifihan
Ifihan / main.md
Created June 20, 2025 20:20
Maximum Manhattan Distance After K Changes
@Ifihan
Ifihan / main.md
Created June 19, 2025 21:57
Partition Array Such That Maximum Difference Is K

Question

Approach

I first sorted the array so I could group elements more easily. Then I used a greedy strategy, starting from the smallest ungrouped element and including as many consecutive elements as possible within the difference constraint k. I repeated this process until all elements were grouped, incrementing the count of subsequences each time I started a new group.

Implementation

class Solution:
 def partitionArray(self, nums: List[int], k: int) -> int:
@Ifihan
Ifihan / main.md
Created June 18, 2025 19:26
Divide Array Into Arrays With Max Difference

Question

Approach

I first sort the array nums so that elements that are closer in value are grouped together. After sorting, I iterate through the array in chunks of 3 elements since each group must contain exactly 3 numbers. For every group of three consecutive elements, I check if the difference between the maximum and minimum element in that group is less than or equal to k. If all such groups satisfy the condition, I collect them into the result. If I find any group where the difference exceeds k, I immediately return an empty array, since it’s impossible to divide nums as required.

Implementation

class Solution:
@Ifihan
Ifihan / main.md
Created June 17, 2025 02:13
Count the Number of Arrays with K Matching Adjacent Elements
@Ifihan
Ifihan / main.md
Created June 17, 2025 02:05
Maximum Difference Between Increasing Elements

Question

Approach

I keep track of the smallest value seen so far as I iterate through the array. For each element, I check if it’s greater than this minimum value. If it is, I compute the difference and update the maximum difference found so far. If it’s not, I update the minimum value.

Implementation

@Ifihan
Ifihan / main.md
Created June 15, 2025 21:32
Max Difference You Can Get From Changing an Integer

Question

Approach

To solve this problem, I try to get the maximum and minimum numbers possible by remapping one digit in the original number. To get the maximum number, I find the first digit that isn’t 9 and replace all its occurrences with 9, maximizing the number. For the minimum number, I handle it carefully to avoid leading zeros: if the first digit isn’t 1, I replace all its occurrences with 1; otherwise, I look for the next digit (excluding 0 and 1) to replace with 0, minimizing the number while keeping it valid. After applying these two strategies independently, I compute the difference between the maximum and minimum numbers and return that value.

Implementation

class Solution:
@Ifihan
Ifihan / main.md
Created June 14, 2025 03:09
Maximum Difference by Remapping a Digit

Question

Approach

I convert the number into a string so I can easily manipulate digits. To maximize the number, I choose the first digit that is not 9 and replace all occurrences of it with 9. This gives me the highest possible value. To minimize the number, I choose the first digit that is not 0 and replace all occurrences of it with 0.

Implementation

class Solution:
@Ifihan
Ifihan / main.md
Created June 14, 2025 02:55
Maximum Difference by Remapping a Digit

Question

Approach

I start by sorting the array nums because closer numbers are more likely to form pairs with smaller differences. Then I binary search on the minimum maximum allowed difference (let’s call it maxDiff). For each maxDiff candidate, I greedily count how many disjoint pairs I can form such that each pair has a difference ≤ maxDiff. If I can form at least p such pairs, I reduce the search space. Otherwise, I increase it.

Implementation

class Solution: