From c900e5a0def1cc7718474141ef621e96cfd0e6d8 Mon Sep 17 00:00:00 2001 From: ThanhNIT Date: Tue, 14 Dec 2021 08:02:11 +0700 Subject: [PATCH 1/7] Added tasks 357, 363 --- .../Solution.java | 17 +++ .../readme.md | 23 ++++ .../Solution.java | 124 ++++++++++++++++++ .../readme.md | 33 +++++ .../SolutionTest.java | 19 +++ .../SolutionTest.java | 20 +++ 6 files changed, 236 insertions(+) create mode 100644 src/main/java/g0301_0400/s0357_count_numbers_with_unique_digits/Solution.java create mode 100644 src/main/java/g0301_0400/s0357_count_numbers_with_unique_digits/readme.md create mode 100644 src/main/java/g0301_0400/s0363_max_sum_of_rectangle_no_larger_than_k/Solution.java create mode 100644 src/main/java/g0301_0400/s0363_max_sum_of_rectangle_no_larger_than_k/readme.md create mode 100644 src/test/java/g0301_0400/s0357_count_numbers_with_unique_digits/SolutionTest.java create mode 100644 src/test/java/g0301_0400/s0363_max_sum_of_rectangle_no_larger_than_k/SolutionTest.java diff --git a/src/main/java/g0301_0400/s0357_count_numbers_with_unique_digits/Solution.java b/src/main/java/g0301_0400/s0357_count_numbers_with_unique_digits/Solution.java new file mode 100644 index 000000000..1c1a816ba --- /dev/null +++ b/src/main/java/g0301_0400/s0357_count_numbers_with_unique_digits/Solution.java @@ -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; + } +} diff --git a/src/main/java/g0301_0400/s0357_count_numbers_with_unique_digits/readme.md b/src/main/java/g0301_0400/s0357_count_numbers_with_unique_digits/readme.md new file mode 100644 index 000000000..771542b65 --- /dev/null +++ b/src/main/java/g0301_0400/s0357_count_numbers_with_unique_digits/readme.md @@ -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 0 <= x < 10n. + +**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` \ No newline at end of file diff --git a/src/main/java/g0301_0400/s0363_max_sum_of_rectangle_no_larger_than_k/Solution.java b/src/main/java/g0301_0400/s0363_max_sum_of_rectangle_no_larger_than_k/Solution.java new file mode 100644 index 000000000..afdcf7888 --- /dev/null +++ b/src/main/java/g0301_0400/s0363_max_sum_of_rectangle_no_larger_than_k/Solution.java @@ -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; + + 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; + } +} diff --git a/src/main/java/g0301_0400/s0363_max_sum_of_rectangle_no_larger_than_k/readme.md b/src/main/java/g0301_0400/s0363_max_sum_of_rectangle_no_larger_than_k/readme.md new file mode 100644 index 000000000..4a08bcc6e --- /dev/null +++ b/src/main/java/g0301_0400/s0363_max_sum_of_rectangle_no_larger_than_k/readme.md @@ -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` +* -105 <= k <= 105 + +**Follow up:** What if the number of rows is much larger than the number of columns? \ No newline at end of file diff --git a/src/test/java/g0301_0400/s0357_count_numbers_with_unique_digits/SolutionTest.java b/src/test/java/g0301_0400/s0357_count_numbers_with_unique_digits/SolutionTest.java new file mode 100644 index 000000000..502acacdc --- /dev/null +++ b/src/test/java/g0301_0400/s0357_count_numbers_with_unique_digits/SolutionTest.java @@ -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 { + + @Test + void testCountNumbersWithUniqueDigits() { + assertThat(new Solution().countNumbersWithUniqueDigits(2), equalTo(91)); + } + + @Test + void testCountNumbersWithUniqueDigits2() { + assertThat(new Solution().countNumbersWithUniqueDigits(0), equalTo(1)); + } +} diff --git a/src/test/java/g0301_0400/s0363_max_sum_of_rectangle_no_larger_than_k/SolutionTest.java b/src/test/java/g0301_0400/s0363_max_sum_of_rectangle_no_larger_than_k/SolutionTest.java new file mode 100644 index 000000000..596bb1d93 --- /dev/null +++ b/src/test/java/g0301_0400/s0363_max_sum_of_rectangle_no_larger_than_k/SolutionTest.java @@ -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() { + 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)); + } +} From 0b1802a365ca2ec779d61be50794be2590debc7d Mon Sep 17 00:00:00 2001 From: ThanhNIT Date: Tue, 14 Dec 2021 08:15:48 +0700 Subject: [PATCH 2/7] Update Solution.java --- .../Solution.java | 39 +++++++++++-------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/src/main/java/g0301_0400/s0363_max_sum_of_rectangle_no_larger_than_k/Solution.java b/src/main/java/g0301_0400/s0363_max_sum_of_rectangle_no_larger_than_k/Solution.java index afdcf7888..21fac6b79 100644 --- a/src/main/java/g0301_0400/s0363_max_sum_of_rectangle_no_larger_than_k/Solution.java +++ b/src/main/java/g0301_0400/s0363_max_sum_of_rectangle_no_larger_than_k/Solution.java @@ -11,10 +11,12 @@ * 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. +* 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. +* 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. * */ @@ -22,18 +24,18 @@ import java.util.Arrays; public class Solution { - private int[] M; + private int[] m; - private int merge(int[] A, int l, int m, int r, int k) { + 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) { + while (i <= m && a[j] - a[i] > k) { i++; } if (i > m) { break; } - res = Math.max(res, A[j] - A[i]); + res = Math.max(res, a[j] - a[i]); if (res == k) { return res; } @@ -42,16 +44,16 @@ private int merge(int[] A, int l, int m, int r, int k) { int j = m + 1; int t = 0; while (i <= m && j <= r) { - M[t++] = A[i] <= A[j] ? A[i++] : A[j++]; + this.m[t++] = a[i] <= a[j] ? a[i++] : a[j++]; } while (i <= m) { - M[t++] = A[i++]; + this.m[t++] = a[i++]; } while (j <= r) { - M[t++] = A[j++]; + this.m[t++] = a[j++]; } for (i = l; i <= r; i++) { - A[i] = M[i - l]; + a[i] = this.m[i - l]; } return res; } @@ -72,8 +74,9 @@ private int mergeSort(int[] A, int l, int r, int k) { return Math.max(res, merge(A, l, m, r, k)); } - private int maxSumSubarray(int[] A) { - int min = 0, res = Integer.MIN_VALUE; + private int maxSumSubArray(int[] A) { + int min = 0; + int res = Integer.MIN_VALUE; for (int sum : A) { res = Math.max(res, sum - min); min = Math.min(min, sum); @@ -81,9 +84,11 @@ private int maxSumSubarray(int[] A) { return res; } - private int maxSumSubarray(int[] A, int k) { - int res = maxSumSubarray(A); - if (res <= k) 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); } @@ -99,7 +104,7 @@ public int maxSumSubMatrix(int[][] matrix, int k) { groupingRows = false; } int[] sum = new int[n]; - this.M = 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++) { @@ -113,7 +118,7 @@ public int maxSumSubMatrix(int[][] matrix, int k) { sum[t] += pre += matrix[t][j]; } } - res = Math.max(res, maxSumSubarray(sum, k)); + res = Math.max(res, maxSumSubArray(sum, k)); if (res == k) { return res; } From 708f057711dd46c9cece4f0d4851569e8372f1e4 Mon Sep 17 00:00:00 2001 From: ThanhNIT Date: Tue, 14 Dec 2021 08:16:56 +0700 Subject: [PATCH 3/7] remove empty lines --- .../s0357_count_numbers_with_unique_digits/SolutionTest.java | 1 - .../SolutionTest.java | 1 - 2 files changed, 2 deletions(-) diff --git a/src/test/java/g0301_0400/s0357_count_numbers_with_unique_digits/SolutionTest.java b/src/test/java/g0301_0400/s0357_count_numbers_with_unique_digits/SolutionTest.java index 502acacdc..da5f01e10 100644 --- a/src/test/java/g0301_0400/s0357_count_numbers_with_unique_digits/SolutionTest.java +++ b/src/test/java/g0301_0400/s0357_count_numbers_with_unique_digits/SolutionTest.java @@ -2,7 +2,6 @@ import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.MatcherAssert.assertThat; - import org.junit.jupiter.api.Test; class SolutionTest { diff --git a/src/test/java/g0301_0400/s0363_max_sum_of_rectangle_no_larger_than_k/SolutionTest.java b/src/test/java/g0301_0400/s0363_max_sum_of_rectangle_no_larger_than_k/SolutionTest.java index 596bb1d93..c2ff33845 100644 --- a/src/test/java/g0301_0400/s0363_max_sum_of_rectangle_no_larger_than_k/SolutionTest.java +++ b/src/test/java/g0301_0400/s0363_max_sum_of_rectangle_no_larger_than_k/SolutionTest.java @@ -2,7 +2,6 @@ import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.MatcherAssert.assertThat; - import org.junit.jupiter.api.Test; class SolutionTest { From d39350f384cebb2c12a520897cbd4d6410b65ad0 Mon Sep 17 00:00:00 2001 From: ThanhNIT Date: Tue, 14 Dec 2021 08:21:54 +0700 Subject: [PATCH 4/7] fix comments --- .../Solution.java | 20 +++++++++---------- .../SolutionTest.java | 4 ++-- .../SolutionTest.java | 4 ++-- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/main/java/g0301_0400/s0363_max_sum_of_rectangle_no_larger_than_k/Solution.java b/src/main/java/g0301_0400/s0363_max_sum_of_rectangle_no_larger_than_k/Solution.java index 21fac6b79..66dcb9377 100644 --- a/src/main/java/g0301_0400/s0363_max_sum_of_rectangle_no_larger_than_k/Solution.java +++ b/src/main/java/g0301_0400/s0363_max_sum_of_rectangle_no_larger_than_k/Solution.java @@ -58,38 +58,38 @@ private int merge(int[] a, int l, int m, int r, int k) { return res; } - private int mergeSort(int[] A, int l, int r, int k) { + private int mergeSort(int[] a, int l, int r, int k) { if (l == r) { - return A[l] <= k ? A[l] : Integer.MIN_VALUE; + return a[l] <= k ? a[l] : Integer.MIN_VALUE; } int m = l + ((r - l) >> 1); - int res = mergeSort(A, l, m, k); + int res = mergeSort(a, l, m, k); if (res == k) { return res; } - res = Math.max(res, mergeSort(A, m + 1, r, k)); + 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)); + return Math.max(res, merge(a, l, m, r, k)); } - private int maxSumSubArray(int[] A) { + private int maxSumSubArray(int[] a) { int min = 0; int res = Integer.MIN_VALUE; - for (int sum : A) { + 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); + 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); + return mergeSort(a.clone(), 0, a.length - 1, k); } public int maxSumSubMatrix(int[][] matrix, int k) { diff --git a/src/test/java/g0301_0400/s0357_count_numbers_with_unique_digits/SolutionTest.java b/src/test/java/g0301_0400/s0357_count_numbers_with_unique_digits/SolutionTest.java index da5f01e10..3c66bc53c 100644 --- a/src/test/java/g0301_0400/s0357_count_numbers_with_unique_digits/SolutionTest.java +++ b/src/test/java/g0301_0400/s0357_count_numbers_with_unique_digits/SolutionTest.java @@ -7,12 +7,12 @@ class SolutionTest { @Test - void testCountNumbersWithUniqueDigits() { + void countNumbersWithUniqueDigits() { assertThat(new Solution().countNumbersWithUniqueDigits(2), equalTo(91)); } @Test - void testCountNumbersWithUniqueDigits2() { + void countNumbersWithUniqueDigits2() { assertThat(new Solution().countNumbersWithUniqueDigits(0), equalTo(1)); } } diff --git a/src/test/java/g0301_0400/s0363_max_sum_of_rectangle_no_larger_than_k/SolutionTest.java b/src/test/java/g0301_0400/s0363_max_sum_of_rectangle_no_larger_than_k/SolutionTest.java index c2ff33845..4a450eab5 100644 --- a/src/test/java/g0301_0400/s0363_max_sum_of_rectangle_no_larger_than_k/SolutionTest.java +++ b/src/test/java/g0301_0400/s0363_max_sum_of_rectangle_no_larger_than_k/SolutionTest.java @@ -7,13 +7,13 @@ class SolutionTest { @Test - void testMaxSumSubMatrix() { + void maxSumSubMatrix() { assertThat( new Solution().maxSumSubMatrix(new int[][] {{1, 0, 1}, {0, -2, 3}}, 2), equalTo(2)); } @Test - void testMaxSumSubMatrix2() { + void maxSumSubMatrix2() { assertThat(new Solution().maxSumSubMatrix(new int[][] {{2, 2, -1}}, 3), equalTo(3)); } } From c4106117c0b2ae6855db7206c145747fb245afce Mon Sep 17 00:00:00 2001 From: ThanhNIT Date: Tue, 14 Dec 2021 08:37:54 +0700 Subject: [PATCH 5/7] update --- .../Solution.java | 27 ++++++++++--------- .../SolutionTest.java | 1 + .../SolutionTest.java | 1 + 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/main/java/g0301_0400/s0363_max_sum_of_rectangle_no_larger_than_k/Solution.java b/src/main/java/g0301_0400/s0363_max_sum_of_rectangle_no_larger_than_k/Solution.java index 66dcb9377..556d7f674 100644 --- a/src/main/java/g0301_0400/s0363_max_sum_of_rectangle_no_larger_than_k/Solution.java +++ b/src/main/java/g0301_0400/s0363_max_sum_of_rectangle_no_larger_than_k/Solution.java @@ -28,7 +28,8 @@ public class Solution { 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++) { + for (int j = m + 1; j <= r; j++) { + int i = l; while (i <= m && a[j] - a[i] > k) { i++; } @@ -93,28 +94,28 @@ private int maxSumSubArray(int[] a, int k) { } public int maxSumSubMatrix(int[][] matrix, int k) { - int m = matrix.length; - int n = m == 0 ? 0 : matrix[0].length; + int x = matrix.length; + int y = x == 0 ? 0 : matrix[0].length; int res = Integer.MIN_VALUE; boolean groupingRows = true; - if (m > n) { - int temp = m; - m = n; - n = temp; + if (x > y) { + int temp = x; + x = y; + y = temp; groupingRows = false; } - int[] sum = new int[n]; - this.m = new int[n]; - for (int i = 0; i < m; i++) { + int[] sum = new int[y]; + this.m = new int[y]; + for (int i = 0; i < x; i++) { Arrays.fill(sum, 0); - for (int j = i; j < m; j++) { + for (int j = i; j < x; j++) { int pre = 0; if (groupingRows) { - for (int t = 0; t < n; t++) { + for (int t = 0; t < y; t++) { sum[t] += pre += matrix[j][t]; } } else { - for (int t = 0; t < n; t++) { + for (int t = 0; t < y; t++) { sum[t] += pre += matrix[t][j]; } } diff --git a/src/test/java/g0301_0400/s0357_count_numbers_with_unique_digits/SolutionTest.java b/src/test/java/g0301_0400/s0357_count_numbers_with_unique_digits/SolutionTest.java index 3c66bc53c..520e8038c 100644 --- a/src/test/java/g0301_0400/s0357_count_numbers_with_unique_digits/SolutionTest.java +++ b/src/test/java/g0301_0400/s0357_count_numbers_with_unique_digits/SolutionTest.java @@ -2,6 +2,7 @@ import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.MatcherAssert.assertThat; + import org.junit.jupiter.api.Test; class SolutionTest { diff --git a/src/test/java/g0301_0400/s0363_max_sum_of_rectangle_no_larger_than_k/SolutionTest.java b/src/test/java/g0301_0400/s0363_max_sum_of_rectangle_no_larger_than_k/SolutionTest.java index 4a450eab5..2e4842ffd 100644 --- a/src/test/java/g0301_0400/s0363_max_sum_of_rectangle_no_larger_than_k/SolutionTest.java +++ b/src/test/java/g0301_0400/s0363_max_sum_of_rectangle_no_larger_than_k/SolutionTest.java @@ -2,6 +2,7 @@ import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.MatcherAssert.assertThat; + import org.junit.jupiter.api.Test; class SolutionTest { From 22ee74cd9b6d4cbf19cf5269cbbe2513c0979513 Mon Sep 17 00:00:00 2001 From: ThanhNIT Date: Tue, 14 Dec 2021 08:41:03 +0700 Subject: [PATCH 6/7] Update Solution.java --- .../Solution.java | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/main/java/g0301_0400/s0363_max_sum_of_rectangle_no_larger_than_k/Solution.java b/src/main/java/g0301_0400/s0363_max_sum_of_rectangle_no_larger_than_k/Solution.java index 556d7f674..ea0684f78 100644 --- a/src/main/java/g0301_0400/s0363_max_sum_of_rectangle_no_larger_than_k/Solution.java +++ b/src/main/java/g0301_0400/s0363_max_sum_of_rectangle_no_larger_than_k/Solution.java @@ -94,28 +94,28 @@ private int maxSumSubArray(int[] a, int k) { } public int maxSumSubMatrix(int[][] matrix, int k) { - int x = matrix.length; - int y = x == 0 ? 0 : matrix[0].length; + int localM = matrix.length; + int localN = localM == 0 ? 0 : matrix[0].length; int res = Integer.MIN_VALUE; boolean groupingRows = true; - if (x > y) { - int temp = x; - x = y; - y = temp; + if (localM > localN) { + int temp = localM; + localM = localN; + localN = temp; groupingRows = false; } - int[] sum = new int[y]; - this.m = new int[y]; - for (int i = 0; i < x; i++) { + int[] sum = new int[localN]; + this.m = new int[localN]; + for (int i = 0; i < localM; i++) { Arrays.fill(sum, 0); - for (int j = i; j < x; j++) { + for (int j = i; j < localM; j++) { int pre = 0; if (groupingRows) { - for (int t = 0; t < y; t++) { + for (int t = 0; t < localN; t++) { sum[t] += pre += matrix[j][t]; } } else { - for (int t = 0; t < y; t++) { + for (int t = 0; t < localN; t++) { sum[t] += pre += matrix[t][j]; } } From 2881086b9381389d6ef36c8c67d8f03afeb59776 Mon Sep 17 00:00:00 2001 From: ThanhNIT Date: Tue, 14 Dec 2021 08:43:14 +0700 Subject: [PATCH 7/7] fix comments --- .../Solution.java | 8 ++++---- .../SolutionTest.java | 1 - .../SolutionTest.java | 1 - 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/main/java/g0301_0400/s0363_max_sum_of_rectangle_no_larger_than_k/Solution.java b/src/main/java/g0301_0400/s0363_max_sum_of_rectangle_no_larger_than_k/Solution.java index ea0684f78..6d7db52ed 100644 --- a/src/main/java/g0301_0400/s0363_max_sum_of_rectangle_no_larger_than_k/Solution.java +++ b/src/main/java/g0301_0400/s0363_max_sum_of_rectangle_no_larger_than_k/Solution.java @@ -63,16 +63,16 @@ 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); + int localM = l + ((r - l) >> 1); + int res = mergeSort(a, l, localM, k); if (res == k) { return res; } - res = Math.max(res, mergeSort(a, m + 1, r, k)); + res = Math.max(res, mergeSort(a, localM + 1, r, k)); if (res == k) { return res; } - return Math.max(res, merge(a, l, m, r, k)); + return Math.max(res, merge(a, l, localM, r, k)); } private int maxSumSubArray(int[] a) { diff --git a/src/test/java/g0301_0400/s0357_count_numbers_with_unique_digits/SolutionTest.java b/src/test/java/g0301_0400/s0357_count_numbers_with_unique_digits/SolutionTest.java index 520e8038c..f52a2feda 100644 --- a/src/test/java/g0301_0400/s0357_count_numbers_with_unique_digits/SolutionTest.java +++ b/src/test/java/g0301_0400/s0357_count_numbers_with_unique_digits/SolutionTest.java @@ -6,7 +6,6 @@ import org.junit.jupiter.api.Test; class SolutionTest { - @Test void countNumbersWithUniqueDigits() { assertThat(new Solution().countNumbersWithUniqueDigits(2), equalTo(91)); diff --git a/src/test/java/g0301_0400/s0363_max_sum_of_rectangle_no_larger_than_k/SolutionTest.java b/src/test/java/g0301_0400/s0363_max_sum_of_rectangle_no_larger_than_k/SolutionTest.java index 2e4842ffd..e250cbbb5 100644 --- a/src/test/java/g0301_0400/s0363_max_sum_of_rectangle_no_larger_than_k/SolutionTest.java +++ b/src/test/java/g0301_0400/s0363_max_sum_of_rectangle_no_larger_than_k/SolutionTest.java @@ -6,7 +6,6 @@ import org.junit.jupiter.api.Test; class SolutionTest { - @Test void maxSumSubMatrix() { assertThat(