Given an array H representing heights of buildings. You have to count the buildings which will see the sunrise (Assume : Sun rise on the side of array starting point).
Note : Height of building should be strictly greater than height of buildings in left in order to see the sun.
Example 1:
Input: 
N = 5
H[] = {7, 4, 8, 2, 9}
Output: 3
Explanation: As 7 is the first element, it
can see the sunrise. 4 can't see the
sunrise as 7 is hiding it.  8 can see.
2 can't see the sunrise. 9 also can see
the sunrise.
Example 2:
Input: 
N = 4
H[] = {2, 3, 4, 5}
Output: 4
Explanation: As 2 is the first element, it
can see the sunrise.  3 can see the
sunrise as 2 is not hiding it. Same for 4
and 5, they also can see the sunrise.
Your Task:  
You don't need to read input or print anything. Your task is to complete the function countBuildings() which takes the array of integers h and n as parameters and returns an integer denoting the answer.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6}
1 ≤ H_{i} ≤ 10^{8}

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Complete search

Starter code: #User function Template for python3

class Solution:

    

    def countBuildings(self,h, n):

        # code here

Solutions:
class Solution:

	def countBuildings(self, h, n):
		count = 1
		maxheight = h[0]
		for i in range(1, n):
			if h[i] > maxheight:
				maxheight = h[i]
				count += 1
		return count


class Solution:

	def countBuildings(self, h, n):
		mini = h[0]
		count = 1
		for i in range(1, n):
			if h[i] > mini:
				count += 1
			mini = max(mini, h[i])
		return count


class Solution:

	def countBuildings(self, h, n):
		new = []
		mx = 0
		for i in range(n):
			if h[i] > mx:
				mx = h[i]
				new.append(mx)
		return len(new)


class Solution:

	def countBuildings(self, h, n):
		c = 1
		m = h[0]
		for i in range(n - 1):
			if h[i] < h[i + 1] and m < h[i + 1]:
				c += 1
				m = h[i + 1]
		return c


class Solution:

	def countBuildings(self, h, n):
		c = 1
		max = h[0]
		for i in range(n):
			if h[i] > max:
				max = h[i]
				c = c + 1
		return c


class Solution:

	def countBuildings(self, h, n):
		count = 1
		maxx = h[0]
		for i in range(1, len(h)):
			if h[i] > h[i - 1] and h[i] > maxx:
				maxx = h[i]
				count += 1
		return count


class Solution:

	def countBuildings(self, h, n):
		a = h[0]
		c = 0
		for ele in h:
			if ele > a:
				c = c + 1
				a = ele
		return c + 1


class Solution:

	def countBuildings(self, h, n):
		curr_height = h[0]
		count = 1
		for x in h[1:]:
			if x > curr_height:
				curr_height = x
				count += 1
		return count


class Solution:

	def countBuildings(self, h, n):
		current_height = h[0]
		count = 1
		for i in range(1, len(h)):
			if h[i] > current_height:
				current_height = h[i]
				count += 1
			else:
				pass
		return count


class Solution:

	def countBuildings(self, h, n):
		max_till_now = -1
		count = 0
		for i in h:
			if i > max_till_now:
				count = count + 1
				max_till_now = i
		return count


class Solution:

	def countBuildings(self, h, n):
		c = 1
		k = h[0]
		for i in range(0, len(h)):
			if h[i] > k:
				c += 1
				k = h[i]
		return c


class Solution:

	def countBuildings(self, h, n):
		max = h[0]
		counter = 1
		for i in h[1:]:
			if i > max:
				counter += 1
				max = i
		return counter


class Solution:

	def countBuildings(self, h, n):
		big = 0
		count = 0
		for elem in h:
			if elem > big:
				big = elem
				count += 1
		return count


class Solution:

	def countBuildings(self, h, n):
		x = h[0]
		l = 1
		for i in range(1, n):
			if h[i] > x:
				l += 1
				x = h[i]
		return l


class Solution:

	def countBuildings(self, h, n):
		maxi = h[0]
		count = 1
		for ele in h:
			if ele > maxi:
				count += 1
				maxi = ele
		return count


class Solution:

	def countBuildings(self, arr, n):
		ans = 1
		large = arr[0]
		for elm in arr:
			if elm > large:
				large = elm
				ans += 1
		return ans


class Solution:

	def countBuildings(self, h, n):
		i1 = 0
		m = []
		for i in h:
			if i > i1:
				i1 = i
				m.append(i1)
		return len(m)


class Solution:

	def countBuildings(self, h, n):
		max_hight = h[0]
		can_see = 1
		for i in h[1:]:
			if i > max_hight:
				max_hight = i
				can_see += 1
		return can_see


class Solution:

	def countBuildings(self, h, n):
		mx = 0
		c = 0
		for i in h:
			if mx < i:
				mx = i
				c += 1
		return c


class Solution:

	def countBuildings(self, h, n):
		count = 1
		max = h[0]
		for j in range(n):
			if h[j] > max:
				count = count + 1
				max = h[j]
		return count


class Solution:

	def countBuildings(self, h, n):
		l = [h[0]]
		m = max(h)
		for i in range(1, n):
			if l[-1] < h[i]:
				l.append(h[i])
		return len(l)


class Solution:

	def countBuildings(self, h, n):
		l = 0
		ll = []
		if h == sorted(h):
			return 0
		else:
			for i in range(len(h)):
				if h[i] > l:
					l = h[i]
					ll.append(l)
			return len(ll)


class Solution:

	def countBuildings(self, h, n):
		mx = h[0]
		c = 1
		j = 1
		while j < n:
			if mx < h[j]:
				mx = h[j]
				c += 1
			j += 1
		return c


class Solution:

	def countBuildings(self, h, n):
		t = 1
		maxx = h[0]
		for i in range(1, n):
			if h[i] > maxx:
				t += 1
				maxx = h[i]
		return t


class Solution:

	def countBuildings(self, h, n):
		if n <= 1:
			return n
		memo = [0] * n
		memo[0] = h[0]
		for i in range(1, n):
			memo[i] = max(memo[i - 1], h[i])
		ans = 1
		for i in range(1, n):
			if h[i] > memo[i - 1]:
				ans += 1
		return ans


class Solution:

	def countBuildings(self, h, n):
		list = []
		curr_max = 0
		for i in range(n):
			if h[i] > curr_max:
				curr_max = h[i]
				list.append(curr_max)
		return len(list)


class Solution:

	def countBuildings(self, h, n):
		res = []
		max = 0
		for i in range(n):
			if h[i] > max:
				max = h[i]
				res.append(max)
		return len(res)


class Solution:

	def countBuildings(self, h, n):
		l = []
		a = 0
		for i in range(n):
			if h[i] > a:
				a = h[i]
				l.append(i)
		return len(l)


class Solution:

	def countBuildings(self, h, n):
		max_height = 0