Joey has been attacked by Joker . Bruce Wayne has deployed automatic machine gun at each tower of Smith. All the towers in Smith are in straight line. You are given no of towers 'n' followed by height of 'n' towers. For every tower(p), find the height of the closest tower (towards the right), greater than the height of tower(p). Now , Print sum of all such heights (mod 1000000001).
Question
Joey has been attacked by Joker . Bruce Wayne has deployed automatic machine gun at each tower of Smith. All the towers in Smith are in straight line. You are given no of towers 'n' followed by height of 'n' towers. For every tower(p), find the height of the closest tower (towards the right), greater than the height of tower(p). Now , Print sum of all such heights (mod 1000000001).
Solution
This problem can be solved using a stack data structure. Here are the steps:
- Initialize a stack and a variable
sumto 0. - Iterate over the heights of the towers from right to left.
- For each tower, while the stack is not empty and the height of the current tower is greater than the height of the tower at the top of the stack, pop the stack.
- If the stack is not empty after the above step, add the height of the tower at the top of the stack to
sum. - Push the height of the current tower onto the stack.
- After iterating over all the towers, print
summodulo 1000000001.
This algorithm works because the stack always contains the heights of the towers that are to the right of the current tower and are taller than all the towers that have been seen so far to the right of the current tower. Therefore, the height at the top of the stack is always the height of the closest tower to the right that is taller than the current tower.
Similar Questions
Which building designed by Adrian Smith of SOM, broke all height records and graces the skyline of Dubai as the tallest building in the world?*1 pointBurj Al ArabAbraj Al-Bait Clock TowerLakhta CenterBurj Khalifa
int furthestBuilding(vector<int>& heights, int bricks, int ladders) { int n=heights.size(); int count=0; for(int i=0;i<n-1;i++){ if(heights[i]<heights[i+1]){ int diff=heights[i+1]-heights[i]; if(bricks>diff){ count++;… return count; }};
During a trip to the beach, Rafi built sandcastles with 2 towers each, and Monica built sandcastles with 15 towers each. Before leaving, they counted the towers and realized that they had each built the same number. What is the smallest number of towers each must have built?
Imagine yourself as a brave explorer stepping into an ancient world full of amazing things. Stories tell of a special grid filled with secret numbers that hold ancient powers. Your mission is to create a C program that understands how big the grid is, with its rows and columns. These numbers might have hidden meanings!Each box in this puzzle holds a special number that is part of a story. Your task is to print the smallest number—a very important clue—to uncover the secrets of the grid.Input FormatThe First Line contains 2 integers N and MThe Next N Lines contains M integers seperated by space.Constraints1 <= N * M <= 1e6-1e7 <= a[i][j] <= 1e7Output FormatPrint an integer as outputSample Input 03 31 2 3-5 3 29 8 7Sample Output 0-5Sample Input 17 7412 290 783 800 859 318 108 721 692 258 436 491 30 422 558 836 92 721 519 337 998 441 958 823 653 316 117 465 438 978 689 803 629 456 545 958 674 421 631 843 375 796 878 211 149 902 192 677 0 Sample Output 10Contest ends in 5 hoursSubmissions: 524Max Score: 100Rate This Challenge: More C1#include <stdio.h>2#include <string.h>3#include <math.h>4#include <stdlib.h>56int main() {78 /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 9 return 0;10}11
Ramanuj walked from “Tower A” to “Tower B” in the East 10 feet. Then she turned to the right and walked 3 feet. Again he turned to the right and walked 14 feet. How far is she from Tower A?4 feet5 feet24 feet27 feet
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.