LeetCode开心刷题二十二天——41. First Missing Positive42. Trapping Rain Water

本是古典 何须时尚 2023-08-17 15:14 113阅读 0赞
  1. First Missing Positive

Hard

1825606FavoriteShare

Given an unsorted integer array, find the smallest missing positive integer.

Example 1:

  1. Input: [1,2,0]
  2. Output: 3

Example 2:

  1. Input: [3,4,-1,1]
  2. Output: 2

Example 3:

  1. Input: [7,8,9,11,12]
  2. Output: 1

Note:

Your algorithm should run in O(n) time and uses constant extra space.

ATTENTION:

we need to find the relationship between subscripts and array value.

Correct order is the subscripts i corresponding i+1,which means arr[i]=i+1;

So if you find this value is not the correct corresponding just swap it to the right place.

  1. if(arr[i]!=i+1&&arr[i]>=1&&arr[i]<=n&&arr[arr[i]-1]!=arr[i])
  2. This code is the most important one,but you need to pay attention that if arr[i]-1<0
  3. the arr will outrange so when we dealing with arrays we need to pay attention to its
  4. subscripts
  5. #include<iostream>
  6. #include<string>
  7. #include<stdio.h>
  8. #include<string.h>
  9. #include<iomanip>
  10. #include<vector>
  11. #include<list>
  12. #include<queue>
  13. #include<algorithm>
  14. #include<stack>
  15. #include<map>
  16. using namespace std;
  17. //solve function
  18. class Solution
  19. {
  20. public:
  21. int firstMissingPositive(vector<int>& arr)
  22. {
  23. int res,n=arr.size();
  24. for(int i=0;i<n;)
  25. {
  26. if(arr[i]!=i+1&&arr[i]>=1&&arr[i]<=n&&arr[arr[i]-1]!=arr[i])
  27. {
  28. swap(arr[i],arr[arr[i]-1]);
  29. }
  30. else
  31. {
  32. i++;
  33. }
  34. }
  35. for(int i=0;i<n;i++)
  36. {
  37. if(arr[i]!=i+1)
  38. {
  39. res=i+1;
  40. return res;
  41. }
  42. }
  43. res=n+1;
  44. return res;
  45. }
  46. };
  47. int main()
  48. {
  49. Solution s;
  50. vector<int> arr={
  51. 0,0,2};
  52. int res;
  53. res=s.firstMissingPositive(arr);
  54. cout<<res<<endl;
  55. return 0;
  56. }

The main progress is to use python,in face the thoughts are similar,so we need to operate skillfully

In fact,python has such difference with C,the main part is it needn’t main function

Now paste the solution below,including main function,and we use class so we need to let b=solution()

  1. class Solution(object):
  2. def firstMissingPositive(self, nums):
  3. for i in range(0,len(nums)):
  4. while nums[i]>0 and nums[i]<=len(nums) and nums[i]!=i+1 and nums[nums[i]-1]!=nums[i]:
  5. nums[nums[i]-1], nums[i] = nums[i], nums[nums[i]-1]
  6. for i in range(0, len(nums)):
  7. if nums[i] != (i+1):
  8. return i+1
  9. return len(nums)+1
  10. nums=[3,4,-1,1]
  11. b=Solution()
  12. print(b.firstMissingPositive(nums))
  1. Trapping Rain Water

Hard

388672FavoriteShare

Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.

rainwatertrap.png
The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image!

Example:

  1. Input: [0,1,0,2,1,0,1,3,2,1,2,1]
  2. Output: 6

Accepted

312,934

Submissions

714,173

IDEA ATTENTION:

The first consensus to be agree is not to waste time on a problem more than 15mins believe me darling,you need to have a clear perceive to you situation.which also as you known ,dilemma!!You are racing with time

Now you must totally clear that time only can be used to deeper the right way instead of thinking useless method you are not strong enough to pave the new road.

