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.
- Prepare
- Algorithms
- Dynamic Programming
- Equal
- Discussions
Equal
Equal
Sort by
recency
|
468 Discussions
|
Please Login in order to post a comment
Can someone explain what's wrong in this code? I am adding the difference between max and min(in rounds of 1,2,5) to each element except the max element and updating the array to perform above operation again until i get diff=0. I am able to solve in less operations compared to output of test cases, which is weird. Here's the code:
import math import os import random import re import sys
#
Complete the 'equal' function below.
#
The function is expected to return an INTEGER.
The function accepts INTEGER_ARRAY arr as parameter.
#
def split_diff(diff, splitrounds): if diff == 1: splitrounds = splitrounds + 1 elif diff < 5 and diff >= 2: quotient = diff // 2 # print("quo:", quotient) remainder = diff % 2 splitrounds = splitrounds + quotient + remainder elif diff >= 5: quotient = diff // 5 remainder = diff % 5 if remainder != 0: splitrounds = splitrounds + quotient + split_diff(remainder, 0) else: splitrounds = splitrounds + quotient
def equal(arr, rounds):
if name == 'main': fptr = open(os.environ['OUTPUT_PATH'], 'w')
Can someone explain what's wrong in this code? I am adding the difference between max and min(in rounds of 1,2,5) to each element except the max element and updating the array to perform above operation again until i get diff=0. I am able to solve in less operations compared to output of test cases, which is weird. Here's the code:
!/bin/python3
import math import os import random import re import sys
def split_diff(diff, splitrounds): if diff == 1: splitrounds = splitrounds + 1 elif diff < 5 and diff >= 2: quotient = diff // 2 remainder = diff % 2 splitrounds = splitrounds + quotient + remainder elif diff >= 5: quotient = diff // 5 remainder = diff % 5 if remainder != 0: splitrounds = splitrounds + quotient + split_diff(remainder, 0) else: splitrounds = splitrounds + quotient
def equal(arr, rounds):
if name == 'main': fptr = open(os.environ['OUTPUT_PATH'], 'w')
The key perspective is that adding candies to all but one colleague is mathematically the same as substracting from that one colleague the amount added to everyone else.
Well, after reinspecting my solution a number of times, looks like I managed to solve the equalization with quite few rounds than the tests are pointing... :D
I can't see where it could be going wrong, it reduces the gap by 5 preferrably, then 2, and 1 given the maximum difference between elements in the list to get as fewer steps as possible, never touching the greater value in the list, then reduces the list to make it distinct.
Checking the log, it goes down to a single element meaning all item gaps reduced to 0. Here's the funny part, with way lesser rounds than the tests expected output.
Here it is in Java 8 (comments are welcome):
`
class Result {
}
`
Powerful ERP for SMEs in India - Chirix by JM-Origin
Transform your SME with Chirix ERP from JM-Origin — a cloud-based, scalable system built for Indian businesses to boost efficiency, cut costs & drive growth.