1 <= n <= 231 - 1
+* `1 <= pick <= n` \ No newline at end of file diff --git a/src/main/java/g0301_0400/s0378_kth_smallest_element_in_a_sorted_matrix/Solution.java b/src/main/java/g0301_0400/s0378_kth_smallest_element_in_a_sorted_matrix/Solution.java new file mode 100644 index 000000000..431861b35 --- /dev/null +++ b/src/main/java/g0301_0400/s0378_kth_smallest_element_in_a_sorted_matrix/Solution.java @@ -0,0 +1,52 @@ +package g0301_0400.s0378_kth_smallest_element_in_a_sorted_matrix; + +// #Medium #Top_Interview_Questions #Array #Sorting #Binary_Search #Matrix #Heap_Priority_Queue + +public class Solution { + public int kthSmallest(int[][] matrix, int k) { + if (matrix == null || matrix.length == 0) { + return -1; + } + int start = matrix[0][0]; + int end = matrix[matrix.length - 1][matrix[0].length - 1]; + // O(log(max-min)) time + while (start + 1 < end) { + int mid = start + (end - start) / 2; + if (countLessEqual(matrix, mid) < k) { + // look towards end + start = mid; + } else { + // look towards start + end = mid; + } + } + + // leave only with start and end, one of them must be the answer + // try to see if start fits the criteria first + if (countLessEqual(matrix, start)>= k) {
+ return start;
+ } else {
+ return end;
+ }
+ }
+
+ // countLessEqual
+ // O(n) Time
+ private int countLessEqual(int[][] matrix, int target) {
+ // binary elimination from top right
+ int row = 0;
+ int col = matrix[0].length - 1;
+ int count = 0;
+ while (row < matrix.length && col>= 0) {
+ if (matrix[row][col] <= target) { + // get the count in current row + count += col + 1; + row++; + } else if (matrix[row][col]> target) {
+ // eliminate the current col
+ col--;
+ }
+ }
+ return count;
+ }
+}
diff --git a/src/main/java/g0301_0400/s0378_kth_smallest_element_in_a_sorted_matrix/readme.md b/src/main/java/g0301_0400/s0378_kth_smallest_element_in_a_sorted_matrix/readme.md
new file mode 100644
index 000000000..85803a3c0
--- /dev/null
+++ b/src/main/java/g0301_0400/s0378_kth_smallest_element_in_a_sorted_matrix/readme.md
@@ -0,0 +1,36 @@
+378\. Kth Smallest Element in a Sorted Matrix
+
+Medium
+
+Given an `n x n` `matrix` where each of the rows and columns is sorted in ascending order, return _the_ kth
_smallest element in the matrix_.
+
+Note that it is the kth
smallest element **in the sorted order**, not the kth
**distinct** element.
+
+You must find a solution with a memory complexity better than O(n2)
.
+
+**Example 1:**
+
+**Input:** matrix = \[\[1,5,9\],\[10,11,13\],\[12,13,15\]\], k = 8
+
+**Output:** 13
+
+**Explanation:** The elements in the matrix are \[1,5,9,10,11,12,13,**13**,15\], and the 8th smallest number is 13
+
+**Example 2:**
+
+**Input:** matrix = \[\[-5\]\], k = 1
+
+**Output:** -5
+
+**Constraints:**
+
+* `n == matrix.length == matrix[i].length`
+* `1 <= n <= 300` +* -109 <= matrix[i][j] <= 109
+* All the rows and columns of `matrix` are **guaranteed** to be sorted in **non-decreasing order**.
+* 1 <= k <= n2
+
+**Follow up:**
+
+* Could you solve the problem with a constant memory (i.e., `O(1)` memory complexity)?
+* Could you solve the problem in `O(n)` time complexity? The solution may be too advanced for an interview but you may find reading [this paper](http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf) fun.
\ No newline at end of file
diff --git a/src/main/java/g0301_0400/s0380_insert_delete_getrandom_o1/RandomizedSet.java b/src/main/java/g0301_0400/s0380_insert_delete_getrandom_o1/RandomizedSet.java
new file mode 100644
index 000000000..a979a2c6d
--- /dev/null
+++ b/src/main/java/g0301_0400/s0380_insert_delete_getrandom_o1/RandomizedSet.java
@@ -0,0 +1,64 @@
+package g0301_0400.s0380_insert_delete_getrandom_o1;
+
+// #Medium #Top_Interview_Questions #Array #Hash_Table #Math #Design #Randomized
+
+import java.security.SecureRandom;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class RandomizedSet {
+ private final SecureRandom rand;
+ private final List-231 <= val <= 231 - 1
+* At most `2 * `105
calls will be made to `insert`, `remove`, and `getRandom`.
+* There will be **at least one** element in the data structure when `getRandom` is called.
\ No newline at end of file
diff --git a/src/test/java/g0301_0400/s0374_guess_number_higher_or_lower/SolutionTest.java b/src/test/java/g0301_0400/s0374_guess_number_higher_or_lower/SolutionTest.java
new file mode 100644
index 000000000..51fc1bcff
--- /dev/null
+++ b/src/test/java/g0301_0400/s0374_guess_number_higher_or_lower/SolutionTest.java
@@ -0,0 +1,28 @@
+package g0301_0400.s0374_guess_number_higher_or_lower;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+import org.junit.jupiter.api.Test;
+
+class SolutionTest {
+ @Test
+ void guessNumber() {
+ assertThat(new Solution().guessNumber(10), equalTo(7));
+ }
+
+ @Test
+ void guessNumber2() {
+ assertThat(new Solution().guessNumber(1), equalTo(-1));
+ }
+
+ @Test
+ void guessNumber3() {
+ assertThat(new Solution().guessNumber(2), equalTo(-1));
+ }
+
+ @Test
+ void guessNumber4() {
+ assertThat(new Solution().guessNumber(6), equalTo(-1));
+ }
+}
diff --git a/src/test/java/g0301_0400/s0378_kth_smallest_element_in_a_sorted_matrix/SolutionTest.java b/src/test/java/g0301_0400/s0378_kth_smallest_element_in_a_sorted_matrix/SolutionTest.java
new file mode 100644
index 000000000..c7e4a948b
--- /dev/null
+++ b/src/test/java/g0301_0400/s0378_kth_smallest_element_in_a_sorted_matrix/SolutionTest.java
@@ -0,0 +1,20 @@
+package g0301_0400.s0378_kth_smallest_element_in_a_sorted_matrix;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+import org.junit.jupiter.api.Test;
+
+class SolutionTest {
+ @Test
+ void kthSmallest() {
+ assertThat(
+ new Solution().kthSmallest(new int[][] {{1, 5, 9}, {10, 11, 13}, {12, 13, 15}}, 8),
+ equalTo(13));
+ }
+
+ @Test
+ void kthSmallest2() {
+ assertThat(new Solution().kthSmallest(new int[][] {{-5}}, 1), equalTo(-5));
+ }
+}
diff --git a/src/test/java/g0301_0400/s0380_insert_delete_getrandom_o1/RandomizedSetTest.java b/src/test/java/g0301_0400/s0380_insert_delete_getrandom_o1/RandomizedSetTest.java
new file mode 100644
index 000000000..b47c9d07c
--- /dev/null
+++ b/src/test/java/g0301_0400/s0380_insert_delete_getrandom_o1/RandomizedSetTest.java
@@ -0,0 +1,38 @@
+package g0301_0400.s0380_insert_delete_getrandom_o1;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import org.junit.jupiter.api.Test;
+
+class RandomizedSetTest {
+ @Test
+ void randomizedSet() {
+ List