Hi, Folks! Today, I solved three problems on LeetCode: Moving Stones Until Consecutive II, Permutation in String, and Boats to Save People. To solve these problems, we didn't use any complex data structures. Instead, we focused on finding the correct logic. Developing logical solutions comes with practice.
In both Moving Stones Until Consecutive II and Boats to Save People, the initial step is sorting of given input arrays. Sorting makes the logic easier for both problems.
In the problem Moving Stones Until Consecutive II, we can use the sliding window approach to find the largest number of consecutive stones. We calculate the maximum moves by moving the smallest stone to the left and the largest stone to the right until all the stones are consecutive. This is how we can solve the problem.
For example, in the Permutation in String problem we can first use a hash map that stores the first string character's frequency. With the sliding window approach, this time traversing the first string, we take it with a window length similar to that of the length of the second string for each step we check if its characters constitute a permutation based upon the stored frequencies. Such is one way of working out such a problem.
In the problem Boats to Save People, we travel through the array using two pointers, one from each end. We continue traveling until the pointers meet and check if the sum of the weights is within the limit. That is how we can solve this problem.
I hope my experience will be helpful.
Top comments (0)