Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Added tasks 357, 363 #112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
javadev merged 7 commits into javadev:main from ThanhNIT:tasks-357-363
Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package g0301_0400.s0357_count_numbers_with_unique_digits;

// #Medium #Dynamic_Programming #Math #Backtracking

public class Solution {
public int countNumbersWithUniqueDigits(int n) {
int ans = 1;
for (int i = 1; i <= n; i++) {
int mul = 1;
for (int j = 1; j < i; j++) {
mul *= (10 - j);
}
ans = ans + 9 * mul;
}
return ans;
}
}
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
357\. Count Numbers with Unique Digits

Medium

Given an integer `n`, return the count of all numbers with unique digits, `x`, where <code>0 <= x < 10<sup>n</sup></code>.

**Example 1:**

**Input:** n = 2

**Output:** 91

**Explanation:** The answer should be the total numbers in the range of 0 ≤ x < 100, excluding 11,22,33,44,55,66,77,88,99

**Example 2:**

**Input:** n = 0

**Output:** 1

**Constraints:**

* `0 <= n <= 8`
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package g0301_0400.s0363_max_sum_of_rectangle_no_larger_than_k;

// #Hard #Array #Dynamic_Programming #Binary_Search #Matrix #Ordered_Set

/*
*
* Basic idea is the same as previous approach but we solve the problem in Step 2 differently.
* Here we leverage divide and conquer technique. Basically we perform merge sort on prefix sum values and
* calculate result during merge step.
* One might remember the idea of using merge sort to count inversions in an array. This is very similar.

* So how exactly do we compute result during merge step?
* Suppose we are merging left prefix subarray and right prefix subarray.
* Remember from previous approach, for each index we're trying to find an old prefix sum which is just greater than or equal to current prefix sum - k.
* So we can iterate over right subarray and for each index j, keep incrementing the pointer
* in left array i (initialized to start index) till that situation is false (or basically prefix[i] < prefix[j] - k).
* This way, we can compute the result for all cross subarrays (i.e. i in left subarray and j in right subarray) in linear time.
* After this, we do the standard merging part of merge sort.
*
*/

import java.util.Arrays;

public class Solution {
private int[] M;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be not capital letter


private int merge(int[] A, int l, int m, int r, int k) {
int res = Integer.MIN_VALUE;
for (int j = m + 1, i = l; j <= r; j++) {
while (i <= m && A[j] - A[i] > k) {
i++;
}
if (i > m) {
break;
}
res = Math.max(res, A[j] - A[i]);
if (res == k) {
return res;
}
}
int i = l;
int j = m + 1;
int t = 0;
while (i <= m && j <= r) {
M[t++] = A[i] <= A[j] ? A[i++] : A[j++];
}
while (i <= m) {
M[t++] = A[i++];
}
while (j <= r) {
M[t++] = A[j++];
}
for (i = l; i <= r; i++) {
A[i] = M[i - l];
}
return res;
}

private int mergeSort(int[] A, int l, int r, int k) {
if (l == r) {
return A[l] <= k ? A[l] : Integer.MIN_VALUE;
}
int m = l + ((r - l) >> 1);
int res = mergeSort(A, l, m, k);
if (res == k) {
return res;
}
res = Math.max(res, mergeSort(A, m + 1, r, k));
if (res == k) {
return res;
}
return Math.max(res, merge(A, l, m, r, k));
}

private int maxSumSubarray(int[] A) {
int min = 0, res = Integer.MIN_VALUE;
for (int sum : A) {
res = Math.max(res, sum - min);
min = Math.min(min, sum);
}
return res;
}

private int maxSumSubarray(int[] A, int k) {
int res = maxSumSubarray(A);
if (res <= k) return res;
return mergeSort(A.clone(), 0, A.length - 1, k);
}

public int maxSumSubMatrix(int[][] matrix, int k) {
int m = matrix.length;
int n = m == 0 ? 0 : matrix[0].length;
int res = Integer.MIN_VALUE;
boolean groupingRows = true;
if (m > n) {
int temp = m;
m = n;
n = temp;
groupingRows = false;
}
int[] sum = new int[n];
this.M = new int[n];
for (int i = 0; i < m; i++) {
Arrays.fill(sum, 0);
for (int j = i; j < m; j++) {
int pre = 0;
if (groupingRows) {
for (int t = 0; t < n; t++) {
sum[t] += pre += matrix[j][t];
}
} else {
for (int t = 0; t < n; t++) {
sum[t] += pre += matrix[t][j];
}
}
res = Math.max(res, maxSumSubarray(sum, k));
if (res == k) {
return res;
}
}
}
return res;
}
}
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
363. Max Sum of Rectangle No Larger Than K

Hard

Given an `m x n` matrix `matrix` and an integer `k`, return _the max sum of a rectangle in the matrix such that its sum is no larger than_ `k`.

It is **guaranteed** that there will be a rectangle with a sum no larger than `k`.

**Example 1:**

![](https://assets.leetcode.com/uploads/2021/03/18/sum-grid.jpg)

**Input:** matrix = [[1,0,1],[0,-2,3]], k = 2

**Output:** 2

**Explanation:** Because the sum of the blue rectangle [[0, 1], [-2, 3]] is 2, and 2 is the max number no larger than k (k = 2).

**Example 2:**

**Input:** matrix = [[2,2,-1]], k = 3

**Output:** 3

**Constraints:**

* `m == matrix.length`
* `n == matrix[i].length`
* `1 <= m, n <= 100`
* `-100 <= matrix[i][j] <= 100`
* <code>-10<sup>5</sup> <= k <= 10<sup>5</sup></code>

**Follow up:** What if the number of rows is much larger than the number of columns?
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package g0301_0400.s0357_count_numbers_with_unique_digits;

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

import org.junit.jupiter.api.Test;

class SolutionTest {

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove empty line

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the empty line appear when I run 'gradlew.bat clean spotlessJavaApply test ' command

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image
I think we need to add the empty line to pass the check

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove empty line only before @test then format sources

Copy link
Owner

@javadev javadev Dec 14, 2021
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the empty line appear when I run 'gradlew.bat clean spotlessJavaApply test ' command

I see other tests without empty line.

@Test
void testCountNumbersWithUniqueDigits() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rename method without test

assertThat(new Solution().countNumbersWithUniqueDigits(2), equalTo(91));
}

@Test
void testCountNumbersWithUniqueDigits2() {
assertThat(new Solution().countNumbersWithUniqueDigits(0), equalTo(1));
}
}
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package g0301_0400.s0363_max_sum_of_rectangle_no_larger_than_k;

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

import org.junit.jupiter.api.Test;

class SolutionTest {

@Test
void testMaxSumSubMatrix() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rename method without test

assertThat(
new Solution().maxSumSubMatrix(new int[][] {{1, 0, 1}, {0, -2, 3}}, 2), equalTo(2));
}

@Test
void testMaxSumSubMatrix2() {
assertThat(new Solution().maxSumSubMatrix(new int[][] {{2, 2, -1}}, 3), equalTo(3));
}
}

AltStyle によって変換されたページ (->オリジナル) /