This research presents a novel algorithm designed to reduce computational time in the meso-scale analysis of masonry buildings. The algorithm employs nonlinear topology optimization in conjunction with the Drucker-Prager yield criterion to identify critical zones within a structure. These critical zones are modeled at the meso-scale, while less critical regions are represented at the macro-scale. To evaluate the efficacy and accuracy of the proposed method, three masonry wall samples were analyzed, comparing computational time and accuracy across three modeling strategies: full meso-scale, full macro-scale, and optimized meso-macro scale. The results indicate that while macro-scale models provided faster analyses, they exhibited lower accuracy compared to meso-scale models and demonstrated greater initial stiffness and maximum force due to their elastic-perfectly plastic behavior. In contrast, the optimized meso-scale models reduced the computational time by 32.5%, 46%, and 30% compared to full meso-scale models, while maintaining high accuracy in replicating crack patterns and force-displacement responses observed in experimental data. These findings suggest that the developed algorithm offers an efficient and accurate computational approach for analyzing the complex behavior of masonry buildings under various loading conditions.