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 3fd87c4

Browse files
committed
feat: update solutions to lc problem: No.0441,2032~2035
No.0441.Arranging Coins No.2032.Two Out of Three No.2034.Stock Price Fluctuation No.2035.Partition Array Into Two Arrays to Minimize Sum Difference
1 parent 939ae94 commit 3fd87c4

File tree

14 files changed

+404
-65
lines changed

14 files changed

+404
-65
lines changed

‎solution/0400-0499/0441.Arranging Coins/README.md‎

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,20 @@ n = 8
3939
因为第四行不完整,所以返回3.
4040
</pre>
4141

42-
4342
## 解法
4443

4544
<!-- 这里可写通用的实现逻辑 -->
4645

46+
1. 数学推导
47+
4748
`(1 + x) * x / 2 <= n`,求解 x。
4849

4950
`(x + 1/2)2 <= 2n + 1/4`,即 `x <= sqrt(2n + 1/4) - 1/2`
5051

5152
由于 2n 可能溢出,故转换为 `x <= sqrt(2) * sqrt(n + 1/8) - 1/2`
5253

54+
2. 二分查找
55+
5356
<!-- tabs:start -->
5457

5558
### **Python3**
@@ -62,6 +65,20 @@ class Solution:
6265
return int(math.sqrt(2) * math.sqrt(n + 0.125) - 0.5)
6366
```
6467

68+
```python
69+
class Solution:
70+
def arrangeCoins(self, n: int) -> int:
71+
left, right = 1, n
72+
while left < right:
73+
mid = (left + right + 1) >> 1
74+
s = ((1 + mid) * mid) >> 1
75+
if n < s:
76+
right = mid - 1
77+
else:
78+
left = mid
79+
return left
80+
```
81+
6582
### **Java**
6683

6784
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -74,6 +91,63 @@ class Solution {
7491
}
7592
```
7693

94+
```java
95+
class Solution {
96+
public int arrangeCoins(int n) {
97+
long left = 1, right = n;
98+
while (left < right) {
99+
long mid = (left + right + 1) >> 1;
100+
long s = ((1 + mid) * mid) >> 1;
101+
if (n < s) {
102+
right = mid - 1;
103+
} else {
104+
left = mid;
105+
}
106+
}
107+
return (int) left;
108+
}
109+
}
110+
```
111+
112+
### **C++**
113+
114+
```cpp
115+
using LL = long;
116+
117+
class Solution {
118+
public:
119+
int arrangeCoins(int n) {
120+
LL left = 1, right = n;
121+
while (left < right)
122+
{
123+
LL mid = left + right + 1 >> 1;
124+
LL s = (1 + mid) * mid >> 1;
125+
if (n < s) right = mid - 1;
126+
else left = mid;
127+
}
128+
return left;
129+
}
130+
};
131+
```
132+
133+
### **Go**
134+
135+
```go
136+
func arrangeCoins(n int) int {
137+
left, right := 1, n
138+
for left < right {
139+
mid := (left + right + 1) >> 1
140+
s := (1 + mid) * mid >> 1
141+
if n < s {
142+
right = mid - 1
143+
} else {
144+
left = mid
145+
}
146+
}
147+
return left
148+
}
149+
```
150+
77151
### **...**
78152

79153
```

‎solution/0400-0499/0441.Arranging Coins/README_EN.md‎

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
<li><code>1 &lt;= n &lt;= 2<sup>31</sup> - 1</code></li>
3333
</ul>
3434

35-
3635
## Solutions
3736

3837
<!-- tabs:start -->
@@ -45,6 +44,20 @@ class Solution:
4544
return int(math.sqrt(2) * math.sqrt(n + 0.125) - 0.5)
4645
```
4746

47+
```python
48+
class Solution:
49+
def arrangeCoins(self, n: int) -> int:
50+
left, right = 1, n
51+
while left < right:
52+
mid = (left + right + 1) >> 1
53+
s = ((1 + mid) * mid) >> 1
54+
if n < s:
55+
right = mid - 1
56+
else:
57+
left = mid
58+
return left
59+
```
60+
4861
### **Java**
4962

5063
```java
@@ -55,6 +68,63 @@ class Solution {
5568
}
5669
```
5770

