Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.76 KB | None | 0 0
  1. # Problem Solving Strategies for Algorithms
  2. <p>Algorithms can be really intimidating, especially if you don't know how to approach them. This article will cover some common problem solving strategies that you can use to solve alot of common algorithms, please note that these problem solving strategies won't work for every algorithm and may not produce the best efficiencies for ones they do work on</p>
  3. <br />
  4. <h3>Multiple Pointers</h3>
  5. <p>One of the best approaches to solving a problem that involves some kind of looping is creating multiple pointers for that particular loop, this is a fantastic strategy for not only simplifying the problem at hand, but also tends to make problems more efficient. </p>
  6. <br>
  7. <p>To show how we can use multiple pointers, here's a very basic example of a common problem where multiple pointers can be used: reversing an array</p>
  8.  
  9. ```javascript
  10. function reverseArr(arr){
  11. let right = arr.length - 1;
  12. let left = 0;
  13. while(left < right){
  14. const temp = arr[right];
  15. arr[right] = arr[left];
  16. arr[left] = temp;
  17. right--;
  18. left++
  19. }
  20. return arr;
  21. }
  22. ```
  23.  
  24. Here we create two pointers, one that points to the beginning of the array and another that points to the end. This is great because it lets us essentially keep track of multiple portions of the array at one time, saving us from having to loop more than once.
  25.  
  26. Here's another example:
  27. Merging two sorted arrays
  28. ```javascript
  29. function mergeArrs(arr1,arr2){
  30. let pt1 = 0;
  31. let pt2 = 0;
  32. let mergedArr = new Array(arr1.length+arr2.length);
  33. while(pt1 < arr1.length || pt2 < arr2.length){
  34. const num1 = arr1[pt1];
  35. const num2 = arr2[pt2];
  36. if(pt2 >= arr2.length || num1 <= num2){
  37. mergedArr[pt1+pt2] = num1;
  38. pt1++;
  39. }else{
  40. mergedArr[pt1+pt2] = num2;
  41. pt2++;
  42. }
  43. }
  44. return mergedArr;
  45. }
  46. ```
  47. Here we have a pointer that points at a number in the first array and another that points at a number in the second array. We compare the numbers each pointer is pointing at and add them to our merged array based on which one is less than the other. In the case a pointer reaches the end of one array, we simply add the other number into our array. This lets us keep track of both arrays and do comparisons between them in the same loop iterations.
  48.  
  49. <h3>Shifting Frames</h3>
  50. <p>A common theme that comes up with algorithms are problems that require a "frame" of solutions. For example, lets say we're given an array and we want to retrieve the maximum subarray sum, for example:</p>
  51. ```javascript
  52. maxSubSum([1,2,-10,9,-3]) // Returns 9 because the maximum subarray sum is [9]
  53. maxSubSum([1,2,-10,2,-3]) // Returns 3 because the maximum subarray sum is [1,2]
  54. ```
  55. <p> Notice in this problem, our solution relies on maximizing a contiguous portion of our input. These problems can be solved with shifting frame in which we keep track of a frame of the input that can grow or shorten accordingly for example: </p>
  56. For the input: [1,2,-10,-3], we can have a pointer which marks the start of our frame and another that marks the end, each would start at the origin point since our answer could theoretically just be one number from our subarray like so:
  57. <br>
  58. st <br>
  59. end <br>
  60. [1,2,-10,3] <br>
  61. In this first iteration, since both of our pointers are at 1, our current maximum subarray is [1] which has a sum of 1, we can now look ahead at the next value and see if expanding our frame and including that value will increase our current maximum value. In this case, [1,2] equals 3 which is greater than [1], so we move our end pointer forwards: <br>
  62. <br>
  63. st <br>
  64. end <br>
  65. [1,2,-10,3] <br>
  66. We can continue this process until we reach the end of our array to return 3. Our shifting frame ends up being [1,2]. What if our example was a bit more complicated? For example, what if we have to move our head pointer as well? Here's a problem where that scenario would be the case:
  67. <br>
  68. st<br>
  69. end<br>
  70. [-1,2,3,-10,3]<br>
  71. In our first iteration, our max sum is -1, we look ahead to 2, since 2+-1 is greater than 1, we move our end pointer forwards <br>
  72. <br>
  73. st<br>
  74. end<br>
  75. [-1,2,3,-10,3]<br>
  76. In our next iteration, adding 3 also increases our max sum of 1, so we move our pointer again and end with a max sum of 4
  77. <br>
  78. st<br>
  79. end<br>
  80. [-1,2,3,-10,3]<br>
  81. If we continue to the end, we'll notice no more additions will make 4 greater, so we return [-1,2,3] => 4, however 4 isn't our largest sum, [2,3] => 5 is. How do we move our start pointer forwards? We can do so by simply comparing our start number to the next number in the frame; if our start number is less than the current number and is negative, we simply move our frame forward. Alternatively, we can check if the start number + the next number in the frame is less than the next number in the frame (is -1+2 < 2?); if it fits either of these cases, we simply move the start pointer forwards.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement