Merge remote-tracking branch 'origin/master'

This commit is contained in:
轩辕龙儿 2022-02-12 21:49:41 +08:00
commit 8535c8c1f4
4 changed files with 268 additions and 0 deletions

View File

@ -0,0 +1,111 @@
//给你一个在 X-Y 平面上的点构成的数据流设计一个满足下述要求的算法
//
//
// 添加 一个在数据流中的新点到某个数据结构中可以添加 重复 的点并会视作不同的点进行处理
// 给你一个查询点请你从数据结构中选出三个点使这三个点和查询点一同构成一个 面积为正 轴对齐正方形 统计 满足该要求的方案数目
//
//
// 轴对齐正方形 是一个正方形除四条边长度相同外还满足每条边都与 x- y- 平行或垂直
//
// 实现 DetectSquares
//
//
// DetectSquares() 使用空数据结构初始化对象
// void add(int[] point) 向数据结构添加一个新的点 point = [x, y]
// int count(int[] point) 统计按上述方式与点 point = [x, y] 共同构造 轴对齐正方形 的方案数
//
//
//
//
// 示例
//
//
//输入
//["DetectSquares", "add", "add", "add", "count", "count", "add", "count"]
//[[], [[3, 10]], [[11, 2]], [[3, 2]], [[11, 10]], [[14, 8]], [[11, 2]], [[11, 1
//0]]]
//输出
//[null, null, null, null, 1, 0, null, 2]
//
//解释
//DetectSquares detectSquares = new DetectSquares();
//detectSquares.add([3, 10]);
//detectSquares.add([11, 2]);
//detectSquares.add([3, 2]);
//detectSquares.count([11, 10]); // 返回 1 你可以选择
// // - 第一个第二个和第三个点
//detectSquares.count([14, 8]); // 返回 0 查询点无法与数据结构中的这些点构成正方形
//detectSquares.add([11, 2]); // 允许添加重复的点
//detectSquares.count([11, 10]); // 返回 2 你可以选择
// // - 第一个第二个和第三个点
// // - 第一个第三个和第四个点
//
//
//
//
// 提示
//
//
// point.length == 2
// 0 <= x, y <= 1000
// 调用 add count 总次数 最多为 5000
//
// Related Topics 设计 数组 哈希表 计数 👍 65 👎 0
package leetcode.editor.cn;
import java.util.HashMap;
import java.util.Map;
//2013:检测正方形
public class DetectSquares1 {
public static void main(String[] args) {
// Solution solution = new DetectSquares().new Solution();
// TO TEST
}
//leetcode submit region begin(Prohibit modification and deletion)
class DetectSquares {
Map<Integer, Map<Integer, Integer>> xmap;
public DetectSquares() {
xmap = new HashMap<>();
}
public void add(int[] point) {
Map<Integer, Integer> map = xmap.getOrDefault(point[0], new HashMap<>());
map.put(point[1], map.getOrDefault(point[1], 0) + 1);
xmap.put(point[0], map);
}
public int count(int[] point) {
int x = point[0], y = point[1];
int ans = 0;
Map<Integer, Integer> col2Cnt = xmap.getOrDefault(x, new HashMap<>());
for (int ny : col2Cnt.keySet()) {
if (ny == y) {
continue;
}
int c1 = col2Cnt.get(ny);
int len = y - ny;
int[] nums = new int[]{x + len, x - len};
for (int nx : nums) {
Map<Integer, Integer> temp = xmap.getOrDefault(nx, new HashMap<>());
int c2 = temp.getOrDefault(y, 0), c3 = temp.getOrDefault(ny, 0);
ans += c1 * c2 * c3;
}
}
return ans;
}
}
/**
* Your DetectSquares object will be instantiated and called as such:
* DetectSquares obj = new DetectSquares();
* obj.add(point);
* int param_2 = obj.count(point);
*/
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,66 @@
//给你一个 下标从 0 开始 的整数数组 nums 其中 nums[i] 表示第 i 名学生的分数另给你一个整数 k
//
// 从数组中选出任意 k 名学生的分数使这 k 个分数间 最高分 最低分 差值 达到 最小化
//
// 返回可能的 最小差值
//
//
//
// 示例 1
//
// 输入nums = [90], k = 1
//输出0
//解释选出 1 名学生的分数仅有 1 种方法
//- [90] 最高分和最低分之间的差值是 90 - 90 = 0
//可能的最小差值是 0
//
//
// 示例 2
//
// 输入nums = [9,4,1,7], k = 2
//输出2
//解释选出 2 名学生的分数 6 种方法
//- [9,4,1,7] 最高分和最低分之间的差值是 9 - 4 = 5
//- [9,4,1,7] 最高分和最低分之间的差值是 9 - 1 = 8
//- [9,4,1,7] 最高分和最低分之间的差值是 9 - 7 = 2
//- [9,4,1,7] 最高分和最低分之间的差值是 4 - 1 = 3
//- [9,4,1,7] 最高分和最低分之间的差值是 7 - 4 = 3
//- [9,4,1,7] 最高分和最低分之间的差值是 7 - 1 = 6
//可能的最小差值是 2
//
//
//
// 提示
//
//
// 1 <= k <= nums.length <= 1000
// 0 <= nums[i] <= 10
//
// Related Topics 数组 排序 滑动窗口 👍 37 👎 0
package leetcode.editor.cn;
import java.util.Arrays;
import java.util.Collection;
//1984:学生分数的最小差值
public class MinimumDifferenceBetweenHighestAndLowestOfKScores {
public static void main(String[] args) {
Solution solution = new MinimumDifferenceBetweenHighestAndLowestOfKScores().new Solution();
// TO TEST
}
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int minimumDifference(int[] nums, int k) {
Arrays.sort(nums);
int min = Integer.MAX_VALUE;
for (int i = 0; i <= nums.length - k; i++) {
min = Math.min(min, nums[i + k - 1] - nums[i]);
}
return min;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,52 @@
<p>给你一个在 X-Y 平面上的点构成的数据流。设计一个满足下述要求的算法:</p>
<ul>
<li><strong>添加</strong> 一个在数据流中的新点到某个数据结构中<strong></strong>可以添加 <strong>重复</strong> 的点,并会视作不同的点进行处理。</li>
<li>给你一个查询点,请你从数据结构中选出三个点,使这三个点和查询点一同构成一个 <strong>面积为正</strong><strong>轴对齐正方形</strong> <strong>统计</strong> 满足该要求的方案数目<strong></strong></li>
</ul>
<p><strong>轴对齐正方形</strong> 是一个正方形,除四条边长度相同外,还满足每条边都与 x-轴 或 y-轴 平行或垂直。</p>
<p>实现 <code>DetectSquares</code> 类:</p>
<ul>
<li><code>DetectSquares()</code> 使用空数据结构初始化对象</li>
<li><code>void add(int[] point)</code> 向数据结构添加一个新的点 <code>point = [x, y]</code></li>
<li><code>int count(int[] point)</code> 统计按上述方式与点 <code>point = [x, y]</code> 共同构造 <strong>轴对齐正方形</strong> 的方案数。</li>
</ul>
<p>&nbsp;</p>
<p><strong>示例:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/09/01/image.png" style="width: 869px; height: 504px;" />
<pre>
<strong>输入:</strong>
["DetectSquares", "add", "add", "add", "count", "count", "add", "count"]
[[], [[3, 10]], [[11, 2]], [[3, 2]], [[11, 10]], [[14, 8]], [[11, 2]], [[11, 10]]]
<strong>输出:</strong>
[null, null, null, null, 1, 0, null, 2]
<strong>解释:</strong>
DetectSquares detectSquares = new DetectSquares();
detectSquares.add([3, 10]);
detectSquares.add([11, 2]);
detectSquares.add([3, 2]);
detectSquares.count([11, 10]); // 返回 1 。你可以选择:
// - 第一个,第二个,和第三个点
detectSquares.count([14, 8]); // 返回 0 。查询点无法与数据结构中的这些点构成正方形。
detectSquares.add([11, 2]); // 允许添加重复的点。
detectSquares.count([11, 10]); // 返回 2 。你可以选择:
// - 第一个,第二个,和第三个点
// - 第一个,第三个,和第四个点
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>point.length == 2</code></li>
<li><code>0 &lt;= x, y &lt;= 1000</code></li>
<li>调用&nbsp;<code>add</code><code>count</code><strong>总次数</strong> 最多为 <code>5000</code></li>
</ul>
<div><div>Related Topics</div><div><li>设计</li><li>数组</li><li>哈希表</li><li>计数</li></div></div><br><div><li>👍 65</li><li>👎 0</li></div>

View File

@ -0,0 +1,39 @@
<p>给你一个 <strong>下标从 0 开始</strong> 的整数数组 <code>nums</code> ,其中 <code>nums[i]</code> 表示第 <code>i</code> 名学生的分数。另给你一个整数 <code>k</code></p>
<p>从数组中选出任意 <code>k</code> 名学生的分数,使这 <code>k</code> 个分数间 <strong>最高分</strong><strong>最低分</strong><strong>差值</strong> 达到<strong> 最小化</strong></p>
<p>返回可能的 <strong>最小差值</strong></p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre><strong>输入:</strong>nums = [90], k = 1
<strong>输出:</strong>0
<strong>解释:</strong>选出 1 名学生的分数,仅有 1 种方法:
- [<em><strong>90</strong></em>] 最高分和最低分之间的差值是 90 - 90 = 0
可能的最小差值是 0
</pre>
<p><strong>示例 2</strong></p>
<pre><strong>输入:</strong>nums = [9,4,1,7], k = 2
<strong>输出:</strong>2
<strong>解释:</strong>选出 2 名学生的分数,有 6 种方法:
- [<em><strong>9</strong></em>,<em><strong>4</strong></em>,1,7] 最高分和最低分之间的差值是 9 - 4 = 5
- [<em><strong>9</strong></em>,4,<em><strong>1</strong></em>,7] 最高分和最低分之间的差值是 9 - 1 = 8
- [<em><strong>9</strong></em>,4,1,<em><strong>7</strong></em>] 最高分和最低分之间的差值是 9 - 7 = 2
- [9,<em><strong>4</strong></em>,<em><strong>1</strong></em>,7] 最高分和最低分之间的差值是 4 - 1 = 3
- [9,<em><strong>4</strong></em>,1,<em><strong>7</strong></em>] 最高分和最低分之间的差值是 7 - 4 = 3
- [9,4,<em><strong>1</strong></em>,<em><strong>7</strong></em>] 最高分和最低分之间的差值是 7 - 1 = 6
可能的最小差值是 2</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= k &lt;= nums.length &lt;= 1000</code></li>
<li><code>0 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
</ul>
<div><div>Related Topics</div><div><li>数组</li><li>排序</li><li>滑动窗口</li></div></div><br><div><li>👍 37</li><li>👎 0</li></div>