71+
```java
72+
class Solution {
73+
public int arrangeCoins(int n) {
74+
long left = 1, right = n;
75+
while (left < right) {
76+
long mid = (left + right + 1) >> 1;
77+
long s = ((1 + mid) * mid) >> 1;
78+
if (n < s) {
79+
right = mid - 1;
80+
} else {
81+
left = mid;
82+
}
83+
}
84+
return (int) left;
85+
}
86+
}
87+
```
88+
89+
### **C++**
90+
91+
```cpp
92+
using LL = long;
93+
94+
class Solution {
95+
public:
96+
int arrangeCoins(int n) {
97+
LL left = 1, right = n;
98+
while (left < right)
99+
{
100+
LL mid = left + right + 1 >> 1;
101+
LL s = (1 + mid) * mid >> 1;
102+
if (n < s) right = mid - 1;
103+
else left = mid;
104+
}
105+
return left;
106+
}
107+
};
108+
```
109+
110+
### **Go**
111+
112+
```go
113+
func arrangeCoins(n int) int {
114+
left, right := 1, n
115+
for left < right {
116+
mid := (left + right + 1) >> 1
117+
s := (1 + mid) * mid >> 1
118+
if n < s {
119+
right = mid - 1
120+
} else {
121+
left = mid
122+
}
123+
}
124+
return left
125+
}
126+
```
127+
58128
### **...**
59129

