Skip to content

Commit f186cb5

Browse files
authored
feat: add solutions for lc No.3857~3859 (#5058)
1 parent 279dc11 commit f186cb5

21 files changed

Lines changed: 992 additions & 20 deletions

File tree

solution/3800-3899/3857.Minimum Cost to Split into Ones/README.md

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,32 +135,59 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3800-3899/3857.Mi
135135

136136
<!-- solution:start -->
137137

138-
### 方法一
138+
### 方法一:数学
139+
140+
要使得代价最小,我们首先要将 $n$ 拆分成 $1$ 和 $n-1$,代价为 $1 \cdot (n-1) = n-1$。接下来,我们将 $n-1$ 拆分成 $1$ 和 $n-2$,代价为 $1 \cdot (n-2) = n-2$。
141+
142+
继续这个过程,直到我们将 $2$ 拆分成 $1$ 和 $1$,代价为 $1 \cdot 1 = 1$。因此,总代价为 $(n-1) + (n-2) + \ldots + 2 + 1 = \frac{n(n-1)}{2}$。
143+
144+
时间复杂度 $O(1)$,空间复杂度 $O(1)$。
139145

140146
<!-- tabs:start -->
141147

142148
#### Python3
143149

144150
```python
145-
151+
class Solution:
152+
def minCost(self, n: int) -> int:
153+
return n * (n - 1) // 2
146154
```
147155

148156
#### Java
149157

150158
```java
151-
159+
class Solution {
160+
public int minCost(int n) {
161+
return n * (n - 1) / 2;
162+
}
163+
}
152164
```
153165

154166
#### C++
155167

156168
```cpp
157-
169+
class Solution {
170+
public:
171+
int minCost(int n) {
172+
return n * (n - 1) / 2;
173+
}
174+
};
158175
```
159176
160177
#### Go
161178
162179
```go
180+
func minCost(n int) int {
181+
return n * (n - 1) / 2
182+
}
183+
```
184+
185+
#### TypeScript
163186

187+
```ts
188+
function minCost(n: number): number {
189+
return (n * (n - 1)) >> 1;
190+
}
164191
```
165192

166193
<!-- tabs:end -->

solution/3800-3899/3857.Minimum Cost to Split into Ones/README_EN.md

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,32 +132,59 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3800-3899/3857.Mi
132132

133133
<!-- solution:start -->
134134

135-
### Solution 1
135+
### Solution 1: Math
136+
137+
To minimize the total cost, we first split $n$ into $1$ and $n-1$, with a cost of $1 \cdot (n-1) = n-1$. Next, we split $n-1$ into $1$ and $n-2$, with a cost of $1 \cdot (n-2) = n-2$.
138+
139+
We continue this process until we split $2$ into $1$ and $1$, with a cost of $1 \cdot 1 = 1$. Therefore, the total cost is $(n-1) + (n-2) + \ldots + 2 + 1 = \frac{n(n-1)}{2}$.
140+
141+
The time complexity is $O(1)$, and the space complexity is $O(1)$.
136142

137143
<!-- tabs:start -->
138144

139145
#### Python3
140146

141147
```python
142-
148+
class Solution:
149+
def minCost(self, n: int) -> int:
150+
return n * (n - 1) // 2
143151
```
144152

145153
#### Java
146154

147155
```java
148-
156+
class Solution {
157+
public int minCost(int n) {
158+
return n * (n - 1) / 2;
159+
}
160+
}
149161
```
150162

151163
#### C++
152164

153165
```cpp
154-
166+
class Solution {
167+
public:
168+
int minCost(int n) {
169+
return n * (n - 1) / 2;
170+
}
171+
};
155172
```
156173
157174
#### Go
158175
159176
```go
177+
func minCost(n int) int {
178+
return n * (n - 1) / 2
179+
}
180+
```
181+
182+
#### TypeScript
160183

184+
```ts
185+
function minCost(n: number): number {
186+
return (n * (n - 1)) >> 1;
187+
}
161188
```
162189

163190
<!-- 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 minCost(int n) {
4+
return n * (n - 1) / 2;
5+
}
6+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
func minCost(n int) int {
2+
return n * (n - 1) / 2
3+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution {
2+
public int minCost(int n) {
3+
return n * (n - 1) / 2;
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 minCost(self, n: int) -> int:
3+
return n * (n - 1) // 2
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function minCost(n: number): number {
2+
return (n * (n - 1)) >> 1;
3+
}

solution/3800-3899/3858.Minimum Bitwise OR From Grid/README.md

Lines changed: 140 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,25 +91,162 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3800-3899/3858.Mi
9191
#### Python3
9292

9393
```python
94-
94+
class Solution:
95+
def minimumOR(self, grid: List[List[int]]) -> int:
96+
mx = max(map(max, grid))
97+
m = mx.bit_length()
98+
ans = 0
99+
for i in range(m - 1, -1, -1):
100+
mask = ans | ((1 << i) - 1)
101+
for row in grid:
102+
found = False
103+
for x in row:
104+
if (x | mask) == mask:
105+
found = True
106+
break
107+
if not found:
108+
ans |= 1 << i
109+
break
110+
return ans
95111
```
96112

97113
#### Java
98114

99115
```java
100-
116+
class Solution {
117+
public int minimumOR(int[][] grid) {
118+
int mx = 0;
119+
for (int[] row : grid) {
120+
for (int x : row) {
121+
mx = Math.max(mx, x);
122+
}
123+
}
124+
125+
int m = 32 - Integer.numberOfLeadingZeros(mx);
126+
int ans = 0;
127+
128+
for (int i = m - 1; i >= 0; i--) {
129+
int mask = ans | ((1 << i) - 1);
130+
for (int[] row : grid) {
131+
boolean found = false;
132+
for (int x : row) {
133+
if ((x | mask) == mask) {
134+
found = true;
135+
break;
136+
}
137+
}
138+
if (!found) {
139+
ans |= 1 << i;
140+
break;
141+
}
142+
}
143+
}
144+
145+
return ans;
146+
}
147+
}
101148
```
102149

103150
#### C++
104151

105152
```cpp
106-
153+
class Solution {
154+
public:
155+
int minimumOR(vector<vector<int>>& grid) {
156+
int mx = 0;
157+
for (auto& row : grid) {
158+
mx = max(mx, ranges::max(row));
159+
}
160+
161+
int m = 32 - __builtin_clz(mx);
162+
int ans = 0;
163+
164+
for (int i = m - 1; i >= 0; i--) {
165+
int mask = ans | ((1 << i) - 1);
166+
for (auto& row : grid) {
167+
bool found = false;
168+
for (int x : row) {
169+
if ((x | mask) == mask) {
170+
found = true;
171+
break;
172+
}
173+
}
174+
if (!found) {
175+
ans |= 1 << i;
176+
break;
177+
}
178+
}
179+
}
180+
181+
return ans;
182+
}
183+
};
107184
```
108185

109186
#### Go
110187

111188
```go
189+
func minimumOR(grid [][]int) int {
190+
mx := 0
191+
for _, row := range grid {
192+
mx = max(mx, slices.Max(row))
193+
}
194+
195+
m := bits.Len(uint(mx))
196+
ans := 0
197+
198+
for i := m - 1; i >= 0; i-- {
199+
mask := ans | ((1 << i) - 1)
200+
for _, row := range grid {
201+
found := false
202+
for _, x := range row {
203+
if (x | mask) == mask {
204+
found = true
205+
break
206+
}
207+
}
208+
if !found {
209+
ans |= 1 << i
210+
break
211+
}
212+
}
213+
}
214+
215+
return ans
216+
}
217+
```
112218

219+
#### TypeScript
220+
221+
```ts
222+
function minimumOR(grid: number[][]): number {
223+
let mx = 0;
224+
for (const row of grid) {
225+
mx = Math.max(mx, Math.max(...row));
226+
}
227+
228+
const m = mx === 0 ? 0 : 32 - Math.clz32(mx);
229+
let ans = 0;
230+
231+
for (let i = m - 1; i >= 0; i--) {
232+
const mask = ans | ((1 << i) - 1);
233+
for (const row of grid) {
234+
let found = false;
235+
for (const x of row) {
236+
if ((x | mask) === mask) {
237+
found = true;
238+
break;
239+
}
240+
}
241+
if (!found) {
242+
ans |= 1 << i;
243+
break;
244+
}
245+
}
246+
}
247+
248+
return ans;
249+
}
113250
```
114251

115252
<!-- tabs:end -->

0 commit comments

Comments
 (0)