We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
A small Java solution in O(n) time with O(1) space:
//Java 8/*Initial Thoughts: We can keep a running max and update it if wefind something larger, if we find something smallerwe just keep looking and if we find something equalthen we increment a counter variableTime Complexity: O(n) //We must check the height of every candleSpace Complexity: O(1) //We only store a max and a frequency*/importjava.io.*;importjava.util.*;importjava.text.*;importjava.math.*;importjava.util.regex.*;publicclassSolution{publicstaticvoidmain(String[]args){Scannerin=newScanner(System.in);intn=in.nextInt();inttallest=0;intfrequency=0;for(inti=0;i<n;i++){intheight=in.nextInt();if(height>tallest){tallest=height;frequency=1;}elseif(height==tallest)frequency++;}System.out.println(frequency);}}
You can find more HackerRank solutions like this here
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Birthday Cake Candles
You are viewing a single comment's thread. Return to all comments →
A small Java solution in O(n) time with O(1) space:
You can find more HackerRank solutions like this here