This Weeks Leetcode Challenges!

This week I really cracked down on my Leetcode again, because I have been finding more jobs that are a Ruby/JavaScript stack which are my main two stacks anyway. And like my mock interviewer said, practice what you are best at and show what you can do, and they will trust in you to learn their stack as well.

My first leetcode challenge was “Kids with Candy”. It was a relatively simple coding challenge that ended up with the code

`def kids_with_candies(candies, extra_candies)

highest_candy = candies.max

candies.map do |candy|

candy + extra_candies >= highest_candy

end

end`
All this code does is locate which child has the least amount of candy, and returns the boolean “false”. You map over the candy, and see if it is the lowest candy provided, and in that instance returns false, with the rest showing true.

My next code was “Defanging an IP Address” where you change all . to [.] which is just a simple gsub command, so the one line code was
`def defang_i_paddr(address)

address.gsub ‘.’, ‘[.]’

end`
This was extremely easy and took very little time for me to complete.

The next challenge was “Number of Good Pairs” which took a little bit of research, and I ended up looking at a solution because I was really interested in one line code. I wanted to see how far off I was, and it turned out that I was very close to completing this coding challenge in one line on my own. The final solution was
`def num_identical_pairs(nums)

nums.each_with_index.reduce(0) { |sum, (el, i)| sum += nums[i+1..-1].count(el) }

end`
I was extremely happy to get this in one line, and looking up why the commands why they were using what they were using.

My 4th Leetcode challenge was “Goal Parser” which just changes a lot of the text out, much like the gsub one, but I typed it far more written out than the one line gsub using if/else and if/elsif commands to iterate though the whole problem.
`def interpret(command)

s = “”

l = “”

command.each_char.with_index do |c, i|

if c == “G”

s += c

else

l += c

if l == “()”

s += “o”

l = “”

elsif l == “(al)”

s += “al”

l = “”

end

end

end

s

end`
Lastly, I wrote Jewels and Stones, where the explanation was “ You’re given strings jewels representing the types of stones that are jewels, and stones representing the stones you have. Each character in stones is a type of stone you have. You want to know how many of the stones you have are also jewels.

Letters are case sensitive, so "a" is considered a different type of stone from "A". This one wasn’t to bad, it was again, just a simple do loop where you count up the “output” as I designated it, and output the new number that has the tallied number of stones.
`# @param {String} jewels

# @param {String} stones

# @return {Integer}

def num_jewels_in_stones(j, s)

output = 0

for i in 0…j.length do

output += s.count(j[i])

end

output

end`

This week was all easy Leetcode challenges, and I think I will continue until all of the free challenges are done before I move up to the medium difficulties. I still have a lot of learning to do and these are really helping my gaps, being that the knowledge I am missing from my bootcamp is independent solving and dealing with little problems like this. We mainly focused on databases and web development so this is a whole new world for me.