Merge remote-tracking branch 'origin/master'
# Conflicts: # src/main/java/leetcode/editor/cn/doc/all.json
This commit is contained in:
commit
1a083e04e8
159
src/main/java/com/code/leet/doubleWeek/SolutionD60.java
Normal file
159
src/main/java/com/code/leet/doubleWeek/SolutionD60.java
Normal file
@ -0,0 +1,159 @@
|
|||||||
|
package com.code.leet.doubleWeek;
|
||||||
|
|
||||||
|
import java.math.BigInteger;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
public class SolutionD60 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SolutionD60 solution = new SolutionD60();
|
||||||
|
//5
|
||||||
|
// solution.numberOfGoodSubsets(new int[]{4, 2, 3, 15});
|
||||||
|
//6
|
||||||
|
// solution.numberOfGoodSubsets(new int[]{1, 2, 3, 4});
|
||||||
|
//7
|
||||||
|
// solution.numberOfGoodSubsets(new int[]{30, 28, 26, 30, 19, 12, 12, 25, 12});
|
||||||
|
//5368
|
||||||
|
solution.numberOfGoodSubsets(new int[]{5,10,1,26,24,21,24,23,11,12,27,4,17,16,2,6,1,1,6,8,13,30,24,20,2,19});
|
||||||
|
}
|
||||||
|
|
||||||
|
public int findMiddleIndex(int[] nums) {
|
||||||
|
int[] sums = new int[nums.length + 1];
|
||||||
|
for (int i = 0; i < nums.length; i++) {
|
||||||
|
sums[i + 1] = sums[i] + nums[i];
|
||||||
|
}
|
||||||
|
for (int i = 1; i < sums.length; i++) {
|
||||||
|
if (sums[i - 1] == sums[sums.length - 1] - sums[i]) {
|
||||||
|
return i - 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int[][] findFarmland(int[][] land) {
|
||||||
|
boolean[][] use = new boolean[land.length][land[0].length];
|
||||||
|
int[] xIndex = new int[]{1, 0};
|
||||||
|
int[] yIndex = new int[]{0, 1};
|
||||||
|
List<int[]> list = new ArrayList<>();
|
||||||
|
for (int i = 0; i < land.length; i++) {
|
||||||
|
for (int j = 0; j < land[0].length; j++) {
|
||||||
|
if (land[i][j] == 1 && !use[i][j]) {
|
||||||
|
Queue<int[]> queue = new LinkedList<>();
|
||||||
|
queue.add(new int[]{i, j});
|
||||||
|
use[i][j] = true;
|
||||||
|
int[] temp = new int[]{i, j, i, j};
|
||||||
|
while (!queue.isEmpty()) {
|
||||||
|
int[] arr = queue.poll();
|
||||||
|
for (int k = 0; k < 2; k++) {
|
||||||
|
int x = arr[0] + xIndex[k];
|
||||||
|
int y = arr[1] + yIndex[k];
|
||||||
|
if (x >= land.length || y >= land[0].length || land[x][y] == 0 || use[x][y]) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (x > temp[2] || y > temp[3]) {
|
||||||
|
temp[2] = x;
|
||||||
|
temp[3] = y;
|
||||||
|
}
|
||||||
|
use[x][y] = true;
|
||||||
|
queue.add(new int[]{x, y});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
list.add(temp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int[][] result = new int[list.size()][4];
|
||||||
|
for (int i = 0; i < list.size(); i++) {
|
||||||
|
result[i] = list.get(i);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// class LockingTree {
|
||||||
|
//
|
||||||
|
// int[] parent;
|
||||||
|
// boolean[] isLock;
|
||||||
|
// int[] userLock;
|
||||||
|
//
|
||||||
|
// public LockingTree(int[] parent) {
|
||||||
|
// this.parent = parent;
|
||||||
|
// isLock = new boolean[parent.length];
|
||||||
|
// userLock = new int[parent.length];
|
||||||
|
// Arrays.fill(userLock, -1);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public boolean lock(int num, int user) {
|
||||||
|
//
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public boolean unlock(int num, int user) {
|
||||||
|
//
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public boolean upgrade(int num, int user) {
|
||||||
|
//
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
long temp;
|
||||||
|
|
||||||
|
public int numberOfGoodSubsets(int[] nums) {
|
||||||
|
int[] counts = new int[31];
|
||||||
|
List<Integer> list = new ArrayList<>(Arrays.asList(2, 3, 5, 6, 7, 10, 11, 13, 14, 15, 17, 19, 21, 22, 23, 26, 29, 30));
|
||||||
|
long sum = 0;
|
||||||
|
for (int num : nums) {
|
||||||
|
counts[num]++;
|
||||||
|
}
|
||||||
|
for (int i = list.size() - 1; i >= 0; i--) {
|
||||||
|
if (counts[list.get(i)] == 0) {
|
||||||
|
list.remove(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Map<Integer, List<Integer>> pd = new HashMap<>();
|
||||||
|
pd.put(2, Arrays.asList(2, 6, 10, 14, 22, 26, 30));
|
||||||
|
pd.put(3, Arrays.asList(3, 6, 15, 21, 30));
|
||||||
|
pd.put(5, Arrays.asList(5, 10, 15, 30));
|
||||||
|
pd.put(7, Arrays.asList(7, 14, 21));
|
||||||
|
pd.put(11, Arrays.asList(11, 22));
|
||||||
|
pd.put(13, Arrays.asList(13, 26));
|
||||||
|
pd.put(17, Collections.singletonList(17));
|
||||||
|
pd.put(19, Collections.singletonList(19));
|
||||||
|
pd.put(23, Collections.singletonList(23));
|
||||||
|
pd.put(29, Collections.singletonList(29));
|
||||||
|
boolean[] use = new boolean[31];
|
||||||
|
dfs(list, use, 0, 0, pd, counts);
|
||||||
|
if (counts[1] > 0) {
|
||||||
|
sum += ((long) counts[1] * (counts[1] + 1) / 2 + 1) * temp;
|
||||||
|
} else {
|
||||||
|
sum = temp;
|
||||||
|
}
|
||||||
|
return (int) (sum % 1000000007);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void dfs(List<Integer> list, boolean[] use, long count, int index, Map<Integer, List<Integer>> pd, int[] counts) {
|
||||||
|
if (index == list.size()) {
|
||||||
|
temp += count;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
boolean bl = true;
|
||||||
|
List<Integer> l = new ArrayList<>();
|
||||||
|
for (int key : pd.keySet()) {
|
||||||
|
if (pd.get(key).contains(list.get(index))) {
|
||||||
|
if (!use[key]) {
|
||||||
|
l.add(key);
|
||||||
|
} else {
|
||||||
|
bl = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (bl) {
|
||||||
|
for (Integer integer : l) {
|
||||||
|
use[integer] = true;
|
||||||
|
}
|
||||||
|
dfs(list, use, Math.max(count, 1) * counts[list.get(index)], index + 1, pd, counts);
|
||||||
|
for (Integer integer : l) {
|
||||||
|
use[integer] = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
dfs(list, use, count, index + 1, pd, counts);
|
||||||
|
}
|
||||||
|
}
|
70
src/main/java/com/code/leet/week/Solution257.java
Normal file
70
src/main/java/com/code/leet/week/Solution257.java
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
package com.code.leet.week;
|
||||||
|
|
||||||
|
import com.code.leet.entiy.TwoArray;
|
||||||
|
|
||||||
|
import java.math.BigInteger;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description:
|
||||||
|
* @author: Administrator
|
||||||
|
* @date: 2021/8/22-10:29
|
||||||
|
*/
|
||||||
|
public class Solution257 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Solution257 solution = new Solution257();
|
||||||
|
// TwoArray twoArray = new TwoArray("[[1,1],[2,1],[2,2],[1,2]]", true);
|
||||||
|
// System.out.println(solution.numberOfWeakCharacters(twoArray.getArr()));
|
||||||
|
System.out.println(solution.firstDayBeenInAllRooms(new int[]{0, 0}));
|
||||||
|
}
|
||||||
|
|
||||||
|
public int countQuadruplets(int[] nums) {
|
||||||
|
int count = 0;
|
||||||
|
for (int i = 0; i < nums.length; i++) {
|
||||||
|
for (int j = i + 1; j < nums.length; j++) {
|
||||||
|
for (int k = j + 1; k < nums.length; k++) {
|
||||||
|
for (int l = k + 1; l < nums.length; l++) {
|
||||||
|
if (nums[i] + nums[j] + nums[k] == nums[l]) {
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int numberOfWeakCharacters(int[][] properties) {
|
||||||
|
Arrays.sort(properties, (o1, o2) -> o1[0] == o2[0] ? o2[1] - o1[1] : o2[0] - o1[0]);
|
||||||
|
TreeSet<Integer> set = new TreeSet<>();
|
||||||
|
int count = 0;
|
||||||
|
List<Integer> list = new ArrayList<>();
|
||||||
|
for (int i = 0; i < properties.length; i++) {
|
||||||
|
if (set.higher(properties[i][1]) != null) {
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
list.add(properties[i][1]);
|
||||||
|
if (i != properties.length - 1 && properties[i][0] != properties[i + 1][0]) {
|
||||||
|
set.addAll(list);
|
||||||
|
list.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (int i = properties.length - 1; i >= 0; i--) {
|
||||||
|
if (i != properties.length - 1 && properties[i][0] != properties[i + 1][0] && set.higher(properties[i][1]) != null) {
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
set.add(properties[i][1]);
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int firstDayBeenInAllRooms(int[] nextVisit) {
|
||||||
|
int[] dp = new int[nextVisit.length];
|
||||||
|
int[] sum = new int[nextVisit.length];
|
||||||
|
for (int i = 1; i < nextVisit.length; i++) {
|
||||||
|
sum[i] = dp[i - 1] - dp[nextVisit[i - 1]];
|
||||||
|
dp[i] = dp[i - 1] + sum[i];
|
||||||
|
}
|
||||||
|
return dp[nextVisit.length - 1];
|
||||||
|
}
|
||||||
|
}
|
65
src/main/java/leetcode/editor/cn/FeiBoNaQiShuLieLcof.java
Normal file
65
src/main/java/leetcode/editor/cn/FeiBoNaQiShuLieLcof.java
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
//写一个函数,输入 n ,求斐波那契(Fibonacci)数列的第 n 项(即 F(N))。斐波那契数列的定义如下:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//F(0) = 0, F(1) = 1
|
||||||
|
//F(N) = F(N - 1) + F(N - 2), 其中 N > 1.
|
||||||
|
//
|
||||||
|
// 斐波那契数列由 0 和 1 开始,之后的斐波那契数就是由之前的两数相加而得出。
|
||||||
|
//
|
||||||
|
// 答案需要取模 1e9+7(1000000007),如计算初始结果为:1000000008,请返回 1。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 1:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:n = 2
|
||||||
|
//输出:1
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 2:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:n = 5
|
||||||
|
//输出:5
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 提示:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 0 <= n <= 100
|
||||||
|
//
|
||||||
|
// Related Topics 记忆化搜索 数学 动态规划 👍 223 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
//剑指 Offer 10- I:斐波那契数列
|
||||||
|
class FeiBoNaQiShuLieLcof {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new FeiBoNaQiShuLieLcof().new Solution();
|
||||||
|
}
|
||||||
|
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
public int fib(int n) {
|
||||||
|
if (n < 2) {
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
int f0 = 0;
|
||||||
|
int f1 = 1;
|
||||||
|
long result = 0;
|
||||||
|
for (int i = 2; i <= n; ++i) {
|
||||||
|
result = (f0 + f1) % 1000000007;
|
||||||
|
f0 = f1;
|
||||||
|
f1 = (int) result;
|
||||||
|
}
|
||||||
|
return (int) result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,82 @@
|
|||||||
|
//已有方法 rand7 可生成 1 到 7 范围内的均匀随机整数,试写一个方法 rand10 生成 1 到 10 范围内的均匀随机整数。
|
||||||
|
//
|
||||||
|
// 不要使用系统的 Math.random() 方法。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 1:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入: 1
|
||||||
|
//输出: [7]
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 2:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入: 2
|
||||||
|
//输出: [8,4]
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 3:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入: 3
|
||||||
|
//输出: [8,1,10]
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 提示:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// rand7 已定义。
|
||||||
|
// 传入参数: n 表示 rand10 的调用次数。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 进阶:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// rand7()调用次数的 期望值 是多少 ?
|
||||||
|
// 你能否尽量少调用 rand7() ?
|
||||||
|
//
|
||||||
|
// Related Topics 数学 拒绝采样 概率与统计 随机化 👍 307 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
//470:用 Rand7() 实现 Rand10()
|
||||||
|
class ImplementRand10UsingRand7 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
// Solution solution = new ImplementRand10UsingRand7().new Solution();
|
||||||
|
}
|
||||||
|
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The rand7() API is already defined in the parent class SolBase.
|
||||||
|
* public int rand7();
|
||||||
|
*
|
||||||
|
* @return a random integer in the range 1 to 7
|
||||||
|
*/
|
||||||
|
// class Solution extends SolBase {
|
||||||
|
// public int rand10() {
|
||||||
|
// while (true) {
|
||||||
|
// int num = (rand7() - 1) * 7 + rand7();
|
||||||
|
// if (num <= 40) {
|
||||||
|
// return num % 10 + 1;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
File diff suppressed because one or more lines are too long
@ -0,0 +1,34 @@
|
|||||||
|
<p>写一个函数,输入 <code>n</code> ,求斐波那契(Fibonacci)数列的第 <code>n</code> 项(即 <code>F(N)</code>)。斐波那契数列的定义如下:</p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
F(0) = 0, F(1) = 1
|
||||||
|
F(N) = F(N - 1) + F(N - 2), 其中 N > 1.</pre>
|
||||||
|
|
||||||
|
<p>斐波那契数列由 0 和 1 开始,之后的斐波那契数就是由之前的两数相加而得出。</p>
|
||||||
|
|
||||||
|
<p>答案需要取模 1e9+7(1000000007),如计算初始结果为:1000000008,请返回 1。</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>n = 2
|
||||||
|
<strong>输出:</strong>1
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>n = 5
|
||||||
|
<strong>输出:</strong>5
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>0 <= n <= 100</code></li>
|
||||||
|
</ul>
|
||||||
|
<div><div>Related Topics</div><div><li>记忆化搜索</li><li>数学</li><li>动态规划</li></div></div><br><div><li>👍 223</li><li>👎 0</li></div>
|
@ -0,0 +1,48 @@
|
|||||||
|
<p>已有方法 <code>rand7</code> 可生成 1 到 7 范围内的均匀随机整数,试写一个方法 <code>rand10</code> 生成 1 到 10 范围内的均匀随机整数。</p>
|
||||||
|
|
||||||
|
<p>不要使用系统的 <code>Math.random()</code> 方法。</p>
|
||||||
|
|
||||||
|
<ol>
|
||||||
|
</ol>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入: </strong>1
|
||||||
|
<strong>输出: </strong>[7]
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入: </strong>2
|
||||||
|
<strong>输出: </strong>[8,4]
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 3:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入: </strong>3
|
||||||
|
<strong>输出: </strong>[8,1,10]
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong></p>
|
||||||
|
|
||||||
|
<ol>
|
||||||
|
<li><code>rand7</code> 已定义。</li>
|
||||||
|
<li>传入参数: <code>n</code> 表示 <code>rand10</code> 的调用次数。</li>
|
||||||
|
</ol>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>进阶:</strong></p>
|
||||||
|
|
||||||
|
<ol>
|
||||||
|
<li><code>rand7()</code>调用次数的 <a href="https://en.wikipedia.org/wiki/Expected_value" target="_blank">期望值</a> 是多少 ?</li>
|
||||||
|
<li>你能否尽量少调用 <code>rand7()</code> ?</li>
|
||||||
|
</ol>
|
||||||
|
<div><div>Related Topics</div><div><li>数学</li><li>拒绝采样</li><li>概率与统计</li><li>随机化</li></div></div><br><div><li>👍 307</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user