60130
```
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using LL = long;
2+
3+
class Solution {
4+
public:
5+
int arrangeCoins(int n) {
6+
LL left = 1, right = n;
7+
while (left < right)
8+
{
9+
LL mid = left + right + 1 >> 1;
10+
LL s = (1 + mid) * mid >> 1;
11+
if (n < s) right = mid - 1;
12+
else left = mid;
13+
}
14+
return left;
15+
}
16+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
func arrangeCoins(n int) int {
2+
left, right := 1, n
3+
for left < right {
4+
mid := (left + right + 1) >> 1
5+
s := (1 + mid) * mid >> 1
6+
if n < s {
7+
right = mid - 1
8+
} else {
9+
left = mid
10+
}
11+
}
12+
return left
13+
}

‎solution/2000-2099/2032.Two Out of Three/README.md‎

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -74,25 +74,20 @@ class Solution:
7474
```java
7575
class Solution {
7676
public List<Integer> twoOutOfThree(int[] nums1, int[] nums2, int[] nums3) {
77+
int[] s1 = get(nums1), s2 = get(nums2), s3 = get(nums3);
7778
List<Integer> ans = new ArrayList<>();
78-
Set<Integer> s1 = get(nums1);
79-
Set<Integer> s2 = get(nums2);
80-
Set<Integer> s3 = get(nums3);
8179
for (int i = 1; i <= 100; ++i) {
82-
int a = s1.contains(i) ? 1 : 0;
83-
int b = s2.contains(i) ? 1 : 0;
84-
int c = s3.contains(i) ? 1 : 0;
85-
if (a + b + c > 1) {
80+
if (s1[i] + s2[i] + s3[i] > 1) {
8681
ans.add(i);
8782
}
8883
}
8984
return ans;
9085
}
9186

92-
private Set<Integer> get(int[] nums) {
93-
Set<Integer> s = new HashSet<>();
87+
private int[] get(int[] nums) {
88+
int[] s = new int[101];
9489
for (int num : nums) {
95-
s.add(num);
90+
s[num] =1;
9691
}
9792
return s;
9893
}
@@ -108,18 +103,14 @@ public:
108103
auto s1 = get(nums1), s2 = get(nums2), s3 = get(nums3);
109104
vector<int> ans;
110105
for (int i = 1; i <= 100; ++i)
111-
{
112-
int a = s1.count(i) ? 1 : 0;
113-
int b = s2.count(i) ? 1 : 0;
114-
int c = s3.count(i) ? 1 : 0;
115-
if (a + b + c > 1) ans.push_back(i);
116-
}
106+
if (s1[i] + s2[i] + s3[i] > 1)
107+
ans.push_back(i);
117108
return ans;
118109
}
119110

120-
unordered_set<int> get(vector<int>& nums) {
121-
unordered_set<int> s;
122-
for (int num : nums) s.insert(num);
111+
vector<int> get(vector<int>& nums) {
112+
vector<int> s(101);
113+
for (int num : nums) s[num] = 1;
123114
return s;
124115
}
125116
};

‎solution/2000-2099/2032.Two Out of Three/README_EN.md‎

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -67,25 +67,20 @@ class Solution:
6767
```java
6868
class Solution {
6969
public List<Integer> twoOutOfThree(int[] nums1, int[] nums2, int[] nums3) {
70+
int[] s1 = get(nums1), s2 = get(nums2), s3 = get(nums3);
7071
List<Integer> ans = new ArrayList<>();
71-
Set<Integer> s1 = get(nums1);
72-
Set<Integer> s2 = get(nums2);
73-
Set<Integer> s3 = get(nums3);
7472
for (int i = 1; i <= 100; ++i) {
75-
int a = s1.contains(i) ? 1 : 0;
76-
int b = s2.contains(i) ? 1 : 0;
77-
int c = s3.contains(i) ? 1 : 0;
78-
if (a + b + c > 1) {
73+
if (s1[i] + s2[i] + s3[i] > 1) {
7974
ans.add(i);
8075
}
8176
}
8277
return ans;
8378
}
8479

85-
private Set<Integer> get(int[] nums) {
86-
Set<Integer> s = new HashSet<>();
80+
private int[] get(int[] nums) {
81+
int[] s = new int[101];
8782
for (int num : nums) {
88-
s.add(num);
83+
s[num] =1;
8984
}
9085
return s;
9186
}
@@ -101,18 +96,14 @@ public:
10196
auto s1 = get(nums1), s2 = get(nums2), s3 = get(nums3);
10297
vector<int> ans;
10398
for (int i = 1; i <= 100; ++i)
104-
{
105-
int a = s1.count(i) ? 1 : 0;
106-
int b = s2.count(i) ? 1 : 0;
107-
int c = s3.count(i) ? 1 : 0;
108-
if (a + b + c > 1) ans.push_back(i);
109-
}
99+
if (s1[i] + s2[i] + s3[i] > 1)
100+
ans.push_back(i);
110101
return ans;
111102
}
112103

113-
unordered_set<int> get(vector<int>& nums) {
114-
unordered_set<int> s;
115-
for (int num : nums) s.insert(num);
104+
vector<int> get(vector<int>& nums) {
105+
vector<int> s(101);
106+
for (int num : nums) s[num] = 1;
116107
return s;
117108
}
118109
};

‎solution/2000-2099/2032.Two Out of Three/Solution.cpp‎

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,14 @@ class Solution {
44
auto s1 = get(nums1), s2 = get(nums2), s3 = get(nums3);
55
vector<int> ans;
66
for (int i = 1; i <= 100; ++i)
7-
{
8-
int a = s1.count(i) ? 1 : 0;
9-
int b = s2.count(i) ? 1 : 0;
10-
int c = s3.count(i) ? 1 : 0;
11-
if (a + b + c > 1) ans.push_back(i);
12-
}
7+
if (s1[i] + s2[i] + s3[i] > 1)
8+
ans.push_back(i);
139
return ans;
1410
}
1511

16-
unordered_set<int> get(vector<int>& nums) {
17-
unordered_set<int> s;
18-
for (int num : nums) s.insert(num);
12+
vector<int> get(vector<int>& nums) {
13+
vector<int> s(101);
14+
for (int num : nums) s[num] = 1;
1915
return s;
2016
}
2117
};

‎solution/2000-2099/2032.Two Out of Three/Solution.java‎

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
11
class Solution {
22
public List<Integer> twoOutOfThree(int[] nums1, int[] nums2, int[] nums3) {
3+
int[] s1 = get(nums1), s2 = get(nums2), s3 = get(nums3);
34
List<Integer> ans = new ArrayList<>();
4-
Set<Integer> s1 = get(nums1);
5-
Set<Integer> s2 = get(nums2);
6-
Set<Integer> s3 = get(nums3);
75
for (int i = 1; i <= 100; ++i) {
8-
int a = s1.contains(i) ? 1 : 0;
9-
int b = s2.contains(i) ? 1 : 0;
10-
int c = s3.contains(i) ? 1 : 0;
11-
if (a + b + c > 1) {
6+
if (s1[i] + s2[i] + s3[i] > 1) {
127
ans.add(i);
138
}
149
}
1510
return ans;
1611
}
1712

18-
private Set<Integer> get(int[] nums) {
19-
Set<Integer> s = new HashSet<>();
13+
private int[] get(int[] nums) {
14+
int[] s = new int[101];
2015
for (int num : nums) {
21-
s.add(num);
16+
s[num] = 1;
2217
}
2318
return s;
2419
}

0 commit comments

Comments
(0)

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