DSA GRIND #4
DSA GRIND #4 BY ASHUR BAROUTTA
This week we kept up with the trend of solving 2 problems a week. The first problem asked us to concatenate two arrays. Stating "Given an integer array nums of length N, create an array ans of length 2N where ans[i] == nums[i] and ans[i+n] == nums[i]". This took more thought than actual coding as the solution turned out to be far simpler than imagined.
I settled for making a new array with twice the size of nums length, then I initialized a variable j = 0. After that I made a for loop and within that loop I set a condition to check if i was less than the length of the original array as if it was I knew I could just make the indexes of the new array equal to the same index as nums array. After that, we set newarray[i] equal to nums[j] as j is incrementing from 0 and at the point where this started we would be passed all the first recorded indexes.
A merger like this is probably useful when wanting to join two different specific arrays into one for some sort of data processing. The submission is as follows.
The second problem is easy to state hard to explain unless you've dealt with stacks before. It states "Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid." Simply put, I used a stack to keep layering the symbols and continued to remove or add depending on the character that followed. The final return was if the stack was empty as if the characters matched, id always peel layers off the stack. So if all the appropriate characters in the stack matched, the stack would be empty, otherwise the return would be false.
The solution to the problem is below. I am pleased with this one.
Great work on these problems, Ashur. You should be proud.
ReplyDeleteInteresting, I sometimes overthink as well when problems have a solution simpler than I imagined. The loop inside a loop seemed complicated.
ReplyDelete