Advertisement
maycod23

Untitled

Mar 23rd, 2023
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. /*
  2. question -> https://leetcode.com/problems/product-of-array-except-self/description/
  3. */
  4.  
  5. //code:
  6. class Solution {
  7. public:
  8. vector<int> productExceptSelf(vector<int>& a) {
  9. int n=a.size();
  10. vector<int> ans(n);
  11. int p=1;
  12. for(int i=0;i<n;i++){
  13. ans[i]=p;
  14. p=(p*a[i]);
  15. }
  16. p=1;
  17. for(int i=n-1;i>=0;i--){
  18. ans[i]*=p;
  19. p=(p*a[i]);
  20. }
  21. return ans;
  22. }
  23. };
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30. /*
  31. question link-> https://leetcode.com/problems/check-knight-tour-configuration/
  32. */
  33.  
  34.  
  35. //code:
  36.  
  37.  
  38. class Solution {
  39. public:
  40. bool can_go(int s,int e, map<int,pair<int,int>>& m){
  41. pair<int,int> p1=m[s],p2=m[e];
  42. int y1=p1.first,x1=p1.second,y2=p2.first,x2=p2.second;
  43. if(abs(x2-x1)==1&&abs(y2-y1)==2) return true;
  44.  
  45. if(abs(x2-x1)==2&&abs(y2-y1)==1) return true;
  46. return false;
  47. }
  48. bool checkValidGrid(vector<vector<int>>& grid) {
  49. int n=grid.size();
  50. map<int,pair<int,int>> m;
  51. for(int i=0;i<n;i++){
  52. for(int j=0;j<n;j++){
  53. m[grid[i][j]]={i,j};
  54. }
  55. }
  56. pair<int,int> tempp={0,0};
  57. if(m[0]!=tempp) return false;
  58.  
  59. int temp=(n*n)-1;
  60. for(int i=0;i<=(temp-1);i++){
  61. if(can_go(i,i+1,m)) continue;
  62. else return false;
  63. }
  64. return true;
  65. }
  66. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement