-
Notifications
You must be signed in to change notification settings - Fork 0
/
2154.py
48 lines (33 loc) · 1.15 KB
/
2154.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from typing import List
import unittest
from collections import defaultdict
# https://leetcode.com/problems/keep-multiplying-found-values-by-two/
class Solution:
def _search(self, array: List[int], value: int) -> bool:
left = 0
right = len(array) - 1
while left <= right:
middle = (left + right) // 2
if array[middle] == value:
return True
elif value > array[middle]:
left = middle + 1
elif value < array[middle]:
right = middle - 1
return False
def findFinalValue(self, nums: List[int], original: int) -> int:
nums = list(sorted(nums))
while self._search(nums, original):
original *= 2
return original
class Test(unittest.TestCase):
def setUp(self):
self.solution = Solution()
def test_first(self):
self.assertEqual(
self.solution.findFinalValue(nums=[5, 3, 6, 1, 12], original=3), 24
)
def test_second(self):
self.assertEqual(self.solution.findFinalValue(nums=[2, 7, 9], original=4), 4)
if __name__ == '__main__':
unittest.main()