Skip to content

Commit 5ac25c0

Browse files
solves #2154: Keep Multiplying Found Values by Two in java
1 parent b3ea3b0 commit 5ac25c0

3 files changed

+42
-2
lines changed

Diff for: README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -708,8 +708,8 @@
708708
| 2133 | [Check if Every Row and Column Contains All Numbers](https://leetcode.com/problems/check-if-every-row-and-column-contains-all-numbers) | [![Java](assets/java.png)](src/CheckIfEveryRowAndEveryColumnContainAllNumbers.java) | |
709709
| 2138 | [Divide a String Into Groups of Size k](https://leetcode.com/problems/divide-a-string-into-groups-of-size-k) | [![Java](assets/java.png)](src/DivideTheStringIntoGroupsOfSizeK.java) | |
710710
| 2144 | [Minimum Cost of Buying Candies With Discount](https://leetcode.com/problems/minimum-cost-of-buying-candies-with-discount) | [![Java](assets/java.png)](src/MinimumCostOfBuyingCandiesWithDiscount.java) | |
711-
| 2148 | [Count Elements With Strictly Smaller and Greater Elements](https://leetcode.com/problems/count-elements-with-strictly-smaller-and-greater-elements) | | |
712-
| 2154 | [Keep Multiplying Found Values by Two](https://leetcode.com/problems/keep-multiplying-found-values-by-two) | | |
711+
| 2148 | [Count Elements With Strictly Smaller and Greater Elements](https://leetcode.com/problems/count-elements-with-strictly-smaller-and-greater-elements) | [![Java](assets/java.png)](src/CountElementsWithStrictlySmallerAndGreaterElements.java) | |
712+
| 2154 | [Keep Multiplying Found Values by Two](https://leetcode.com/problems/keep-multiplying-found-values-by-two) | [![Java](assets/java.png)](src/KeepMultiplyingFoundValuesByTwo.java) | |
713713
| 2160 | [Minimum Sum of Four Digit Number After Splitting Digits](https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits) | | |
714714
| 2164 | [Sort Even and Odd Indices Independently](https://leetcode.com/problems/sort-even-and-odd-indices-independently) | | |
715715
| 2169 | [Count Operations to Obtain Zero](https://leetcode.com/problems/count-operations-to-obtain-zero) | | |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// https://leetcode.com/problems/count-elements-with-strictly-smaller-and-greater-elements
2+
// T: O(n log(n))
3+
// S: O(log(n))
4+
5+
import java.util.Arrays;
6+
7+
public class CountElementsWithStrictlySmallerAndGreaterElements {
8+
public int countElements(int[] nums) {
9+
Arrays.sort(nums);
10+
int count = 0;
11+
for (int index = 1 ; index < nums.length - 1 ; index++) {
12+
if (nums[0] != nums[index] && nums[index] != nums[nums.length - 1]) count++;
13+
}
14+
return count;
15+
}
16+
}

Diff for: src/KeepMultiplyingFoundValuesByTwo.java

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// https://leetcode.com/problems/keep-multiplying-found-values-by-two
2+
// T: O(N)
3+
// S: O(N)
4+
5+
import java.util.HashSet;
6+
import java.util.Set;
7+
8+
public class KeepMultiplyingFoundValuesByTwo {
9+
public int findFinalValue(int[] nums, int original) {
10+
final Set<Integer> numbers = setFrom(nums);
11+
while (numbers.contains(original)) {
12+
original *= 2;
13+
}
14+
return original;
15+
}
16+
17+
private Set<Integer> setFrom(int[] numbers) {
18+
final Set<Integer> set = new HashSet<>();
19+
for (int element : numbers) {
20+
set.add(element);
21+
}
22+
return set;
23+
}
24+
}

0 commit comments

Comments
 (0)