This problem was very similar to FizzBuzz, where you had to perform some math based on a couple of conditionals. If the input number was divisible by 2, then you divided by 2, otherwise you subtracted 1 from the number. You count each of those steps until the number gets to 0. I arrived at the solution (via pad and paper) relatively quickly and was elated to see that the answer passed all the checks.
This solution is correct, with Time Complexity of O(logn) and Space Complexity of O(1). The O(logn) notation is new, but it makes sense because dividing by two takes longer the larger the number. I also need to review logarithms but I mostly remember the curvature of the graph and think about it through that context (larger number == longer x axis, initial steepness of the curve is based on the first few divisions by 2).
The twist in this problem comes from the video solution introducing bitwise operations. These are some crazy syntax operations that are slightly faster because they machine is able to compute faster, but they also reduce the readability (in my opinion). I think this problem is really just about setting you up with some bitwise experience for some more advanced problems that are going to come up next. I can't explain bitwise operators via written text yet, but I hope to do that after I see them next.
This solution has the same Time and Space Complexity as the prior solution.
If you are familiar with if / elseif / else logic, then this question was a bit more straightforward than the last two. With a given input integer "n", this function has to count up through each whole number and output a certain value based on certain parameters. For numbers that are divisible by 3, it should output "Fizz", if it is divisible by 5, it should output "Buzz", and if it is divisible by both 3 and 5 then it should output "FizzBuzz". If the number is not divisible by 3 or 5 then it should output the number that it is on. Say n=5, then the output array would be ["1","2","Fizz","4","Buzz"]. It must output a 1-indexed string array.
Again, I grabbed a notepad to sketch out the logic (which I won't picture here) and got my head wrapped around what is supposed to be happening. Once I understood that, I mapped out a for loop that would create an string array consisting of a count up to the input number. I created an empty string array (let output: string[] = [];) and then opened up a for loop that .push() each value into the array as a string (.push(`${i}`)). This was new syntax to me in TypeScript, but it's identical to JavaScript and I think Python as well.
Then I created some if logic with a modulo (%) operator to decide which values to push into the string array. I started with the FizzBuzz case (where the number is divisible by 3 and 5) and worked my way down to divisible by 5 and then 3. If the number is not divisible by 5 or 3 then it pushes the number into the string array with the same syntax mentioned in the previous paragraph.
This solution is correct, with Time Complexity of O(n) and Space Complexity of O(1), but it is not optimal because it isn't very modular and doesn't align with the spirit of the problem. I discovered (via the video solution) that the optimal solution involves declaring boolean values based on if the number is divisible by 3 or 5, and then concatenating a new string variable based on those booleans. This allows for easy introduction of additional criteria via another boolean and if conditional. More elegant for sure.
This solution has the same Time and Space Complexity as the prior solution, so it is more about aligning with the spirit of the question and creating an optimized solution with code.
For my second question on leetcode I grabbed a pen and paper so I could walk through the logic of the problem. I have seen this sort of 2D array before in CS50 and in my PDX Code Guild bootcamp so I was familiar with the concept of building two for loops to get into the second dimension of the array. Despite prior experience with this kind of structure, I still needed to sketch out the array to wrap my head around it.
Once I felt comfortable with how 2D arrays worked I started talking through the logic of how the for loop should work. First, you need to "open up" each item in the array, which is the customer[m]. Then you need to sum up each value in each item of the array (customer[m][n]). This calls for a nested for loop. One thing I struggled with was when to declare the variable that would contain the totals and customer totals.
As I walked through the logic I generated the demand for the variables "total" and "custTotal", but realized that they needed to be initialized outside of each loop so that they could be used within the loop. Once I had the logic written down in rough syntax, I asked chatGPT a couple of questions about how to write the correct syntax, and was able to generate the correct answer without too much guidance. ChatGPT recommended the use of spreader syntax (...) to get to the max value of the array, which I didn't use at first but ended up adopting because I understood what it was doing when I tried to get run Math.max(total) and got a type error.
As with Question 1480, the space complexity is O(1) since we do not create a data structure that is proportional to our problem set. However, since we have to iterate over every item in the 2D array, our Time Complexity is O(m*n).
My first exploration into leetcode. I chose typescript so I could learn a new language while practicing the concepts that leetcode trains you in. This question is an easy-level question that tests your ability to iterate through an array to produce an output. The leetcode video discusses the Time Complexity and Space Complexity of the solutions it provides (big-O notation).
I don't know typescript so I flipped over to chatGPT and had it describe to me what was happening here. The first thing that is happening is "function" declaring a function named "runningSum", which takes the parameter "nums" typed (via the ":") as a number array ("[]"). Also, runningSum is typed as a number array.
From there, the logic is as follows: declare a variable "result" of type number array, and let it equal an empty array. Then declare another variable "sum" and set it equal to 0. It is not explicitly typed because the type (integer) is apparent from the initial definition.
Then you write a for loop that iterates over the full array of nums (for (let num of nums) {}) and utilize the .push method on result (result.push(sum)) to alter result as you iterate through the nums array. Finally, you return result to produce the answer.
The Time Complexity for this solution is O(n), as it has to iterate through each item in the array (n items), and the Space Complexity is O(1) because the solution does not create a data structure that is proportional in size to the input.