Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- question -> https://leetcode.com/problems/product-of-array-except-self/description/
- */
- //code:
- class Solution {
- public:
- vector<int> productExceptSelf(vector<int>& a) {
- int n=a.size();
- vector<int> ans(n);
- int p=1;
- for(int i=0;i<n;i++){
- ans[i]=p;
- p=(p*a[i]);
- }
- p=1;
- for(int i=n-1;i>=0;i--){
- ans[i]*=p;
- p=(p*a[i]);
- }
- return ans;
- }
- };
- /*
- question link-> https://leetcode.com/problems/check-knight-tour-configuration/
- */
- //code:
- class Solution {
- public:
- bool can_go(int s,int e, map<int,pair<int,int>>& m){
- pair<int,int> p1=m[s],p2=m[e];
- int y1=p1.first,x1=p1.second,y2=p2.first,x2=p2.second;
- if(abs(x2-x1)==1&&abs(y2-y1)==2) return true;
- if(abs(x2-x1)==2&&abs(y2-y1)==1) return true;
- return false;
- }
- bool checkValidGrid(vector<vector<int>>& grid) {
- int n=grid.size();
- map<int,pair<int,int>> m;
- for(int i=0;i<n;i++){
- for(int j=0;j<n;j++){
- m[grid[i][j]]={i,j};
- }
- }
- pair<int,int> tempp={0,0};
- if(m[0]!=tempp) return false;
- int temp=(n*n)-1;
- for(int i=0;i<=(temp-1);i++){
- if(can_go(i,i+1,m)) continue;
- else return false;
- }
- return true;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement