Beautiful Pairs : Hacker Rank

Problem : https://www.hackerrank.com/challenges/beautiful-pairs

Logic : Straight forward logic it is. Finding similar pairs which index occur exactaly once.So, I am using dictionary in python 3, use map in c++ or in java and counting the occurance of numbers and then minimum occurance from both array are desire number of pairs.

Language : Python3

 

import math
n=int(input())
a=list(map(int, input().split()))
b=list(map(int, input().split()))
da={}
db={}
ask=[]
cost=0
for i in a:
    if i in da:
        da[i]+=1
    else:
        da[i]=1
        ask.append(i)
for j in b:
	if j in db:
		db[j]+=1
	else:
		db[j]=1
for k in da:
    if k in db:
        cost+=min(da[k],db[k])
    #cost+=min(da[k],db[k])
if cost==n:
	cost-=1
else:
	cost+=1
print(cost)
		

Cutting Board : Hacker Rank

Problem https://www.hackerrank.com/challenges/board-cutting

Logic :  We want to minimum cost. First we sort both horizontal and vertical cost array and start with largest element form both array, because cost is derived from total cutting edges multiply with horizontal or vertical cost. So, minimum cost has been derived by minimum cost multiply with cutting edge.

Language : Python3

 

## Cutting Boards  Greedy ###
## Cutting Boards  Greedy ###
t=int(input())
for i in range(t):
    row,column = list(map(int,input().split()))
    xa= list(map(int,input().split()))
    ya= list(map(int,input().split()))
    xa.sort()
    ya.sort()
    xarr=xa[::-1]
    yarr=ya[::-1]
    pivot_x=0
    pivot_y=0
    cost=0
    cutedge_x=1
    cutedge_y=1
    while pivot_x<row-1 and pivot_y<column-1:
        if xarr[pivot_x]>yarr[pivot_y]:   #horizontal cost is greater than vertical cost
            cost+=cutedge_y*xarr[pivot_x]
            cutedge_x+=1
            pivot_x+=1
        else:
            cost+=cutedge_x*yarr[pivot_y]      #vertical cost is greater than horizontal cost
            cutedge_y+=1
            pivot_y+=1
    while pivot_x>=row-1 and pivot_y<column-1:   # adding remaining vertical cost
        cost+=cutedge_x*yarr[pivot_y]
        cutedge_y+=1
        pivot_y+=1
    while pivot_x<row-1 and pivot_y>=column-1:   # adding remaining horizontal cost
        cost+=cutedge_y*xarr[pivot_x]
        cutedge_x+=1
        pivot_x+=1
    print(cost%1000000007)


HackerRank – Grid Challenge

Problem :- https://www.hackerrank.com/challenges/grid-challenge

My Logic :- I first shorted all horizontal arrays or lists[python]. Then I compared each vertical element by it’s next vertical element that it is greater than its previous one or not.

Language :- Python 3

 

 test = int(input())  #taking total tastcase
for i in range(0,test):
    n = int(input())
    grid = [[i for i in input().strip()]for j in range(n)] #taking input of arrays
    for k in range(n):
        grid[k].sort()      #sorting all horizontal arrays(ROWS)
    boolean = False
    for x in range(n):
        if boolean:
            break
        for y in range(n-1):
            if(grid[y][x]&gt;grid[y+1][x]): boolean=True    #checking vertical element #is greater than its previous one or not, if 'not'then boolean=TRUE
    print('NO' if boolean else 'YES')