1711:大餐计数
This commit is contained in:
parent
4584dd083e
commit
a0c3f6a6dc
69
src/main/java/leetcode/editor/cn/CountGoodMeals.java
Normal file
69
src/main/java/leetcode/editor/cn/CountGoodMeals.java
Normal file
@ -0,0 +1,69 @@
|
||||
//大餐 是指 恰好包含两道不同餐品 的一餐,其美味程度之和等于 2 的幂。
|
||||
//
|
||||
// 你可以搭配 任意 两道餐品做一顿大餐。
|
||||
//
|
||||
// 给你一个整数数组 deliciousness ,其中 deliciousness[i] 是第 i 道餐品的美味程度,返回你可以用数组中的餐品做出的不同 大
|
||||
//餐 的数量。结果需要对 109 + 7 取余。
|
||||
//
|
||||
// 注意,只要餐品下标不同,就可以认为是不同的餐品,即便它们的美味程度相同。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
//
|
||||
//输入:deliciousness = [1,3,5,7,9]
|
||||
//输出:4
|
||||
//解释:大餐的美味程度组合为 (1,3) 、(1,7) 、(3,5) 和 (7,9) 。
|
||||
//它们各自的美味程度之和分别为 4 、8 、8 和 16 ,都是 2 的幂。
|
||||
//
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
//
|
||||
//输入:deliciousness = [1,1,1,3,3,3,7]
|
||||
//输出:15
|
||||
//解释:大餐的美味程度组合为 3 种 (1,1) ,9 种 (1,3) ,和 3 种 (1,7) 。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// 1 <= deliciousness.length <= 105
|
||||
// 0 <= deliciousness[i] <= 220
|
||||
//
|
||||
// Related Topics 数组 哈希表
|
||||
// 👍 72 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
//1711:大餐计数
|
||||
public class CountGoodMeals {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new CountGoodMeals().new Solution();
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int countPairs(int[] deliciousness) {
|
||||
Map<Integer, Integer> map = new HashMap<>();
|
||||
int result = 0;
|
||||
for (int num : deliciousness) {
|
||||
for (int i = 1; i < 22; i++) {
|
||||
result += map.getOrDefault(1 << i - num, 0);
|
||||
result %= 100000007;
|
||||
}
|
||||
map.put(num, map.getOrDefault(num, 0) + 1);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
35
src/main/java/leetcode/editor/cn/CountGoodMeals.md
Normal file
35
src/main/java/leetcode/editor/cn/CountGoodMeals.md
Normal file
@ -0,0 +1,35 @@
|
||||
<p><strong>大餐</strong> 是指 <strong>恰好包含两道不同餐品</strong> 的一餐,其美味程度之和等于 2 的幂。</p>
|
||||
|
||||
<p>你可以搭配 <strong>任意</strong> 两道餐品做一顿大餐。</p>
|
||||
|
||||
<p>给你一个整数数组 <code>deliciousness</code> ,其中 <code>deliciousness[i]</code> 是第 <code>i<sup></sup></code> 道餐品的美味程度,返回你可以用数组中的餐品做出的不同 <strong>大餐</strong> 的数量。结果需要对 <code>10<sup>9</sup> + 7</code> 取余。</p>
|
||||
|
||||
<p>注意,只要餐品下标不同,就可以认为是不同的餐品,即便它们的美味程度相同。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>deliciousness = [1,3,5,7,9]
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>大餐的美味程度组合为 (1,3) 、(1,7) 、(3,5) 和 (7,9) 。
|
||||
它们各自的美味程度之和分别为 4 、8 、8 和 16 ,都是 2 的幂。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>deliciousness = [1,1,1,3,3,3,7]
|
||||
<strong>输出:</strong>15
|
||||
<strong>解释:</strong>大餐的美味程度组合为 3 种 (1,1) ,9 种 (1,3) ,和 3 种 (1,7) 。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= deliciousness.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= deliciousness[i] <= 2<sup>20</sup></code></li>
|
||||
</ul>
|
||||
<div><div>Related Topics</div><div><li>数组</li><li>哈希表</li></div></div>\n<div><li>👍 72</li><li>👎 0</li></div>
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user