The first solution mainly solve by layer.But it cost too much time.Also my own method,but not recommend

  1. #include<iostream>
  2. #include<string>
  3. #include<stdio.h>
  4. #include<string.h>
  5. #include<iomanip>
  6. #include<vector>
  7. #include<list>
  8. #include<queue>
  9. #include<algorithm>
  10. #include<stack>
  11. #include<map>
  12. using namespace std;
  13. class Solution {
  14. public:
  15. int trap(vector<int>& height) {
  16. int n=height.size(),res=0,cnt=1,global=0;
  17. if(n==0||n==1) return 0;
  18. int start=-1,ends=0,flag=0;
  19. while(1)
  20. {
  21. cnt=0;start=-1,ends=-1;
  22. for(int i=1;i<n-1;i++)
  23. {
  24. if(height[i]==0) flag=1;
  25. if(i+1>=n) break;
  26. if(height[i]==0&&(height[i+1]>height[i])&&(height[i-1]>height[i]))
  27. {
  28. cnt++;
  29. }
  30. if(height[i]==0&&height[i+1]==0&&height[i-1]>0)
  31. {
  32. start=i;
  33. }
  34. if(height[i]==0&&height[i-1]==0&&height[i+1]>0)
  35. {
  36. ends=i;
  37. }
  38. if(start!=-1&&ends!=-1&&ends!=n-1&&(ends>start))
  39. {
  40. // cout<<"cntb"<<cnt<<endl;
  41. // cout<<ends<<"::"<<start<<endl;
  42. cnt+=(ends-start+1);
  43. // cout<<"cntA"<<cnt<<endl;
  44. start=-1,ends=-1;
  45. }
  46. global=i;
  47. }
  48. // cout<<"cnt"<<cnt<<endl;
  49. res+=cnt;
  50. for(int i=0;i<n;i++)
  51. {
  52. if(height[i]>0)
  53. height[i]--;
  54. }
  55. int flag0=0;
  56. for(int i=0;i<n;i++)
  57. {
  58. if(height[i]!=0)
  59. flag0=1;
  60. }
  61. if(flag0==0) break;
  62. }
  63. return res;
  64. }
  65. };
  66. int main()
  67. {
  68. Solution s;
  69. vector<int> arr={
  70. 4,9,4,5,3,2};
  71. int res;
  72. res=s.trap(arr);
  73. cout<<res<<endl;
  74. return 0;
  75. }

This is the correct way use two pointer:

  1. #include<iostream>
  2. #include<string>
  3. #include<stdio.h>
  4. #include<string.h>
  5. #include<iomanip>
  6. #include<vector>
  7. #include<list>
  8. #include<queue>
  9. #include<algorithm>
  10. #include<stack>
  11. #include<map>
  12. using namespace std;
  13. class Solution
  14. {
  15. public:
  16. int findWater(vector<int>& height)
  17. {
  18. int n=height.size(),left_max=0,right_max=0;
  19. if(n==0||n==1) return 0;
  20. int res=0,left=0,right=n-1;
  21. //we need to find this situation one less value between two high value
  22. while(left<right)
  23. {
  24. //if left's height already less than right,it means we get min value between left and right
  25. //then we can directly use this to minus
  26. if(height[left]<height[right])
  27. {
  28. (left_max<height[left])?(left_max=height[left]):res+=left_max-height[left];
  29. left++;
  30. }
  31. else
  32. {
  33. (right_max<height[right])?(right_max=height[right]):res+=right_max-height[right];
  34. right--;
  35. }
  36. }
  37. return res;
  38. }
  39. };
  40. int main()
  41. {
  42. Solution s;
  43. vector<int> height={
  44. 0,1,0,2,1,0,1,3,2,1,2,1};
  45. int res;
  46. res=s.findWater(height);
  47. cout<<res<<endl;
  48. return 0;
  49. }

Python Version:

Special Assignment:

just put variable inorder between = left and right

  1. left,right=0,len(height)-1

In python we make class Instantiation like this:

  1. solu = Solution()

Python Code:

  1. class Solution(object):
  2. def trap(self, height):
  3. """
  4. :type height: List[int]
  5. :rtype: int
  6. """
  7. if not height:return 0
  8. left_max=right_max=res=0
  9. left,right=0,len(height)-1
  10. while left<right:
  11. if height[left]<height[right]:
  12. if left_max>height[left]:
  13. res+=left_max-height[left]
  14. else:
  15. left_max=height[left]
  16. left+=1
  17. else:
  18. if right_max>height[right]:
  19. res+=right_max-height[right]
  20. else:
  21. right_max=height[right]
  22. right-=1
  23. return res
  24. if __name__ == '__main__':
  25. height=[0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]
  26. solu = Solution()
  27. print(solu.trap(height))

转载于:https://www.cnblogs.com/Marigolci/p/11216840.html

发表评论

表情:
评论列表 (有 0 条评论,113人围观)

还没有评论,来说两句吧...

相关阅读