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

Filter by extension

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,60 @@
package g3901_4000.s3946_maximum_number_of_items_from_sale_i;

// #Medium #Array #Dynamic_Programming #Greedy #Staff #Weekly_Contest_504
// #2026_10_20_Time_13_ms_(100.00%)_Space_46.50_MB_(96.15%)

public class Solution {
public int maximumSaleItems(int[][] items, int budget) {
int n = items.length;
int maxFactor = 0;
int minPrice = Integer.MAX_VALUE;
// Find the maximum factor (for our frequency array) and the minimum price
for (int[] item : items) {
maxFactor = Math.max(maxFactor, item[0]);
minPrice = Math.min(minPrice, item[1]);
}
// Count frequencies of each factor
int[] freq = new int[maxFactor + 1];
for (int[] item : items) {
freq[item[0]]++;
}
// Precompute how many free items we get by buying at least 1 copy of item i
int[] free = new int[n];
for (int i = 0; i < n; i++) {
int f = items[i][0];
int count = 0;
// Sum up the frequencies of all multiples of factor f
for (int mult = f; mult <= maxFactor; mult += f) {
count += freq[mult];
}
// Subtract 1 because we shouldn't count the item itself as a free copy
free[i] = count - 1;
}
// 0-1 Knapsack DP
// dp[c] stores the max items we can get by spending exactly 'c' budget to activate items
int[] dp = new int[budget + 1];
java.util.Arrays.fill(dp, -1);
dp[0] = 0;
for (int i = 0; i < n; i++) {
int weight = items[i][1];
int value = 1 + free[i];
for (int c = budget; c >= weight; c--) {
if (dp[c - weight] != -1) {
dp[c] = Math.max(dp[c], dp[c - weight] + value);
}
}
}
// Evaluate the best combination of activated items + leftover budget spent on cheapest
// items
int maxTotal = 0;
for (int c = 0; c <= budget; c++) {
if (dp[c] != -1) {
// dp[c] gives us the activation items.
// We buy the cheapest item repeatedly with the remaining budget.
int currentTotal = dp[c] + (budget - c) / minPrice;
maxTotal = Math.max(maxTotal, currentTotal);
}
}
return maxTotal;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
3946\. Maximum Number of Items From Sale I

Medium

You are given a 2D integer array `items`, where <code>items[i] = [factor<sub>i</sub>, price<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> item. You are also given an integer `budget`.

There are unlimited copies of each item available for purchase.You may buy any number of copies of any items such that the total cost of the purchased copies is at most `budget`.

After buying items, you may receive free copies according to the following rules:

* For each item `i` that you bought **at least one copy** of, you receive **one free copy** of every item `j` such that `j != i` and <code>factor<sub>i</sub></code> divides <code>factor<sub>j</sub></code>.
* Buying multiple copies of the same item `i` does **not** give additional free copies through item `i`.
* The same item `j` can be received multiple times for free if it is received from purchases of different item types.

Return the **maximum total number of item copies** you can obtain, including both purchased copies and free copies, while spending at most `budget` on purchased items.

**Example 1:**

**Input:** items = [[6,2],[2,6],[3,4]], budget = 9

**Output:** 4

**Explanation:**

* You can buy 2 copies of item 0 and 1 copy of item 2 for a total cost of `2 * 2 + 4 = 8`, which is not greater than `budget = 9`.
* Buying item 2 gives 1 free copy of item 0, because <code>factor<sub>2</sub> = 3</code> divides <code>factor<sub>0</sub> = 6</code>.
* You leave with 3 purchased copies and 1 free copy, for a total of 4 item copies.

**Example 2:**

**Input:** items = [[2,4],[3,2],[4,1],[6,4],[12,4]], budget = 8

**Output:** 10

**Explanation:**

* You can buy 1 copy of item 0, 1 copy of item 1, and 2 copies of item 2 for a total cost of `4 + 2 + 2 * 1 = 8`.
* Buying item 0 gives 1 free copy of items 2, 3, and 4.
* Buying item 1 gives 1 free copy of items 3 and 4.
* Buying item 2 gives 1 free copy of item 4.
* Thus, you receive 6 free copies. You leave with 4 purchased copies and 6 free copies, for a total of 10 item copies.

**Constraints:**

* `1 <= items.length <= 1000`
* <code>items[i] = [factor<sub>i</sub>, price<sub>i</sub>]</code>
* <code>1 <= factor<sub>i</sub>, price<sub>i</sub> <= 1500</code>
* `1 <= budget <= 1500`
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package g3901_4000.s3947_maximum_number_of_items_from_sale_ii;

// #Medium #Array #Sorting #Greedy #Heap_Priority_Queue #Staff #Weekly_Contest_504
// #2026_10_20_Time_20_ms_(82.93%)_Space_231.68_MB_(87.81%)

import java.util.Arrays;

public class Solution {
public int maximumSaleItems(int[][] items, int budget) {
int n = items.length;
int[] count = new int[n + 1];
long min = Long.MAX_VALUE;
for (int[] item : items) {
count[item[0]]++;
if (item[1] < min) {
min = item[1];
}
}
int[] temp = new int[n + 1];
for (int i = 1; i <= n; i++) {
if (count[i] > 0) {
int m = -1;
for (int j = i; j <= n; j += i) {
m += count[j];
}
temp[i] = m;
}
}
long[] deal = new long[n];
int dealcount = 0;
long limit = min * 2;
for (int[] item : items) {
int first = item[0];
int second = item[1];
if (second < limit) {
int m = temp[first];
if (m > 0) {
deal[dealcount++] = ((long) second << 32) | (m & 0xFFFFFFFFL);
}
}
}
Arrays.sort(deal, 0, dealcount);
long total = 0;
for (int i = 0; i < dealcount; i++) {
long deals = deal[i];
int m = (int) (deals >>> 32);
int counts = (int) (deals & 0xFFFFFFFFL);
if (budget >= m) {
long time = Math.min((long) counts, budget / m);
budget = (int) (budget - (time * m));
total += 2 * time;
}
}
total += budget / min;
return (int) total;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
3947\. Maximum Number of Items From Sale II

Medium

You are given a 2D integer array `items`, where <code>items[i] = [factor<sub>i</sub>, price<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> item. You are also given an integer `budget`.

There are unlimited copies of each item available for purchase. You may buy any number of copies of any items such that the total cost of the purchased copies is at most `budget`.

After buying items, you may receive free copies according to the following rules:

* Each purchased copy of item `i` can give you **at most one** free copy of another item `j`.
* The free item must satisfy `i != j` and <code>factor<sub>i</sub></code> divides <code>factor<sub>j</sub></code>.
* For each ordered pair `(i, j)`, you can receive a free copy of item `j` from purchases of item `i` **at most once**, regardless of how many copies of item `i` you buy.
* The same item `j` can be received multiple times for free if it is received from purchases of different item types.

Return the **maximum total number of item copies** you can obtain, including both purchased copies and free copies, while spending at most `budget` on purchased items.

**Example 1:**

**Input:** items = [[1,6],[2,4],[3,5]], budget = 19

**Output:** 5

**Explanation:**

* You can buy 2 copies of item 0 and 1 copy of item 1 for a total cost of `2 * 6 + 4 = 16`, which is not greater than `budget = 19`.
* One purchased copy of item 0 gives 1 free copy of item 1, because <code>factor<sub>0</sub> = 1</code> divides <code>factor<sub>1</sub> = 2</code>.
* The other purchased copy of item 0 gives 1 free copy of item 2, because <code>factor<sub>0</sub> = 1</code> divides <code>factor<sub>2</sub> = 3</code>.
* You leave with 3 purchased copies and 2 free copies, for a total of 5 item copies.

**Example 2:**

**Input:** items = [[2,8],[1,10],[6,6],[4,12],[5,20],[5,17]], budget = 35

**Output:** 7

**Explanation:**

* You can buy 2 copies of item 0, 1 copy of item 1, and 1 copy of item 2 for a total cost of `2 * 8 + 10 + 6 = 32`, which is not greater than `budget = 35`.
* One purchased copy of item 0 gives 1 free copy of item 2, because <code>factor<sub>0</sub> = 2</code> divides <code>factor<sub>2</sub> = 6</code>.
* The other purchased copy of item 0 gives 1 free copy of item 3, because <code>factor<sub>0</sub> = 2</code> divides <code>factor<sub>3</sub> = 4</code>.
* The purchased copy of item 1 gives 1 free copy of item 2, because <code>factor<sub>1</sub> = 1</code> divides <code>factor<sub>2</sub> = 6</code>.
* Buying item 2 gives no free copy, because <code>factor<sub>2</sub> = 6</code> does not divide the factor of any other item.
* You leave with 4 purchased copies and 3 free copies, for a total of 7 item copies.

**Constraints:**

* <code>1 <= items.length <= 10<sup>5</sup></code>
* <code>items[i] = [factor<sub>i</sub>, price<sub>i</sub>]</code>
* <code>1 <= factor<sub>i</sub> <= items.length</code>
* <code>1 <= price<sub>i</sub> <= 10<sup>9</sup></code>
* <code>1 <= budget <= 10<sup>9</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package g3901_4000.s3948_lexicographically_maximum_mex_array;

// #Hard #Array #Hash_Table #Greedy #Queue #Senior_Staff #Weekly_Contest_504
// #2026_10_20_Time_4_ms_(100.00%)_Space_68.51_MB_(66.67%)

public class Solution {
public int[] maximumMEX(int[] nums) {
int n = nums.length;
int[] suffixMex = new int[n];
boolean[] seen = new boolean[n + 2];
int currMex = 0;
for (int i = n - 1; i >= 0; i--) {
if (nums[i] < n + 2) {
seen[nums[i]] = true;
}
while (seen[currMex]) {
currMex++;
}
suffixMex[i] = currMex;
}
int[] result = new int[n];
int resIdx = 0;
int[] seenAtStep = new int[n + 2];
int stepCounter = 0;
int i = 0;
while (i < n) {
int m = suffixMex[i];
result[resIdx++] = m;
if (m == 0) {
i++;
} else {
stepCounter++;
int found = 0;
int j = i;
while (j < n) {
if (nums[j] < m && seenAtStep[nums[j]] != stepCounter) {
seenAtStep[nums[j]] = stepCounter;
found++;
}
if (found == m) {
i = j + 1;
break;
}
j++;
}
}
}
int[] finalResult = new int[resIdx];
System.arraycopy(result, 0, finalResult, 0, resIdx);
return finalResult;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
3948\. Lexicographically Maximum MEX Array

Hard

You are given an integer array `nums`.

You want to construct an array `result` by repeatedly performing the following operation until `nums` becomes empty:

* Choose an integer `k` such that `1 <= k <= len(nums)`.
* Compute the **MEX** of the first `k` elements of `nums`.
* Append this **MEX** to `result`.
* Remove the first `k` elements from `nums`.

Return the **lexicographically maximum** array `result` that can be obtained after performing the operations.

The **MEX** of an array is the **smallest non-negative** integer not present in the array.

An array `a` is **lexicographically greater** than an array `b` if in the first position where `a` and `b` differ, array `a` has an element that is greater than the corresponding element in `b`. If the first `min(a.length, b.length)` elements do not differ, then the longer array is the **lexicographically greater** one.

**Example 1:**

**Input:** nums = [0,1,0]

**Output:** [2,1]

**Explanation:**

* Take the first `k = 2` elements `[0, 1]` which has MEX = 2. Current `result = [2]`.
* Remaining array `[0]` has MEX = 1. Thus, the final `result = [2, 1]`.

**Example 2:**

**Input:** nums = [1,0,2]

**Output:** [3]

**Explanation:**

* Take the first `k = 3` elements `[1, 0, 2]` which has MEX = 3.
* `nums` is now empty. Thus, the final `result = [3]`.

**Example 3:**

**Input:** nums = [3,1]

**Output:** [0,0]

**Explanation:**

* Take `k = 1`, first element `[3]` has MEX = 0. Current `result = [0]`.
* Remaining array `[1]` has MEX = 0. Thus, the final `result = [0, 0]`.

**Constraints:**

* <code>1 <= nums.length <= 10<sup>5</sup></code>
* <code>0 <= nums[i] <= 10<sup>5</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package g3901_4000.s3950_exactly_one_consecutive_set_bits_pair;

// #Easy #Bit_Manipulation #Mid_Level #Biweekly_Contest_184
// #2026_10_20_Time_1_ms_(98.26%)_Space_42.21_MB_(89.58%)

public class Solution {
public boolean consecutiveSetBits(int n) {
int cnt = 0;
while (n != 0) {
if ((n & 3) == 3) {
cnt++;
}
n = n >> 1;
}
return cnt == 1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
3950\. Exactly One Consecutive Set Bits Pair

Easy

You are given an integer `n`.

Return `true` if its binary representation contains **exactly one adjacent pair** of set bits, and `false` otherwise.

**Example 1:**

**Input:** n = 6

**Output:** true

**Explanation:**

* Binary representation of 6 is `110`.
* There is exactly one adjacent pair of set bits (`"11"`). Thus, the answer is `true`.

**Example 2:**

**Input:** n = 5

**Output:** false

**Explanation:**

* Binary representation of 5 is `101`.
* There is no adjacent pair of set bits. Thus, the answer is `false`.

**Constraints:**

* <code>0 <= n <= 10<sup>5</sup></code>
Loading
Loading