Skip to content

Commit 00e7ae2

Browse files
committed
2154. Keep Multiplying Found Values by Two: AC
1 parent f6e98b3 commit 00e7ae2

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

src/solution/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1627,3 +1627,4 @@ mod s2148_count_elements_with_strictly_smaller_and_greater_elements;
16271627
mod s2149_rearrange_array_elements_by_sign;
16281628
mod s2150_find_all_lonely_numbers_in_the_array;
16291629
mod s2151_maximum_good_people_based_on_statements;
1630+
mod s2154_keep_multiplying_found_values_by_two;
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/**
2+
* [2154] Keep Multiplying Found Values by Two
3+
*
4+
* 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.
5+
* You then do the following steps:
6+
* <ol>
7+
* If original is found in nums, multiply it by two (i.e., set original = 2 * original).
8+
* Otherwise, stop the process.
9+
* Repeat this process with the new number as long as you keep finding the number.
10+
* </ol>
11+
* Return the final value of original.
12+
*
13+
* Example 1:
14+
*
15+
* Input: nums = [5,3,6,1,12], original = 3
16+
* Output: 24
17+
* Explanation:
18+
* - 3 is found in nums. 3 is multiplied by 2 to obtain 6.
19+
* - 6 is found in nums. 6 is multiplied by 2 to obtain 12.
20+
* - 12 is found in nums. 12 is multiplied by 2 to obtain 24.
21+
* - 24 is not found in nums. Thus, 24 is returned.
22+
*
23+
* Example 2:
24+
*
25+
* Input: nums = [2,7,9], original = 4
26+
* Output: 4
27+
* Explanation:
28+
* - 4 is not found in nums. Thus, 4 is returned.
29+
*
30+
*
31+
* Constraints:
32+
*
33+
* 1 <= nums.length <= 1000
34+
* 1 <= nums[i], original <= 1000
35+
*
36+
*/
37+
pub struct Solution {}
38+
39+
// problem: https://leetcode.com/problems/keep-multiplying-found-values-by-two/
40+
// discuss: https://leetcode.com/problems/keep-multiplying-found-values-by-two/discuss/?currentPage=1&orderBy=most_votes&query=
41+
42+
// submission codes start here
43+
44+
impl Solution {
45+
pub fn find_final_value(nums: Vec<i32>, original: i32) -> i32 {
46+
let mut nums = nums;
47+
nums.sort_unstable();
48+
nums.into_iter().fold(original, |mut acc, num| {
49+
if num == acc {
50+
acc <<= 1
51+
}
52+
acc
53+
})
54+
}
55+
}
56+
57+
// submission codes end
58+
59+
#[cfg(test)]
60+
mod tests {
61+
use super::*;
62+
63+
#[test]
64+
fn test_2154_example_1() {
65+
let nums = vec![5, 3, 6, 1, 12];
66+
let original = 3;
67+
68+
let result = 24;
69+
70+
assert_eq!(Solution::find_final_value(nums, original), result);
71+
}
72+
73+
#[test]
74+
fn test_2154_example_2() {
75+
let nums = vec![2, 7, 9];
76+
let original = 4;
77+
78+
let result = 4;
79+
80+
assert_eq!(Solution::find_final_value(nums, original), result);
81+
}
82+
}

0 commit comments

Comments
 (0)