EasyNo. 2154
Keep Multiplying Found Values by Two — LeetCode 2154 Python Solution
- Pattern
- Sorting
- Topics
- Array, Hash Table, Sorting, Simulation
- Reading time
- 2 min
- Source
- leetcode.com
The problem
You are given an array of integers nums. You are also given an integer original which is the first number that needs to be searched for in nums.
Example
- Input
- nums = [5,3,6,1,12], original = 3
- Output
- 24
- Explanation
- - 3 is found in nums. 3 is multiplied by 2 to obtain 6.
Python solution
class Solution:
def findFinalValue(self, nums: List[int], original: int) -> int:
s = set(nums)
while original in s:
original <<= 1
return originalComplexity
- Time
- O(n)
- Space · auxiliary
- O(n)
Pattern: Sorting
Spend O(n log n) once to buy an ordering that makes the rest of the problem trivial. LeetCode 2154. Keep Multiplying Found Values by Two is filed here because LeetCode tags it Sorting, which is the vocabulary this hub collects.
The sorting guide has the Python template for the pattern and the 401 LeetCode problems that use it.
Related problems
| No. | Problem | Difficulty |
|---|---|---|
| 2357 | Make Array Zero by Subtracting Equal Amounts | Easy |
| 2402 | Meeting Rooms III | Hard |
| 2593 | Find Score of an Array After Marking All Elements | Medium |
| 2766 | Relocate Marbles | Medium |
| 0049 | Group Anagrams | Medium |
| 0169 | Majority Element | Easy |
Frequently asked questions
- How hard is LeetCode 2154. Keep Multiplying Found Values by Two?
LeetCode 2154. Keep Multiplying Found Values by Two is rated Easy on LeetCode.
- What is the time complexity of LeetCode 2154. Keep Multiplying Found Values by Two?
The Python solution on this page runs in O(n).
- What is the space complexity of LeetCode 2154. Keep Multiplying Found Values by Two?
The Python solution on this page uses O(n) auxiliary space.
- What topics does LeetCode 2154. Keep Multiplying Found Values by Two cover?
LeetCode 2154. Keep Multiplying Found Values by Two is tagged Array, Hash Table, Sorting and Simulation on LeetCode.