

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Cell fusion in Python
Suppose we have a list of numbers called cells; this list is representing sizes of different cells. Now, in each iteration, the two largest cells a and b interact according to these rules: So, If a = b, they both die. Otherwise, the two cells merge and their size becomes floor of ((a + b) / 3). We have to find the size of the last cell or return -1 if there's no cell is remaining.
So, if the input is like [20,40,40,30], then the output will be 16, in first iteration, 40 and 40 will die, then 20 and 30 become floor of ((20+30) / 3) = floor of 50/3 = 16
To solve this, we will follow these steps −
cells := convert each value of cells array negative
make a heap with cells
while cells is not empty, do −
delete two elements from cells and convert them negative again, and assign them as first and second consecutively
if first is not equal to second, then −
insert negative of floor of (first+second)/3) into heap
return negative of cells[0] if cells has some elements otherwise - 1
Let us see the following implementation to get better understanding −
Example
from heapq import heapify, heappop, heappush class Solution: def solve(self, cells): cells=[-x for x in cells] heapify(cells) while len(cells)>1: first,second = -heappop(cells), -heappop(cells) if first!=second: heappush(cells, -((first+second)//3)) return -cells[0] if cells else -1 ob = Solution() cells = [20,40,40,30] print(ob.solve(cells))
Input
[20,40,40,30]
Output
16
- Related Questions & Answers
- C++ Program to Implement Fusion Tree
- Difference between Fusion Welding and Solid State Welding
- Program to find largest island after changing one water cell to land cell in Python
- Working Principle of Voltaic Cell (Galvanic Cell)
- Cell Count After Removing Corner Diagonals in Python
- Find paths from corner cell to middle cell in maze in C++
- Program to find next state of next cell matrix state in Python?
- How to set cell padding in HTML?
- Dynamically change TableView Cell height in Swift
- Get specific value of cell in MySQL
- Check if a Queen can attack a given cell on chessboard in Python
- How to write value inside a cell in a worksheet in Selenium with python?
- Final cell position in the matrix in C++
- Change One Cell's Data in MySQL?
- Explain the ATM Cell Structure in Computer Network