-
Notifications
You must be signed in to change notification settings - Fork 90
Added tasks 932, 933, 934, 935 #412
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
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
src/main/java/g0901_1000/s0932_beautiful_array/Solution.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Integer, int[]> 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; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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` |
23 changes: 23 additions & 0 deletions
23
src/main/java/g0901_1000/s0933_number_of_recent_calls/RecentCounter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Integer> 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(); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/g0901_1000/s0933_number_of_recent_calls/readme.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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:** | ||
|
||
* <code>1 <= t <= 10<sup>9</sup></code> | ||
* Each test case will call `ping` with **strictly increasing** values of `t`. | ||
* At most <code>10<sup>4</sup></code> calls will be made to `ping`. |
80 changes: 80 additions & 0 deletions
80
src/main/java/g0901_1000/s0934_shortest_bridge/Solution.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Pair> 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<Pair> 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); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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`. |
48 changes: 48 additions & 0 deletions
48
src/main/java/g0901_1000/s0935_knight_dialer/Solution.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<int[]> 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; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: | ||
|
||
 | ||
|
||
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). | ||
|
||
 | ||
|
||
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** <code>10<sup>9</sup> + 7</code>. | ||
|
||
**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` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/test/java/g0901_1000/s0932_beautiful_array/SolutionTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need it? assertThat/equalTo can compare arrays.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
items in these array has not same order
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, let's use this method.