Greedy Approach to Solve Array problems
Are you tired of
using two loops to check individual elements ?
If yes, then try this Greedy
Approach to solve these kind of problems.
For e.g.,
1) We have two arrays as given below:
2) First, sort the arrays in descending order:
3) Now,
check the conditions in a single for loop.
Check
below for full greedy approach (PYTHON CODE SNIPPET) :
count,j=0,0
for j in range(N):
if A[count]>B[j]:
count+=1
print(count)
Comment below if it works!!!
Thank You
Does not work for all cases.
ReplyDelete