Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package g1301_1400.s1354_construct_target_array_with_multiple_sums;

// #Hard #Array #Heap_Priority_Queue #2022_03_21_Time_2_ms_(100.00%)_Space_56.7_MB_(76.64%)

public class Solution {
public boolean isPossible(int[] target) {
long sum = target[0];
int maxIndex = 0;
for (int i = 1; i < target.length; i++) {
sum += target[i];
if (target[i] > target[maxIndex]) {
maxIndex = i;
}
}
long remainingSum = sum - target[maxIndex];
if (target[maxIndex] == 1 || remainingSum == 1) {
return true;
}
if (remainingSum >= target[maxIndex]
|| remainingSum == 0
|| target[maxIndex] % remainingSum == 0) {
return false;
}
target[maxIndex] %= remainingSum;
return isPossible(target);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
1354\. Construct Target Array With Multiple Sums

Hard

You are given an array `target` of n integers. From a starting array `arr` consisting of `n` 1's, you may perform the following procedure :

* let `x` be the sum of all elements currently in your array.
* choose index `i`, such that `0 <= i < n` and set the value of `arr` at index `i` to `x`.
* You may repeat this procedure as many times as needed.

Return `true` _if it is possible to construct the_ `target` _array from_ `arr`_, otherwise, return_ `false`.

**Example 1:**

**Input:** target = [9,3,5]

**Output:** true

**Explanation:** Start with arr = [1, 1, 1]

[1, 1, 1], sum = 3 choose index 1

[1, 3, 1], sum = 5 choose index 2

[1, 3, 5], sum = 9 choose index 0

[9, 3, 5] Done

**Example 2:**

**Input:** target = [1,1,1,2]

**Output:** false

**Explanation:** Impossible to create target array from [1,1,1,1].

**Example 3:**

**Input:** target = [8,5]

**Output:** true

**Constraints:**

* `n == target.length`
* <code>1 <= n <= 5 * 10<sup>4</sup></code>
* <code>1 <= target[i] <= 10<sup>9</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package g1301_1400.s1356_sort_integers_by_the_number_of_1_bits;

// #Easy #Array #Sorting #Bit_Manipulation #Counting
// #Programming_Skills_I_Day_11_Containers_&_Libraries
// #2022_03_21_Time_10_ms_(65.50%)_Space_46.2_MB_(40.10%)

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Solution {
public int[] sortByBits(int[] arr) {
Map<Integer, List<Integer>> map = new HashMap<>();
for (int num : arr) {
int count = Integer.bitCount(num);
if (!map.containsKey(count)) {
map.put(count, new ArrayList<>());
}
map.get(count).add(num);
}
int[] result = new int[arr.length];
int i = 0;
for (int count : map.keySet()) {
List<Integer> list = map.get(count);
Collections.sort(list);
for (int num : list) {
result[i++] = num;
}
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
1356\. Sort Integers by The Number of 1 Bits

Easy

You are given an integer array `arr`. Sort the integers in the array in ascending order by the number of `1`'s in their binary representation and in case of two or more integers have the same number of `1`'s you have to sort them in ascending order.

Return _the array after sorting it_.

**Example 1:**

**Input:** arr = [0,1,2,3,4,5,6,7,8]

**Output:** [0,1,2,4,8,3,5,6,7] **Explantion:** [0] is the only integer with 0 bits.

[1,2,4,8] all have 1 bit.

[3,5,6] have 2 bits.

[7] has 3 bits.

The sorted array by bits is [0,1,2,4,8,3,5,6,7]

**Example 2:**

**Input:** arr = [1024,512,256,128,64,32,16,8,4,2,1]

**Output:** [1,2,4,8,16,32,64,128,256,512,1024] **Explantion:** All integers have 1 bit in the binary representation, you should just sort them in ascending order.

**Constraints:**

* `1 <= arr.length <= 500`
* <code>0 <= arr[i] <= 10<sup>4</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package g1301_1400.s1357_apply_discount_every_n_orders;

// #Medium #Array #Hash_Table #Design #2022_03_21_Time_209_ms_(27.73%)_Space_74.4_MB_(74.79%)

import java.util.Arrays;

public class Cashier {

private int customer = 1;
private final int n;
private final double discount;
private final int[] products;
private final int[] prices;

public Cashier(int n, int discount, int[] products, int[] prices) {
this.n = n;
this.discount = (double) discount;
this.products = products;
this.prices = prices;
}

public double getBill(int[] product, int[] amount) {
double bill = 0.0;
int[] productsInt = this.products;
Integer[] products = Arrays.stream(productsInt).boxed().toArray(Integer[]::new);
int[] prices = this.prices;
for (int i = 0; i < product.length; i++) {
int ithAmount = amount[i];
int id = product[i];
int price = prices[Arrays.asList(products).indexOf(id)];
bill += price * ithAmount;
}
if (this.customer % n == 0) {
this.customer++;
return discounted(this.discount, bill);
}
this.customer++;
return bill;
}

public double discounted(double discount, double price) {
return price * (1 - (discount / 100));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
1356\. Sort Integers by The Number of 1 Bits

Easy

You are given an integer array `arr`. Sort the integers in the array in ascending order by the number of `1`'s in their binary representation and in case of two or more integers have the same number of `1`'s you have to sort them in ascending order.

Return _the array after sorting it_.

**Example 1:**

**Input:** arr = [0,1,2,3,4,5,6,7,8]

**Output:** [0,1,2,4,8,3,5,6,7] **Explantion:** [0] is the only integer with 0 bits.

[1,2,4,8] all have 1 bit.

[3,5,6] have 2 bits.

[7] has 3 bits.

The sorted array by bits is [0,1,2,4,8,3,5,6,7]

**Example 2:**

**Input:** arr = [1024,512,256,128,64,32,16,8,4,2,1]

**Output:** [1,2,4,8,16,32,64,128,256,512,1024] **Explantion:** All integers have 1 bit in the binary representation, you should just sort them in ascending order.

**Constraints:**

* `1 <= arr.length <= 500`
* <code>0 <= arr[i] <= 10<sup>4</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package g1301_1400.s1358_number_of_substrings_containing_all_three_characters;

// #Medium #String #Hash_Table #Sliding_Window
// #2022_03_21_Time_15_ms_(53.82%)_Space_44.9_MB_(64.12%)

public class Solution {
public int numberOfSubstrings(String s) {
int[] counts = new int[3];
int i = 0;
int n = s.length();
int result = 0;
for (int j = 0; j < n; j++) {
counts[s.charAt(j) - 'a']++;
while (counts[0] > 0 && counts[1] > 0 && counts[2] > 0) {
counts[s.charAt(i++) - 'a']--;
}
result += i;
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
1358\. Number of Substrings Containing All Three Characters

Medium

Given a string `s` consisting only of characters _a_, _b_ and _c_.

Return the number of substrings containing **at least** one occurrence of all these characters _a_, _b_ and _c_.

**Example 1:**

**Input:** s = "abcabc"

**Output:** 10

**Explanation:** The substrings containing at least one occurrence of the characters _a_, _b_ and _c are "_abc_", "_abca_", "_abcab_", "_abcabc_", "_bca_", "_bcab_", "_bcabc_", "_cab_", "_cabc_"_ and _"_abc_"_ (**again**)_._

**Example 2:**

**Input:** s = "aaacb"

**Output:** 3

**Explanation:** The substrings containing at least one occurrence of the characters _a_, _b_ and _c are "_aaacb_", "_aacb_"_ and _"_acb_"._

**Example 3:**

**Input:** s = "abc"

**Output:** 1

**Constraints:**

* `3 <= s.length <= 5 x 10^4`
* `s` only consists of _a_, _b_ or _c _characters.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package g1301_1400.s1359_count_all_valid_pickup_and_delivery_options;

// #Hard #Dynamic_Programming #Math #Combinatorics
// #2022_03_21_Time_0_ms_(100.00%)_Space_40.9_MB_(58.06%)

public class Solution {
public int countOrders(int n) {

long[] dp = new long[n + 1];
dp[1] = 1;

long mod = (long) 1e9 + 7;

for (int i = 2; i <= n; i++) {

long gaps = (i - 1) * 2L + 1;

dp[i] = gaps * (gaps + 1) / 2 * dp[i - 1] % mod;
}

return (int) dp[n];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
1359\. Count All Valid Pickup and Delivery Options

Hard

Given `n` orders, each order consist in pickup and delivery services.

Count all valid pickup/delivery possible sequences such that delivery(i) is always after of pickup(i).

Since the answer may be too large, return it modulo 10^9 + 7.

**Example 1:**

**Input:** n = 1

**Output:** 1

**Explanation:** Unique order (P1, D1), Delivery 1 always is after of Pickup 1.

**Example 2:**

**Input:** n = 2

**Output:** 6

**Explanation:** All possible orders:

(P1,P2,D1,D2), (P1,P2,D2,D1), (P1,D1,P2,D2), (P2,P1,D1,D2), (P2,P1,D2,D1) and (P2,D2,P1,D1).

This is an invalid order (P1,D2,P2,D1) because Pickup 2 is after of Delivery 2.

**Example 3:**

**Input:** n = 3

**Output:** 90

**Constraints:**

* `1 <= n <= 500`
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package g1301_1400.s1354_construct_target_array_with_multiple_sums;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void isPossible() {
assertThat(new Solution().isPossible(new int[] {9, 3, 5}), equalTo(true));
}

@Test
void isPossible2() {
assertThat(new Solution().isPossible(new int[] {1, 1, 1, 2}), equalTo(false));
}

@Test
void isPossible3() {
assertThat(new Solution().isPossible(new int[] {8, 5}), equalTo(true));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package g1301_1400.s1356_sort_integers_by_the_number_of_1_bits;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void sortByBits() {
assertThat(
new Solution().sortByBits(new int[] {0, 1, 2, 3, 4, 5, 6, 7, 8}),
equalTo(new int[] {0, 1, 2, 4, 8, 3, 5, 6, 7}));
}

@Test
void sortByBits2() {
assertThat(
new Solution().sortByBits(new int[] {1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1}),
equalTo(new int[] {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024}));
}
}
Loading