diff --git a/src/main/java/g0901_1000/s0932_beautiful_array/Solution.java b/src/main/java/g0901_1000/s0932_beautiful_array/Solution.java new file mode 100644 index 000000000..d78fe037c --- /dev/null +++ b/src/main/java/g0901_1000/s0932_beautiful_array/Solution.java @@ -0,0 +1,41 @@ +package g0901_1000.s0932_beautiful_array; + +// #Medium #Array #Math #Divide_and_Conquer + +import java.util.HashMap; +import java.util.Map; + +public class Solution { + private Map memo; + + public int[] beautifulArray(int n) { + memo = new HashMap<>(); + + return helper(n); + } + + private int[] helper(int n) { + if (n == 1) { + memo.put(1, new int[] {1}); + return new int[] {1}; + } + + if (memo.containsKey(n)) { + return memo.get(n); + } + + int mid = (n + 1) / 2; + int[] left = helper(mid); + int[] right = helper(n - mid); + int[] rst = new int[n]; + for (int i = 0; i < mid; i++) { + rst[i] = left[i] * 2 - 1; + } + for (int i = mid; i < n; i++) { + rst[i] = right[i - mid] * 2; + } + + memo.put(n, rst); + return rst; + } +} diff --git a/src/main/java/g0901_1000/s0932_beautiful_array/readme.md b/src/main/java/g0901_1000/s0932_beautiful_array/readme.md new file mode 100644 index 000000000..9ed1919c3 --- /dev/null +++ b/src/main/java/g0901_1000/s0932_beautiful_array/readme.md @@ -0,0 +1,26 @@ +932\. Beautiful Array + +Medium + +An array `nums` of length `n` is **beautiful** if: + +* `nums` is a permutation of the integers in the range `[1, n]`. +* For every `0 <= i < j < n`, there is no index `k` with `i < k < j` where `2 * nums[k] == nums[i] + nums[j]`. + +Given the integer `n`, return _any **beautiful** array_ `nums` _of length_ `n`. There will be at least one valid answer for the given `n`. + +**Example 1:** + +**Input:** n = 4 + +**Output:** [2,1,4,3] + +**Example 2:** + +**Input:** n = 5 + +**Output:** [3,1,2,5,4] + +**Constraints:** + +* `1 <= n <= 1000` \ No newline at end of file diff --git a/src/main/java/g0901_1000/s0933_number_of_recent_calls/RecentCounter.java b/src/main/java/g0901_1000/s0933_number_of_recent_calls/RecentCounter.java new file mode 100644 index 000000000..b155b0085 --- /dev/null +++ b/src/main/java/g0901_1000/s0933_number_of_recent_calls/RecentCounter.java @@ -0,0 +1,23 @@ +package g0901_1000.s0933_number_of_recent_calls; + +// #Easy #Design #Queue #Data_Stream + +import java.util.LinkedList; +import java.util.Queue; + +public class RecentCounter { + private Queue q; + + public RecentCounter() { + q = new LinkedList<>(); + } + + public int ping(int t) { + q.offer(t); + int min = t - 3000; + while (min > q.peek()) { + q.poll(); + } + return q.size(); + } +} diff --git a/src/main/java/g0901_1000/s0933_number_of_recent_calls/readme.md b/src/main/java/g0901_1000/s0933_number_of_recent_calls/readme.md new file mode 100644 index 000000000..c8bd1bceb --- /dev/null +++ b/src/main/java/g0901_1000/s0933_number_of_recent_calls/readme.md @@ -0,0 +1,26 @@ +933\. Number of Recent Calls + +Easy + +You have a `RecentCounter` class which counts the number of recent requests within a certain time frame. + +Implement the `RecentCounter` class: + +* `RecentCounter()` Initializes the counter with zero recent requests. +* `int ping(int t)` Adds a new request at time `t`, where `t` represents some time in milliseconds, and returns the number of requests that has happened in the past `3000` milliseconds (including the new request). Specifically, return the number of requests that have happened in the inclusive range `[t - 3000, t]`. + +It is **guaranteed** that every call to `ping` uses a strictly larger value of `t` than the previous call. + +**Example 1:** + +**Input** ["RecentCounter", "ping", "ping", "ping", "ping"] [[], [1], [100], [3001], [3002]] + +**Output:** [null, 1, 2, 3, 3] + +**Explanation:** RecentCounter recentCounter = new RecentCounter(); recentCounter.ping(1); // requests = [1], range is [-2999,1], return 1 recentCounter.ping(100); // requests = [1, 100], range is [-2900,100], return 2 recentCounter.ping(3001); // requests = [1, 100, 3001], range is [1,3001], return 3 recentCounter.ping(3002); // requests = [1, 100, 3001, 3002], range is [2,3002], return 3 + +**Constraints:** + +* 1 <= t <= 109 +* Each test case will call `ping` with **strictly increasing** values of `t`. +* At most 104 calls will be made to `ping`. \ No newline at end of file diff --git a/src/main/java/g0901_1000/s0934_shortest_bridge/Solution.java b/src/main/java/g0901_1000/s0934_shortest_bridge/Solution.java new file mode 100644 index 000000000..663de5255 --- /dev/null +++ b/src/main/java/g0901_1000/s0934_shortest_bridge/Solution.java @@ -0,0 +1,80 @@ +package g0901_1000.s0934_shortest_bridge; + +// #Medium #Array #Depth_First_Search #Breadth_First_Search #Matrix + +import java.util.ArrayDeque; + +public class Solution { + public static class Pair { + int x; + int y; + + Pair(int x, int y) { + this.x = x; + this.y = y; + } + } + + int[][] dirs = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; + + public int shortestBridge(int[][] grid) { + ArrayDeque q = new ArrayDeque<>(); + boolean flag = false; + boolean[][] visited = new boolean[grid.length][grid[0].length]; + + for (int i = 0; i < grid.length && !flag; i++) { + for (int j = 0; j < grid[i].length && !flag; j++) { + if (grid[i][j] == 1) { + dfs(grid, i, j, visited, q); + flag = true; + } + } + } + + int level = -1; + + while (!q.isEmpty()) { + int size = q.size(); + level++; + while (size-- > 0) { + Pair rem = q.removeFirst(); + for (int[] dir : dirs) { + int newrow = rem.x + dir[0]; + int newcol = rem.y + dir[1]; + + if (newrow >= 0 + && newcol >= 0 + && newrow < grid.length + && newcol < grid[0].length + && !visited[newrow][newcol]) { + if (grid[newrow][newcol] == 1) { + return level; + } + + q.add(new Pair(newrow, newcol)); + visited[newrow][newcol] = true; + } + } + } + } + return -1; + } + + private void dfs(int[][] grid, int row, int col, boolean[][] visited, ArrayDeque q) { + visited[row][col] = true; + q.add(new Pair(row, col)); + for (int[] dir : dirs) { + int newrow = row + dir[0]; + int newcol = col + dir[1]; + + if (newrow >= 0 + && newcol >= 0 + && newrow < grid.length + && newcol < grid[0].length + && !visited[newrow][newcol] + && grid[newrow][newcol] == 1) { + dfs(grid, newrow, newcol, visited, q); + } + } + } +} diff --git a/src/main/java/g0901_1000/s0934_shortest_bridge/readme.md b/src/main/java/g0901_1000/s0934_shortest_bridge/readme.md new file mode 100644 index 000000000..dc23cefe4 --- /dev/null +++ b/src/main/java/g0901_1000/s0934_shortest_bridge/readme.md @@ -0,0 +1,36 @@ +934\. Shortest Bridge + +Medium + +You are given an `n x n` binary matrix `grid` where `1` represents land and `0` represents water. + +An **island** is a 4-directionally connected group of `1`'s not connected to any other `1`'s. There are **exactly two islands** in `grid`. + +You may change `0`'s to `1`'s to connect the two islands to form **one island**. + +Return _the smallest number of_ `0`_'s you must flip to connect the two islands_. + +**Example 1:** + +**Input:** grid = [[0,1],[1,0]] + +**Output:** 1 + +**Example 2:** + +**Input:** grid = [[0,1,0],[0,0,0],[0,0,1]] + +**Output:** 2 + +**Example 3:** + +**Input:** grid = [[1,1,1,1,1],[1,0,0,0,1],[1,0,1,0,1],[1,0,0,0,1],[1,1,1,1,1]] + +**Output:** 1 + +**Constraints:** + +* `n == grid.length == grid[i].length` +* `2 <= n <= 100` +* `grid[i][j]` is either `0` or `1`. +* There are exactly two islands in `grid`. \ No newline at end of file diff --git a/src/main/java/g0901_1000/s0935_knight_dialer/Solution.java b/src/main/java/g0901_1000/s0935_knight_dialer/Solution.java new file mode 100644 index 000000000..aa9a51e1d --- /dev/null +++ b/src/main/java/g0901_1000/s0935_knight_dialer/Solution.java @@ -0,0 +1,48 @@ +package g0901_1000.s0935_knight_dialer; + +// #Medium #Dynamic_Programming + +import java.util.ArrayList; +import java.util.List; + +public class Solution { + + private static final int[][] MAP = new int[10][]; + private static final List MEMO = new ArrayList<>(); + + static { + MAP[0] = new int[] {4, 6}; + MAP[1] = new int[] {6, 8}; + MAP[2] = new int[] {7, 9}; + MAP[3] = new int[] {4, 8}; + MAP[4] = new int[] {3, 9, 0}; + MAP[5] = new int[0]; + MAP[6] = new int[] {1, 7, 0}; + MAP[7] = new int[] {2, 6}; + MAP[8] = new int[] {1, 3}; + MAP[9] = new int[] {2, 4}; + MEMO.add(new int[] {1, 1, 1, 1, 1, 0, 1, 1, 1, 1}); + } + + public int knightDialer(int n) { + if (n == 1) { + return 10; + } + int mod = 1000_000_007; + while (MEMO.size() < n) { + int[] cur = MEMO.get(MEMO.size() - 1); + int[] next = new int[10]; + for (int i = 0; i < 10; i++) { + for (int d : MAP[i]) { + next[d] = (next[d] + cur[i]) % mod; + } + } + MEMO.add(next); + } + int sum = 0; + for (int x : MEMO.get(n - 1)) { + sum = (sum + x) % mod; + } + return sum; + } +} diff --git a/src/main/java/g0901_1000/s0935_knight_dialer/readme.md b/src/main/java/g0901_1000/s0935_knight_dialer/readme.md new file mode 100644 index 000000000..cf7062e65 --- /dev/null +++ b/src/main/java/g0901_1000/s0935_knight_dialer/readme.md @@ -0,0 +1,47 @@ +935\. Knight Dialer + +Medium + +The chess knight has a **unique movement**, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an **L**). The possible movements of chess knight are shown in this diagaram: + +A chess knight can move as indicated in the chess diagram below: + +![](https://assets.leetcode.com/uploads/2020/08/18/chess.jpg) + +We have a chess knight and a phone pad as shown below, the knight **can only stand on a numeric cell** (i.e. blue cell). + +![](https://assets.leetcode.com/uploads/2020/08/18/phone.jpg) + +Given an integer `n`, return how many distinct phone numbers of length `n` we can dial. + +You are allowed to place the knight **on any numeric cell** initially and then you should perform `n - 1` jumps to dial a number of length `n`. All jumps should be **valid** knight jumps. + +As the answer may be very large, **return the answer modulo** 109 + 7. + +**Example 1:** + +**Input:** n = 1 + +**Output:** 10 + +**Explanation:** We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. + +**Example 2:** + +**Input:** n = 2 + +**Output:** 20 + +**Explanation:** All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] + +**Example 3:** + +**Input:** n = 3131 + +**Output:** 136006598 + +**Explanation:** Please take care of the mod. + +**Constraints:** + +* `1 <= n <= 5000` \ No newline at end of file diff --git a/src/test/java/com_github_leetcode/CommonUtils.java b/src/test/java/com_github_leetcode/CommonUtils.java index 839219409..b719f347b 100644 --- a/src/test/java/com_github_leetcode/CommonUtils.java +++ b/src/test/java/com_github_leetcode/CommonUtils.java @@ -6,14 +6,30 @@ public class CommonUtils { - public static void printArray(int[] nums) { + public static void printArrayInteger(int[] nums) { for (int i : nums) { System.out.print(i + ", "); } System.out.println(); } - public static void printArray(double[] nums) { + public static boolean compareArray(int[] arr1, int[] arr2) { + for (int i : arr1) { + boolean include = false; + for (int j : arr2) { + if (i == j) { + include = true; + break; + } + } + if (!include) { + return false; + } + } + return true; + } + + public static void printArrayDouble(double[] nums) { for (double i : nums) { System.out.print(i + ", "); } diff --git a/src/test/java/g0901_1000/s0932_beautiful_array/SolutionTest.java b/src/test/java/g0901_1000/s0932_beautiful_array/SolutionTest.java new file mode 100644 index 000000000..fb02768ca --- /dev/null +++ b/src/test/java/g0901_1000/s0932_beautiful_array/SolutionTest.java @@ -0,0 +1,23 @@ +package g0901_1000.s0932_beautiful_array; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import com_github_leetcode.CommonUtils; +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void beautifulArray() { + int[] result = new Solution().beautifulArray(4); + int[] expected = new int[] {2, 1, 4, 3}; + assertThat(CommonUtils.compareArray(result, expected), equalTo(true)); + } + + @Test + void beautifulArray2() { + int[] result = new Solution().beautifulArray(5); + int[] expected = new int[] {3, 1, 2, 5, 4}; + assertThat(CommonUtils.compareArray(result, expected), equalTo(true)); + } +} diff --git a/src/test/java/g0901_1000/s0933_number_of_recent_calls/RecentCounterTest.java b/src/test/java/g0901_1000/s0933_number_of_recent_calls/RecentCounterTest.java new file mode 100644 index 000000000..dab87746f --- /dev/null +++ b/src/test/java/g0901_1000/s0933_number_of_recent_calls/RecentCounterTest.java @@ -0,0 +1,18 @@ +package g0901_1000.s0933_number_of_recent_calls; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class RecentCounterTest { + + @Test + void recentCounterTest() { + RecentCounter recentCounter = new RecentCounter(); + assertThat(recentCounter.ping(1), equalTo(1)); + assertThat(recentCounter.ping(100), equalTo(2)); + assertThat(recentCounter.ping(3001), equalTo(3)); + assertThat(recentCounter.ping(3002), equalTo(3)); + } +} diff --git a/src/test/java/g0901_1000/s0934_shortest_bridge/SolutionTest.java b/src/test/java/g0901_1000/s0934_shortest_bridge/SolutionTest.java new file mode 100644 index 000000000..2b6c45d17 --- /dev/null +++ b/src/test/java/g0901_1000/s0934_shortest_bridge/SolutionTest.java @@ -0,0 +1,35 @@ +package g0901_1000.s0934_shortest_bridge; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void shortestBridge() { + assertThat(new Solution().shortestBridge(new int[][] {{0, 1}, {1, 0}}), equalTo(1)); + } + + @Test + void shortestBridge2() { + assertThat( + new Solution().shortestBridge(new int[][] {{0, 1, 0}, {0, 0, 0}, {0, 0, 1}}), + equalTo(2)); + } + + @Test + void shortestBridge3() { + assertThat( + new Solution() + .shortestBridge( + new int[][] { + {1, 1, 1, 1, 1}, + {1, 0, 0, 0, 1}, + {1, 0, 1, 0, 1}, + {1, 0, 0, 0, 1}, + {1, 1, 1, 1, 1} + }), + equalTo(1)); + } +} diff --git a/src/test/java/g0901_1000/s0935_knight_dialer/SolutionTest.java b/src/test/java/g0901_1000/s0935_knight_dialer/SolutionTest.java new file mode 100644 index 000000000..26345199f --- /dev/null +++ b/src/test/java/g0901_1000/s0935_knight_dialer/SolutionTest.java @@ -0,0 +1,23 @@ +package g0901_1000.s0935_knight_dialer; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void knightDialer() { + assertThat(new Solution().knightDialer(1), equalTo(10)); + } + + @Test + void knightDialer2() { + assertThat(new Solution().knightDialer(2), equalTo(20)); + } + + @Test + void knightDialer3() { + assertThat(new Solution().knightDialer(3131), equalTo(136006598)); + } +}