Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bfaf051

Browse files
committed
feat: add solutions to lc problems: No.2549~2552
* No.2549.Count Distinct Numbers on Board * No.2550.Count Collisions of Monkeys on a Polygon * No.2551.Put Marbles in Bag * No.2552.Count Increasing Quadruplets
1 parent f7cf05e commit bfaf051

File tree

35 files changed

+1566
-6
lines changed

35 files changed

+1566
-6
lines changed

‎solution/0100-0199/0170.Two Sum III - Data structure design/README_EN.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,11 @@ class TwoSum {
8080
public TwoSum() {
8181

8282
}
83-
83+
8484
public void add(int number) {
8585
cnt.merge(number, 1, Integer::sum);
8686
}
87-
87+
8888
public boolean find(int value) {
8989
for (var e : cnt.entrySet()) {
9090
int x = e.getKey(), v = e.getValue();
@@ -115,11 +115,11 @@ public:
115115
TwoSum() {
116116

117117
}
118-
118+
119119
void add(int number) {
120120
++cnt[number];
121121
}
122-
122+
123123
bool find(int value) {
124124
for (auto& [x, v] : cnt) {
125125
long y = (long) value - x;

‎solution/0500-0599/0599.Minimum Index Sum of Two Lists/README_EN.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ The strings with the least index sum are "sad" and "happy".
5050
<li><code>list1[i]</code> and <code>list2[i]</code> consist of spaces <code>&#39; &#39;</code> and English letters.</li>
5151
<li>All the strings of <code>list1</code> are <strong>unique</strong>.</li>
5252
<li>All the strings of <code>list2</code> are <strong>unique</strong>.</li>
53+
<li>There is at least a common string between <code>list1</code> and <code>list2</code>.</li>
5354
</ul>
5455

5556
## Solutions

‎solution/1600-1699/1671.Minimum Number of Removals to Make Mountain Array/README_EN.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ function minimumMountainRemovals(nums: number[]): number {
203203
}
204204
}
205205
return n - ans;
206-
};
206+
}
207207
```
208208

209209
### **...**

‎solution/2300-2399/2317.Maximum XOR After Operations/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646

4747
**方法一:位运算**
4848

49-
在一次操作中,我们可以把 $nums[i]$ 更新为 $nums[i] \& (nums[i] \oplus x),ドル其中 $\&$ 表示逐位与运算,$\oplus$ 表示逐位异或运算。由于 $x$ 是任意非负整数,因此 $nums[i] \oplus x$ 的结果是一个任意值,再与 $nums[i]$ 逐位与运算,可以把 $nums[i]$ 的二进制表示中的若干位 1ドル$ 变为 0ドル$。
49+
在一次操作中,我们可以把 `nums[i]` 更新为 `nums[i] AND (nums[i] XOR x)`。由于 $x$ 是任意非负整数,因此 $nums[i] \oplus x$ 的结果是一个任意值,再与 `nums[i]` 逐位与运算,可以把 `nums[i]` 的二进制表示中的若干位 1ドル$ 变为 0ドル$。
5050

5151
而题目中要获取的是 `nums` 所有元素的最大逐位异或和,对于一个二进制位,只要在 `nums` 中存在一个元素对应的二进制位为 1ドル,ドル那么这个二进制位对于最大逐位异或和的贡献就是 1ドル$。因此答案就是 `nums` 中所有元素的逐位或运算的结果。
5252

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# [2549. 统计桌面上的不同数字](https://leetcode.cn/problems/count-distinct-numbers-on-board)
2+
3+
[English Version](/solution/2500-2599/2549.Count%20Distinct%20Numbers%20on%20Board/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个正整数 <code>n</code> ,开始时,它放在桌面上。在 <code>10<sup>9</sup></code> 天内,每天都要执行下述步骤:</p>
10+
11+
<ul>
12+
<li>对于出现在桌面上的每个数字 <code>x</code> ,找出符合 <code>1 &lt;= i &lt;= n</code> 且满足 <code>x % i == 1</code> 的所有数字 <code>i</code> 。</li>
13+
<li>然后,将这些数字放在桌面上。</li>
14+
</ul>
15+
16+
<p>返回在 <code>10<sup>9</sup></code> 天之后,出现在桌面上的 <strong>不同</strong> 整数的数目。</p>
17+
18+
<p><strong>注意:</strong></p>
19+
20+
<ul>
21+
<li>一旦数字放在桌面上,则会一直保留直到结束。</li>
22+
<li><code>%</code> 表示取余运算。例如,<code>14 % 3</code> 等于 <code>2</code> 。</li>
23+
</ul>
24+
25+
<p>&nbsp;</p>
26+
27+
<p><strong>示例 1:</strong></p>
28+
29+
<pre>
30+
<strong>输入:</strong>n = 5
31+
<strong>输出:</strong>4
32+
<strong>解释:</strong>最开始,5 在桌面上。
33+
第二天,2 和 4 也出现在桌面上,因为 5 % 2 == 1 且 5 % 4 == 1 。
34+
再过一天 3 也出现在桌面上,因为 4 % 3 == 1 。
35+
在十亿天结束时,桌面上的不同数字有 2 、3 、4 、5 。
36+
</pre>
37+
38+
<p><strong>示例 2:</strong></p>
39+
40+
<pre>
41+
<strong>输入:</strong>n = 3
42+
<strong>输出:</strong>2
43+
<strong>解释:</strong>
44+
因为 3 % 2 == 1 ,2 也出现在桌面上。
45+
在十亿天结束时,桌面上的不同数字只有两个:2 和 3 。
46+
</pre>
47+
48+
<p>&nbsp;</p>
49+
50+
<p><strong>提示:</strong></p>
51+
52+
<ul>
53+
<li><code>1 &lt;= n &lt;= 100</code></li>
54+
</ul>
55+
56+
## 解法
57+
58+
<!-- 这里可写通用的实现逻辑 -->
59+
60+
**方法一:脑筋急转弯**
61+
62+
由于每一次对桌面上的数字 $n$ 进行操作,会使得数字 $n-1$ 也出现在桌面上,因此最终桌面上的数字为 $[2,...n],ドル即 $n-1$ 个数字。
63+
64+
注意到 $n$ 可能为 1ドル,ドル因此需要特判。
65+
66+
时间复杂度 $O(1),ドル空间复杂度 $O(1)$。
67+
68+
<!-- tabs:start -->
69+
70+
### **Python3**
71+
72+
<!-- 这里可写当前语言的特殊实现逻辑 -->
73+
74+
```python
75+
class Solution:
76+
def distinctIntegers(self, n: int) -> int:
77+
return max(1, n - 1)
78+
```
79+
80+
### **Java**
81+
82+
<!-- 这里可写当前语言的特殊实现逻辑 -->
83+
84+
```java
85+
class Solution {
86+
public int distinctIntegers(int n) {
87+
return Math.max(1, n - 1);
88+
}
89+
}
90+
```
91+
92+
### **C++**
93+
94+
```cpp
95+
class Solution {
96+
public:
97+
int distinctIntegers(int n) {
98+
return max(1, n - 1);
99+
}
100+
};
101+
```
102+
103+
### **Go**
104+
105+
```go
106+
func distinctIntegers(n int) int {
107+
if n == 1 {
108+
return 1
109+
}
110+
return n - 1
111+
}
112+
```
113+
114+
### **...**
115+
116+
```
117+
118+
```
119+
120+
<!-- tabs:end -->
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# [2549. Count Distinct Numbers on Board](https://leetcode.com/problems/count-distinct-numbers-on-board)
2+
3+
[中文文档](/solution/2500-2599/2549.Count%20Distinct%20Numbers%20on%20Board/README.md)
4+
5+
## Description
6+
7+
<p>You are given a positive integer <code>n</code>, that is initially placed on a board. Every day, for <code>10<sup>9</sup></code> days, you perform the following procedure:</p>
8+
9+
<ul>
10+
<li>For each number <code>x</code> present on the board, find all numbers <code>1 &lt;= i &lt;= n</code> such that <code>x % i == 1</code>.</li>
11+
<li>Then, place those numbers on the board.</li>
12+
</ul>
13+
14+
<p>Return<em> the number of <strong>distinct</strong> integers present on the board after</em> <code>10<sup>9</sup></code> <em>days have elapsed</em>.</p>
15+
16+
<p><strong>Note:</strong></p>
17+
18+
<ul>
19+
<li>Once a number is placed on the board, it will remain on it until the end.</li>
20+
<li><code>%</code>&nbsp;stands&nbsp;for the modulo operation. For example,&nbsp;<code>14 % 3</code> is <code>2</code>.</li>
21+
</ul>
22+
23+
<p>&nbsp;</p>
24+
<p><strong class="example">Example 1:</strong></p>
25+
26+
<pre>
27+
<strong>Input:</strong> n = 5
28+
<strong>Output:</strong> 4
29+
<strong>Explanation:</strong> Initially, 5 is present on the board.
30+
The next day, 2 and 4 will be added since 5 % 2 == 1 and 5 % 4 == 1.
31+
After that day, 3 will be added to the board because 4 % 3 == 1.
32+
At the end of a billion days, the distinct numbers on the board will be 2, 3, 4, and 5.
33+
</pre>
34+
35+
<p><strong class="example">Example 2:</strong></p>
36+
37+
<pre>
38+
<strong>Input:</strong> n = 3
39+
<strong>Output:</strong> 2
40+
<strong>Explanation:</strong>
41+
Since 3 % 2 == 1, 2 will be added to the board.
42+
After a billion days, the only two distinct numbers on the board are 2 and 3.
43+
</pre>
44+
45+
<p>&nbsp;</p>
46+
<p><strong>Constraints:</strong></p>
47+
48+
<ul>
49+
<li><code>1 &lt;= n &lt;= 100</code></li>
50+
</ul>
51+
52+
## Solutions
53+
54+
<!-- tabs:start -->
55+
56+
### **Python3**
57+
58+
```python
59+
class Solution:
60+
def distinctIntegers(self, n: int) -> int:
61+
return max(1, n - 1)
62+
```
63+
64+
### **Java**
65+
66+
```java
67+
class Solution {
68+
public int distinctIntegers(int n) {
69+
return Math.max(1, n - 1);
70+
}
71+
}
72+
```
73+
74+
### **C++**
75+
76+
```cpp
77+
class Solution {
78+
public:
79+
int distinctIntegers(int n) {
80+
return max(1, n - 1);
81+
}
82+
};
83+
```
84+
85+
### **Go**
86+
87+
```go
88+
func distinctIntegers(n int) int {
89+
if n == 1 {
90+
return 1
91+
}
92+
return n - 1
93+
}
94+
```
95+
96+
### **...**
97+
98+
```
99+
100+
```
101+
102+
<!-- tabs:end -->
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution {
2+
public:
3+
int distinctIntegers(int n) {
4+
return max(1, n - 1);
5+
}
6+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
func distinctIntegers(n int) int {
2+
if n == 1 {
3+
return 1
4+
}
5+
return n - 1
6+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution {
2+
public int distinctIntegers(int n) {
3+
return Math.max(1, n - 1);
4+
}
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def distinctIntegers(self, n: int) -> int:
3+
return max(1, n - 1)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /