Pastebin: array of integers, sum two integers that match a certain value. #2

Formato
Plain text
Post date
2022-07-05 02:44
Publication Period
Unlimited
  1. Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
  2. You may assume that each input would have exactly one solution, and you may not use the same element twice.
  3. You can return the answer in any order.
  4. Example 1:
  5. Input: nums = [2,7,11,15], target = 9
  6. Output: [0,1]
  7. Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
  8. Example 2:
  9. Input: nums = [3,2,4], target = 6
  10. Output: [1,2]
  11. Example 3:
  12. Input: nums = [3,3], target = 6
  13. Output: [0,1]
  14. Constraints:
  15. 2 <= nums.length <= 104
  16. -109 <= nums[i] <= 109
  17. -109 <= target <= 109
  18. Only one valid answer exists.
  19. Follow-up: Can you come up with an algorithm that is less than O(n2) time complexity?
  20. -----------------
  21. A trivial O(n^2) solution in PHP could be:
  22. function twoSum($nums, $target) {
  23. for ($pos1 = 0; $pos1 < count($nums) - 1 ; $pos1++) {
  24. for ( $pos2 = $pos1+1 ; $pos2 < count($nums); $pos2++) {
  25. $sum = $nums[$pos1] + $nums[$pos2];
  26. if ( $sum == $target) {
  27. return array ($pos1, $pos2);
  28. }
  29. }
  30. }
  31. }
  32. -------------
  33. class Solution {
  34. /**
  35. * @param Integer[] $nums
  36. * @param Integer $target
  37. * @return Integer[]
  38. */
  39. function twoSum($nums, $target) {
  40. #$original_nums = $nums; #copy to maintain the original indexes, we need those.
  41. # this is O(n)
  42. # but then the search may be O(n) too as it is not ordered
  43. # a better idea is to switch keys and values
  44. # so that with the values we can reach the keys.
  45. $orig_num_flipped = array_flip($nums);
  46. # the flip, values are now keys and viceversa,
  47. # should cost O(2n)=O(n) as well. We get a new array.
  48. # update: doesn't work if a value is replicated,
  49. # then if a number is there multiple times it will be associated with the larger key.
  50. sort($nums); # this should be O(n*log(n)), merge sort, happens in place
  51. # then thanks to the fact that we need exactly 2 numbers,
  52. # for the property of the sum, we know that
  53. # target = x+y and if x and y are big, then at most x=y=target/2
  54. # otherwise x (or y) may start from the smallest number up to target/2 and y
  55. # may start from target-x down to target/2
  56. # then we have it.
  57. # That is O(n)
  58. # then between O(n*log(n)) and O(n), O(n*log(n)) should lead.
  59. # the problem is to identify the element in the sorted array that is closer to target.
  60. # otherwise one has to first start from the biggest element and get down to target,
  61. # then start the check (the complexity remains O(n) though)
  62. $upper_pos = count($nums);
  63. for ($pos1 = count($nums)-1 ; $pos1 > -1 ; $pos1--) {
  64. # here we identify the first element smaller than target
  65. if ($nums[$pos1] < $target) {
  66. $upper_pos = $pos1;
  67. break;
  68. }
  69. }
  70. for ( $pos_low=0, $pos_up = $upper_pos ; $pos_low < $pos_up ; ) {
  71. # here we try to identify both elements.
  72. $sum = $nums[$pos_low] + $nums[$pos_up];
  73. print ($sum);
  74. if ($sum == $target) {
  75. return array( $orig_num_flipped[$nums[$pos_low]], $orig_num_flipped[$nums[$pos_up]] );
  76. # nope this doesn't work. the problem is that with the sort
  77. # the association with the original indexes is lost.
  78. # if the function uses the array_flip function, to make an extra array
  79. # where the values are the keys, same values go overwrite the original key
  80. # keeping the largest one.
  81. # To work it is needed to have a sort function that "sorts" also the keys
  82. # a bit like KSORT in userRPL (listExt library)
  83. # With a KSORT function the entire procedure would have taken
  84. # at most O(n*logn) as the heaviest part would be the sorting.
  85. }
  86. else {
  87. # pay attention here, defensive approach
  88. if ($sum < $target) {
  89. # then we presume pos_up can be still be good, and we increment pos_low
  90. $pos_low++;
  91. }
  92. else {
  93. #then we overshoot, pos_up has to come down
  94. $pos_up--;
  95. }
  96. }
  97. }
  98. }
  99. }
Download Printable view

URL of this paste

Embed with JavaScript

Embed with iframe

Raw text