AnswerBun.com

Better performance in solution for the Dole Out Cadbury challenge

Code Review Asked by poojitha yadavalli on January 2, 2022

Dole Out Cadbury

Problem Description

You are a teacher in reputed school. During Celebration Day you were assigned a task to distribute Cadbury such that maximum children get the chocolate. You have a box full of Cadbury with different width and height. You can only distribute largest square shape Cadbury. So if you have a Cadbury of length 10 and width 5, then you need to break Cadbury in 5X5 square and distribute to first child and then remaining 5X5 to next in queue

Constraints

$0<P<Q<1501$
$0<R<S<1501$

Input Format

First line contains an integer P that denotes minimum length of Cadbury in the box
Second line contains an integer Q that denotes maximum length of Cadbury in the box
Third line contains an integer R that denotes minimum width of Cadbury in the box
Fourth line contains an integer S that denotes maximum width of Cadbury in the box

Output
Print total number of children who will get chocolate.

Timeout
1

Example

Input

5
7
3
4

Output

24

Explanation

Length is in between 5 to 7 and width is in between 3 to 4.
So we have 5X3,5X4,6X3,6X4,7X3,7X4 type of Cadbury in the box.
If we take 5X3:

First, we can give 3X3 square Cadbury to 1st child .Then we are left with 3X2. Now largest square is 2X2 which will be given to next child. Next, we are left with two 1X1 part of Cadbury which will be given to another two children.
And so on.

Here is my code, which works but returns . I’m looking for optimizations.

def cadbury(l,b):
    count = 0
    while True:
        lon=max(l,b)
        sh=min(l,b)
        count+=1
        diff=lon-sh
        if diff==0:
            return count
        else :
            l=min(l,b)
            b=diff
minl=int(input())
maxl=int(input())
minw=int(input())
maxw=int(input())
count=0
for i in range(minl,maxl+1):
    for j in range(minw,maxw+1):
        count+=cadbury(i,j)
print(count)

Add your own answers!

Related Questions

3-SAT Solver Python

2  Asked on December 12, 2020 by n00bster

       

Operations on nested data

1  Asked on December 11, 2020 by kluvin

       

Library Management System OOP with c++( Part2 of a series )

2  Asked on December 10, 2020 by theprogrammer

   

A Python Blackjack terminal based game

1  Asked on December 8, 2020 by fames

       

Monte carlo simulation in C

1  Asked on December 8, 2020 by kartik-chhajed

     

Hangman Game in Python-3

1  Asked on December 7, 2020 by phinn-galactica

       

Simple parser using Flex and C++

2  Asked on December 5, 2020

       

PHP array with array value into multiple arrays with same key

1  Asked on December 1, 2020 by robert-wilde

 

Wi-fi scheduler written in C

2  Asked on November 30, 2020 by nnncomplex

         

Border Radius in OpenGL

0  Asked on November 28, 2020 by michaelsnowden

 

Does this SASS make sense?

1  Asked on November 16, 2020 by neil-meyer

 

Ensuring if an object is Assignable from a class type

1  Asked on October 28, 2020 by sourabh

   

Mergesort in python

1  Asked on October 28, 2020 by nishil-patel

         

Selection Sort Java and Analysis

1  Asked on October 24, 2020 by gss

     

Computing on two colums

0  Asked on October 24, 2020 by ajayramesh

   

A* using a Priority Queue

0  Asked on October 17, 2020 by ryan-swann

       

Generic solution to Hibernate’s CriteriaQuery

1  Asked on September 23, 2020 by stefan-georgiev-uzunov

       

Checking if a string contains all unique characters

2  Asked on September 11, 2020 by e-setprimeepsilon

     

Ask a Question

Get help from others!

© 2023 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP