Skip to content

Commit d773da5

Browse files
authored
feat: add solutions to lc problems: No.2917~2919 (doocs#2386)
1 parent 9203729 commit d773da5

File tree

9 files changed

+159
-0
lines changed

9 files changed

+159
-0
lines changed

solution/2900-2999/2917.Find the K-or of an Array/README.md

+18
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,24 @@ function findKOr(nums: number[], k: number): number {
150150
}
151151
```
152152

153+
```cs
154+
public class Solution {
155+
public int FindKOr(int[] nums, int k) {
156+
int ans = 0;
157+
for (int i = 0; i < 32; ++i) {
158+
int cnt = 0;
159+
foreach (int x in nums) {
160+
cnt += (x >> i & 1);
161+
}
162+
if (cnt >= k) {
163+
ans |= 1 << i;
164+
}
165+
}
166+
return ans;
167+
}
168+
}
169+
```
170+
153171
<!-- tabs:end -->
154172

155173
<!-- end -->

solution/2900-2999/2917.Find the K-or of an Array/README_EN.md

+18
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,24 @@ function findKOr(nums: number[], k: number): number {
147147
}
148148
```
149149

150+
```cs
151+
public class Solution {
152+
public int FindKOr(int[] nums, int k) {
153+
int ans = 0;
154+
for (int i = 0; i < 32; ++i) {
155+
int cnt = 0;
156+
foreach (int x in nums) {
157+
cnt += (x >> i & 1);
158+
}
159+
if (cnt >= k) {
160+
ans |= 1 << i;
161+
}
162+
}
163+
return ans;
164+
}
165+
}
166+
```
167+
150168
<!-- tabs:end -->
151169

152170
<!-- end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
public class Solution {
2+
public int FindKOr(int[] nums, int k) {
3+
int ans = 0;
4+
for (int i = 0; i < 32; ++i) {
5+
int cnt = 0;
6+
foreach (int x in nums) {
7+
cnt += (x >> i & 1);
8+
}
9+
if (cnt >= k) {
10+
ans |= 1 << i;
11+
}
12+
}
13+
return ans;
14+
}
15+
}

solution/2900-2999/2918.Minimum Equal Sum of Two Arrays After Replacing Zeros/README.md

+23
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,29 @@ function minSum(nums1: number[], nums2: number[]): number {
165165
}
166166
```
167167

168+
```cs
169+
public class Solution {
170+
public long MinSum(int[] nums1, int[] nums2) {
171+
long s1 = 0, s2 = 0;
172+
bool hasZero = false;
173+
foreach (int x in nums1) {
174+
hasZero |= x == 0;
175+
s1 += Math.Max(x, 1);
176+
}
177+
foreach (int x in nums2) {
178+
s2 += Math.Max(x, 1);
179+
}
180+
if (s1 > s2) {
181+
return MinSum(nums2, nums1);
182+
}
183+
if (s1 == s2) {
184+
return s1;
185+
}
186+
return hasZero ? s2 : -1;
187+
}
188+
}
189+
```
190+
168191
<!-- tabs:end -->
169192

170193
<!-- end -->

solution/2900-2999/2918.Minimum Equal Sum of Two Arrays After Replacing Zeros/README_EN.md

+23
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,29 @@ function minSum(nums1: number[], nums2: number[]): number {
161161
}
162162
```
163163

164+
```cs
165+
public class Solution {
166+
public long MinSum(int[] nums1, int[] nums2) {
167+
long s1 = 0, s2 = 0;
168+
bool hasZero = false;
169+
foreach (int x in nums1) {
170+
hasZero |= x == 0;
171+
s1 += Math.Max(x, 1);
172+
}
173+
foreach (int x in nums2) {
174+
s2 += Math.Max(x, 1);
175+
}
176+
if (s1 > s2) {
177+
return MinSum(nums2, nums1);
178+
}
179+
if (s1 == s2) {
180+
return s1;
181+
}
182+
return hasZero ? s2 : -1;
183+
}
184+
}
185+
```
186+
164187
<!-- tabs:end -->
165188

166189
<!-- end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
public class Solution {
2+
public long MinSum(int[] nums1, int[] nums2) {
3+
long s1 = 0, s2 = 0;
4+
bool hasZero = false;
5+
foreach (int x in nums1) {
6+
hasZero |= x == 0;
7+
s1 += Math.Max(x, 1);
8+
}
9+
foreach (int x in nums2) {
10+
s2 += Math.Max(x, 1);
11+
}
12+
if (s1 > s2) {
13+
return MinSum(nums2, nums1);
14+
}
15+
if (s1 == s2) {
16+
return s1;
17+
}
18+
return hasZero ? s2 : -1;
19+
}
20+
}

solution/2900-2999/2919.Minimum Increment Operations to Make Array Beautiful/README.md

+15
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,21 @@ function minIncrementOperations(nums: number[], k: number): number {
155155
}
156156
```
157157

158+
```cs
159+
public class Solution {
160+
public long MinIncrementOperations(int[] nums, int k) {
161+
long f = 0, g = 0, h = 0;
162+
foreach (int x in nums) {
163+
long hh = Math.Min(Math.Min(f, g), h) + Math.Max(k - x, 0);
164+
f = g;
165+
g = h;
166+
h = hh;
167+
}
168+
return Math.Min(Math.Min(f, g), h);
169+
}
170+
}
171+
```
172+
158173
<!-- tabs:end -->
159174

160175
<!-- end -->

solution/2900-2999/2919.Minimum Increment Operations to Make Array Beautiful/README_EN.md

+15
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,21 @@ function minIncrementOperations(nums: number[], k: number): number {
151151
}
152152
```
153153

154+
```cs
155+
public class Solution {
156+
public long MinIncrementOperations(int[] nums, int k) {
157+
long f = 0, g = 0, h = 0;
158+
foreach (int x in nums) {
159+
long hh = Math.Min(Math.Min(f, g), h) + Math.Max(k - x, 0);
160+
f = g;
161+
g = h;
162+
h = hh;
163+
}
164+
return Math.Min(Math.Min(f, g), h);
165+
}
166+
}
167+
```
168+
154169
<!-- tabs:end -->
155170

156171
<!-- end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
public class Solution {
2+
public long MinIncrementOperations(int[] nums, int k) {
3+
long f = 0, g = 0, h = 0;
4+
foreach (int x in nums) {
5+
long hh = Math.Min(Math.Min(f, g), h) + Math.Max(k - x, 0);
6+
f = g;
7+
g = h;
8+
h = hh;
9+
}
10+
return Math.Min(Math.Min(f, g), h);
11+
}
12+
}

0 commit comments

Comments
 (0)