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,24 @@
package g1401_1500.s1475_final_prices_with_a_special_discount_in_a_shop;

// #Easy #Array #Stack #Monotonic_Stack #2022_04_04_Time_2_ms_(80.33%)_Space_44.8_MB_(17.62%)

public class Solution {
public int[] finalPrices(int[] prices) {
int[] result = new int[prices.length];
for (int i = 0; i < prices.length; i++) {
boolean foundDiscount = false;
for (int j = i + 1; j < prices.length; j++) {
if (prices[j] <= prices[i]) {
result[i] = prices[i] - prices[j];
foundDiscount = true;
break;
}
}
if (!foundDiscount) {
result[i] = prices[i];
}
}
result[prices.length - 1] = prices[prices.length - 1];
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
1475\. Final Prices With a Special Discount in a Shop

Easy

Given the array `prices` where `prices[i]` is the price of the `ith` item in a shop. There is a special discount for items in the shop, if you buy the `ith` item, then you will receive a discount equivalent to `prices[j]` where `j` is the **minimum** index such that `j > i` and `prices[j] <= prices[i]`, otherwise, you will not receive any discount at all.

_Return an array where the `ith` element is the final price you will pay for the `ith` item of the shop considering the special discount._

**Example 1:**

**Input:** prices = [8,4,6,2,3]

**Output:** [4,2,4,2,3]

**Explanation:**

For item 0 with price[0]=8 you will receive a discount equivalent to prices[1]=4, therefore, the final price you will pay is 8 - 4 = 4.

For item 1 with price[1]=4 you will receive a discount equivalent to prices[3]=2, therefore, the final price you will pay is 4 - 2 = 2.

For item 2 with price[2]=6 you will receive a discount equivalent to prices[3]=2, therefore, the final price you will pay is 6 - 2 = 4.

For items 3 and 4 you will not receive any discount at all.

**Example 2:**

**Input:** prices = [1,2,3,4,5]

**Output:** [1,2,3,4,5]

**Explanation:** In this case, for all items, you will not receive any discount at all.

**Example 3:**

**Input:** prices = [10,1,1,6]

**Output:** [9,0,1,6]

**Constraints:**

* `1 <= prices.length <= 500`
* `1 <= prices[i] <= 10^3`
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package g1401_1500.s1476_subrectangle_queries;

// #Medium #Array #Matrix #Design #2022_04_04_Time_20_ms_(97.61%)_Space_44.6_MB_(95.87%)

public class SubrectangleQueries {
private final int[][] rectangle;

public SubrectangleQueries(int[][] rectangle) {
this.rectangle = rectangle;
}

public void updateSubrectangle(int row1, int col1, int row2, int col2, int newValue) {
for (int i = row1; i <= row2; i++) {
for (int j = col1; j <= col2; j++) {
rectangle[i][j] = newValue;
}
}
}

public int getValue(int row, int col) {
return rectangle[row][col];
}
}
41 changes: 41 additions & 0 deletions src/main/java/g1401_1500/s1476_subrectangle_queries/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
1476\. Subrectangle Queries

Medium

Implement the class `SubrectangleQueries` which receives a `rows x cols` rectangle as a matrix of integers in the constructor and supports two methods:

1. `updateSubrectangle(int row1, int col1, int row2, int col2, int newValue)`

* Updates all values with `newValue` in the subrectangle whose upper left coordinate is `(row1,col1)` and bottom right coordinate is `(row2,col2)`.

2. `getValue(int row, int col)`

* Returns the current value of the coordinate `(row,col)` from the rectangle.

**Example 1:**

**Input** ["SubrectangleQueries","getValue","updateSubrectangle","getValue","getValue","updateSubrectangle","getValue","getValue"] [[[[1,2,1],[4,3,4],[3,2,1],[1,1,1]]],[0,2],[0,0,3,2,5],[0,2],[3,1],[3,0,3,2,10],[3,1],[0,2]]

**Output:** [null,1,null,5,5,null,10,5]

**Explanation:** SubrectangleQueries subrectangleQueries = new SubrectangleQueries([[1,2,1],[4,3,4],[3,2,1],[1,1,1]]); // The initial rectangle (4x3) looks like: // 1 2 1 // 4 3 4 // 3 2 1 // 1 1 1 subrectangleQueries.getValue(0, 2); // return 1 subrectangleQueries.updateSubrectangle(0, 0, 3, 2, 5); // After this update the rectangle looks like: // 5 5 5 // 5 5 5 // 5 5 5 // 5 5 5 subrectangleQueries.getValue(0, 2); // return 5 subrectangleQueries.getValue(3, 1); // return 5 subrectangleQueries.updateSubrectangle(3, 0, 3, 2, 10); // After this update the rectangle looks like: // 5 5 5 // 5 5 5 // 5 5 5 // 10 10 10 subrectangleQueries.getValue(3, 1); // return 10 subrectangleQueries.getValue(0, 2); // return 5

**Example 2:**

**Input** ["SubrectangleQueries","getValue","updateSubrectangle","getValue","getValue","updateSubrectangle","getValue"] [[[[1,1,1],[2,2,2],[3,3,3]]],[0,0],[0,0,2,2,100],[0,0],[2,2],[1,1,2,2,20],[2,2]]

**Output:** [null,1,null,100,100,null,20]

**Explanation:** SubrectangleQueries subrectangleQueries = new SubrectangleQueries([[1,1,1],[2,2,2],[3,3,3]]); subrectangleQueries.getValue(0, 0); // return 1 subrectangleQueries.updateSubrectangle(0, 0, 2, 2, 100); subrectangleQueries.getValue(0, 0); // return 100 subrectangleQueries.getValue(2, 2); // return 100 subrectangleQueries.updateSubrectangle(1, 1, 2, 2, 20); subrectangleQueries.getValue(2, 2); // return 20

**Constraints:**

* There will be at most `500` operations considering both methods: `updateSubrectangle` and `getValue`.
* `1 <= rows, cols <= 100`
* `rows == rectangle.length`
* `cols == rectangle[i].length`
* `0 <= row1 <= row2 < rows`
* `0 <= col1 <= col2 < cols`
* `1 <= newValue, rectangle[i][j] <= 10^9`
* `0 <= row < rows`
* `0 <= col < cols`
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package g1401_1500.s1477_find_two_non_overlapping_sub_arrays_each_with_target_sum;

// #Medium #Array #Hash_Table #Dynamic_Programming #Binary_Search #Sliding_Window
// #2022_04_04_Time_8_ms_(89.43%)_Space_50.1_MB_(97.73%)

import java.util.Arrays;

public class Solution {
public int minSumOfLengths(int[] arr, int target) {
int l = 0;
int r = 0;
int sum = 0;
int[] idx = new int[arr.length];
Arrays.fill(idx, arr.length + 1);
int ans = 2 * arr.length + 1;

while (r < arr.length || sum >= target) {
if (sum < target) {
sum += arr[r];
r++;
} else if (sum > target) {
sum -= arr[l];
l++;
} else {
int length = r - l;
idx[r - 1] = length;
if (l > 0 && idx[l - 1] < arr.length + 1) {
ans = Math.min(ans, length + idx[l - 1]);
}
sum -= arr[l];
l++;
}
if (r > 1) {
idx[r - 1] = Math.min(idx[r - 1], idx[r - 2]);
}
}
return ans <= 2 * arr.length ? ans : -1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
1477\. Find Two Non-overlapping Sub-arrays Each With Target Sum

Medium

You are given an array of integers `arr` and an integer `target`.

You have to find **two non-overlapping sub-arrays** of `arr` each with a sum equal `target`. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is **minimum**.

Return _the minimum sum of the lengths_ of the two required sub-arrays, or return `-1` if you cannot find such two sub-arrays.

**Example 1:**

**Input:** arr = [3,2,2,4,3], target = 3

**Output:** 2

**Explanation:** Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.

**Example 2:**

**Input:** arr = [7,3,4,7], target = 7

**Output:** 2

**Explanation:** Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.

**Example 3:**

**Input:** arr = [4,3,2,6,2,3,4], target = 6

**Output:** -1

**Explanation:** We have only one sub-array of sum = 6.

**Constraints:**

* <code>1 <= arr.length <= 10<sup>5</sup></code>
* `1 <= arr[i] <= 1000`
* <code>1 <= target <= 10<sup>8</sup></code>
45 changes: 45 additions & 0 deletions src/main/java/g1401_1500/s1478_allocate_mailboxes/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package g1401_1500.s1478_allocate_mailboxes;

// #Hard #Array #Dynamic_Programming #Math #Sorting
// #2022_04_04_Time_12_ms_(78.87%)_Space_41.6_MB_(89.79%)

import java.util.Arrays;

public class Solution {
public int minDistance(int[] houses, int k) {
Arrays.sort(houses);
int n = houses.length;
int[][] dp = new int[n][k + 1];
for (int[] ar : dp) {
Arrays.fill(ar, -1);
}
return recur(houses, 0, k, dp);
}

private int recur(int[] houses, int idx, int k, int[][] dp) {
if (dp[idx][k] != -1) {
return dp[idx][k];
}
if (k == 1) {
int dist = calDist(houses, idx, houses.length - 1);
dp[idx][k] = dist;
return dp[idx][k];
}
int min = Integer.MAX_VALUE;
for (int i = idx; i + k - 1 < houses.length; i++) {
int dist = calDist(houses, idx, i);
dist += recur(houses, i + 1, k - 1, dp);
min = Math.min(min, dist);
}
dp[idx][k] = min;
return min;
}

private int calDist(int[] ar, int start, int end) {
int result = 0;
while (start < end) {
result += ar[end--] - ar[start++];
}
return result;
}
}
35 changes: 35 additions & 0 deletions src/main/java/g1401_1500/s1478_allocate_mailboxes/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
1478\. Allocate Mailboxes

Hard

Given the array `houses` where `houses[i]` is the location of the <code>i<sup>th</sup></code> house along a street and an integer `k`, allocate `k` mailboxes in the street.

Return _the **minimum** total distance between each house and its nearest mailbox_.

The test cases are generated so that the answer fits in a 32-bit integer.

**Example 1:**

![](https://assets.leetcode.com/uploads/2020/05/07/sample_11_1816.png)

**Input:** houses = [1,4,8,10,20], k = 3

**Output:** 5

**Explanation:** Allocate mailboxes in position 3, 9 and 20. Minimum total distance from each houses to nearest mailboxes is |3-1| + |4-3| + |9-8| + |10-9| + |20-20| = 5

**Example 2:**

![](https://assets.leetcode.com/uploads/2020/05/07/sample_2_1816.png)

**Input:** houses = [2,3,5,12,18], k = 2

**Output:** 9

**Explanation:** Allocate mailboxes in position 3 and 14. Minimum total distance from each houses to nearest mailboxes is |2-3| + |3-3| + |5-3| + |12-14| + |18-14| = 9.

**Constraints:**

* `1 <= k <= houses.length <= 100`
* <code>1 <= houses[i] <= 10<sup>4</sup></code>
* All the integers of `houses` are **unique**.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package g1401_1500.s1480_running_sum_of_1d_array;

// #Easy #Array #Prefix_Sum #2022_04_04_Time_0_ms_(100.00%)_Space_42.9_MB_(76.13%)

public class Solution {
public int[] runningSum(int[] nums) {
int sum = 0;
int[] result = new int[nums.length];
for (int i = 0; i < nums.length; i++) {
sum += nums[i];
result[i] = sum;
}
return result;
}
}
34 changes: 34 additions & 0 deletions src/main/java/g1401_1500/s1480_running_sum_of_1d_array/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
1480\. Running Sum of 1d Array

Easy

Given an array `nums`. We define a running sum of an array as `runningSum[i] = sum(nums[0]…nums[i])`.

Return the running sum of `nums`.

**Example 1:**

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

**Output:** [1,3,6,10]

**Explanation:** Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].

**Example 2:**

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

**Output:** [1,2,3,4,5]

**Explanation:** Running sum is obtained as follows: [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1].

**Example 3:**

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

**Output:** [3,4,6,16,17]

**Constraints:**

* `1 <= nums.length <= 1000`
* `-10^6 <= nums[i] <= 10^6`
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package g1401_1500.s1475_final_prices_with_a_special_discount_in_a_shop;

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

import org.junit.jupiter.api.Test;

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

@Test
void finalPrices2() {
assertThat(
new Solution().finalPrices(new int[] {1, 2, 3, 4, 5}),
equalTo(new int[] {1, 2, 3, 4, 5}));
}

@Test
void finalPrices3() {
assertThat(
new Solution().finalPrices(new int[] {10, 1, 1, 6}),
equalTo(new int[] {9, 0, 1, 6}));
}
}
Loading