Skip to content

Instantly share code, notes, and snippets.

View steph-crown's full-sized avatar
💻
Open to work

Stephen Emmanuel steph-crown

💻
Open to work
View GitHub Profile
@steph-crown
steph-crown / me.md
Created February 25, 2026 08:53
1356. Sort Integers by The Number of 1 Bits

Question

How I Solved It

  • Rust already provided a very helpful method count_ones to count number of 1's in the binary representation of an integer.
  • Therefore I used this together with sort_unstable_by to implement the sort

Here's my solution

@steph-crown
steph-crown / me.md
Created February 24, 2026 21:54
1022. Sum of Root To Leaf Binary Numbers

Question

How I Solved It

  • I used a depth traversal to get the binary strings formed by each path
  • Then I used parseInt(str, 2) to convert the binary string to an integer
  • Finally I returned the sum of these integers

Here's my solution:

@steph-crown
steph-crown / me.md
Created February 18, 2026 10:44
693. Binary Number with Alternating Bits

Question

How I Solved It

  • I used the long division process used to convert a decimal number to a binary
  • while the argument n was greater than 0, I calculated the remainder rem when n is divided by 2, and then I set n to the value of n / 2 (integer division).
  • For each cycle, I keep track of the value of rem from the previous cycle as prev_rem, and in each cycle, I check if new rem equals prev_rem
  • If equals, it violates the alternating principle and I return false early. Otherwise I proceed to the next cycle
  • If I don't return till n becomes 0, it means there wasn't consecutive equal remainders and so I return true
@steph-crown
steph-crown / me.md
Created February 17, 2026 12:15
401. Binary Watch

Question

How I Solved It

  • After some time of pondering on how best to derive all permutations of selections, I decided to look at the hint, and Hint 2 helped me
  • Hint 2 says "Consider calculating all possible times for comparison purposes."
  • So I needed to approach it differently. Instead of the approach I was using previously, I needed to check all the possible times (in 12 hours) that exists, and then see which of them, when written as binaries and counting the 1's will give us the value of turned_on
  • To do this, I created an outer loop to loop through the minutes 0 through 11 and an inner loop through the minutes 0 through 59.
  • Then for each I counted the 1's in the binary representation of the hour and minute.
  • I added the 1's togehther and if it gave me the value of turned_on, it was a valid permutation and I added it to a result I had created
@steph-crown
steph-crown / me.md
Created February 16, 2026 22:39
190. Reverse Bits

Question

How I Solved It

  • I used the long division method of converting a decimal number to a binary number and binary to a decimal, but in the same loop
  • I define a variable total which will store our computed decimal number
  • I also define count that starts from 31 and goes down to 0 for each loop cycle
  • While the number n is greater than 0, I find the remainder after it's divided by 2. If this remainder is 1, I add to total, the value of 2 to the power of count
  • At the end, I return total
@steph-crown
steph-crown / me.md
Last active February 15, 2026 14:52
67. Add Binary

Question

How I Solved It

  • I solved it the old-fashioned way
  • I converted the strings to byte arrays
  • Then I defined a carry variable used to track remainders from bit additions, initially 0
  • I also defined a res variable, which is a string to track the result of the addition
  • Starting from the last index of both arrays, I added corresponding bits together with the carry
  • I computed the result of this addition modulo 2, and appended to res
@steph-crown
steph-crown / me.md
Created February 12, 2026 09:41
3713. Longest Balanced Substring I

Question

How I Solved It

  • This is similar to the problem I solved a couple of days ago about longest balanced subarray, but this time, we're checking the counts of the distinct characters, rather than even/odd numbers
  • I created a nested loop to go through each substring, and for each substring, I check if it's balanced
  • If it's balanced, i track the length
  • Then I return the max of all such lengths

Here's the code

@steph-crown
steph-crown / me.md
Created February 10, 2026 10:08
3719. Longest Balanced Subarray I

Question

How I Solved It

  • I used a nested loop to cover all possible subarrays
  • For each subarray, I note the number of unique odds and unique evens
  • Then I check if it's balanced (odds == evens)
  • If balanced, I get the length of that subarray
  • Finally, I return the maximum of all such lengths of balanced subarrays
@steph-crown
steph-crown / me.md
Created February 6, 2026 11:51
3634. Minimum Removals to Balance Array

Question

How I Solved It

  • First, I sort the array in ascending order
  • Then I use a sliding window to check every sub array, finding the ones that are balanced, i.e, [i..j], such that nums[j] / nums[i] <= k
  • For every window proves to be balanced, I check the length and keep track of the maximum of all such lengths
  • After all windows is covered, this maximum length proves to be the length of the maximum possible balanced subarray which means, taking out this length from length of nums gives the minimum number of removals to arrive at it

Here's my solution:

@steph-crown
steph-crown / me.md
Created February 5, 2026 08:00
3379. Transformed Array

Question

How I Solved It

  • I created a new result array of same length as nums
  • Then I loop through nums
  • For each index i, I calculate the new target_index
  • To do this, I calculate i + steps, then I add the length of nums to it, so that it'll ensure the value is positive
  • But this addition means that, if i + steps was positive in the first place, then adding nums will push it out of bounds, so I also do a % nums to the answer
  • I assign the result[i] to the value at target_index of nums