Tip 1 : Revise what you learned
Tip 2 : Try solving new questions
Tip 3 : See hint after 20 minutes if no solutions hit
Tip 1 : Use Single column
Tip 2 : Mention projects in detail
Intuition
Treat the 2d grid map as an undirected graph and there is an edge between two horizontally or vertically adjacent nodes of value '1'.
Algorithm
Linear scan the 2d grid map, if a node contains a '1', then it is a root node that triggers a Depth First Search. During DFS, every visited node should be set as '0' to mark as visited node. Count the number of root nodes that trigger DFS, this number would be the number of islands since each DFS starting at some root identifies an island.
Intuition
Whenever you see a question that asks for the maximum or minimum of something, consider Dynamic Programming as a possibility. The difficult part of this problem is figuring out when a negative number is "worth" keeping in a subarray. This question in particular is a popular problem that can be solved using an algorithm called Kadane's Algorithm. If you're good at problem solving though, it's quite likely you'll be able to come up with the algorithm on your own. This algorithm also has a very greedy-like intuition behind it.
Let's focus on one important part: where the optimal subarray begins. We'll use the following example.
nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
We can see that the optimal subarray couldn't possibly involve the first 3 values - the overall sum of those numbers would always subtract from the total. Therefore, the subarray either starts at the first 4, or somewhere further to the right.
What if we had this example though?
nums = [-2,1000000000,-3,4,-1,2,1,-5,4]
We need a general way to figure out when a part of the array is worth keeping.
As expected, any subarray whose sum is positive is worth keeping. Let's start with an empty array, and iterate through the input, adding numbers to our array as we go along. Whenever the sum of the array is negative, we know the entire array is not worth keeping, so we'll reset it back to an empty array.
However, we don't actually need to build the subarray, we can just keep an integer variable current_subarray and add the values of each element there. When it becomes negative, we reset it to 0 (an empty array).
F2F interview over zoom call
Tip 1 : Revise SQL basics
Tip 2 : Do SQL problems
Tip 3 : Learn important concepts like ACID properties
Here's your problem of the day
Solving this problem will increase your chance to get selected in this company
How do you write a single-line comment in C++?