My complex solution handling lower left / upper right corner swap


  • 0
    M
    #include <vector>
    #include <iostream>
    #include <climits>
    #include <algorithm>
    using std::vector;
    using std::min;
    using std::max;
    
    class Solution {
    public:
    	int findMax(int A, int E, int C, int G){
    		int maxxy = INT_MIN;
    		if(A > maxxy)
    			maxxy = A;
    		if(E > maxxy)
    			maxxy = E;
    		if(C > maxxy)
    			maxxy = C;
    		if(G > maxxy)
    			maxxy = G;
    		return maxxy;
    	}
    	int findMin(int A, int E, int C, int G){
    		int minxy = INT_MAX;
    		if(A < minxy)
    			minxy = A;
    		if(E < minxy)
    			minxy = E;
    		if(C < minxy)
    			minxy = C;
    		if(G < minxy)
    			minxy = G;
    		return minxy;
    	}
    	void printArray(vector<vector<int>> &arr){
    		for(int i=arr.size()-1; i>=0; i--){		
    			for(int j=0; j<arr[i].size(); j++){
    				std::cout << arr[i][j] << " " ;
    			}			
    				std::cout << std::endl;
    		}
    				std::cout << std::endl;
    	}
    	int sumArray(vector<vector<int>> &arr){
    		int totalArea = 0;
    		for(int i=arr.size()-1; i>=0; i--){		
    			for(int j=0; j<arr[i].size(); j++){
    				totalArea += arr[i][j];
    			}			
    		}
    		return totalArea;
    	}
        int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {       
    		int minX = findMin(A, E, C, G);
    		std::cout << "MinX = " <<  minX << std::endl;
    		int maxX = findMax(A, E, C, G);
    		std::cout << "MaxX = " <<  maxX << std::endl;
    		int minY = findMin(B, D, F, H);
    		std::cout << "MinY = " <<  minY << std::endl;
    		int maxY = findMax(B, D, F, H);
    		std::cout << "MaxY = " <<  maxY << std::endl;
    		int arrayX = maxX-minX + 1;
    		std::cout << "arrayX = " <<  arrayX << std::endl;
    		int arrayY = maxY-minY + 1;
    		std::cout << "arrayY = " <<  arrayY << std::endl;
    		vector<vector<int>> array2D;
    		array2D.resize(arrayY);
    		for (int i = 0; i < arrayY; ++i)
    			array2D[i].resize(arrayX);
    
    		printArray(array2D);
    		
    		for(int i = 0; i < arrayY; i++){
    			for(int j = 0; j < arrayX; j++){
    				array2D[i][j] = 0;
    			}
    		}
    		printArray(array2D);
    		int A_prime = A - minX;
    		int C_prime = C - minX;
    		int E_prime = E - minX;
    		int G_prime = G - minX;
    		int B_prime = B - minY;
    		int D_prime = D - minY;
    		int F_prime = F - minY;
    		int H_prime = H - minY;
    		std::cout << "(A, B); (C, D) = " << "(" << A_prime << ", "<< B_prime << "),("<< C_prime << ", " << D_prime << ")"<< std::endl;
    		std::cout << "(E, F); (G, H) = " << "(" << E_prime << ", "<< F_prime << "),("<< G_prime << ", " << H_prime << ")"<< std::endl;
    		for(int i = min(A_prime, C_prime); i < max(A_prime, C_prime); i++){
    			for(int j = min(B_prime, D_prime); j < max(B_prime, D_prime); j++){
    				array2D[j][i] = 1;
    			}
    		}
    		for(int i = min(E_prime, G_prime); i < max(E_prime, G_prime); i++){
    			for(int j = min(F_prime, H_prime); j < max(F_prime, H_prime); j++){
    				array2D[j][i] = 1;
    			}
    		}
    		printArray(array2D);
    		return sumArray(array2D);
        }
    };
    
    int main(){
    	Solution sol;
    	int A = -3;
    	int B = 0; 
    	int C = 3; 
    	int D = 4; 
    	int E = 0; 
    	int F = -1; 
    	int G = 9; 
    	int H = 2; 
    	int area = sol.computeArea(A, B, C, D, E, F, G, H);
    	std::cout << "total area = " << area << std::endl;
    	return 0;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.