Skip to content

feat: add solutions to lc problems: No.2917~2919 #2386

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions solution/2900-2999/2917.Find the K-or of an Array/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,24 @@ function findKOr(nums: number[], k: number): number {
}
```

```cs
public class Solution {
public int FindKOr(int[] nums, int k) {
int ans = 0;
for (int i = 0; i < 32; ++i) {
int cnt = 0;
foreach (int x in nums) {
cnt += (x >> i & 1);
}
if (cnt >= k) {
ans |= 1 << i;
}
}
return ans;
}
}
```

<!-- tabs:end -->

<!-- end -->
18 changes: 18 additions & 0 deletions solution/2900-2999/2917.Find the K-or of an Array/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,24 @@ function findKOr(nums: number[], k: number): number {
}
```

```cs
public class Solution {
public int FindKOr(int[] nums, int k) {
int ans = 0;
for (int i = 0; i < 32; ++i) {
int cnt = 0;
foreach (int x in nums) {
cnt += (x >> i & 1);
}
if (cnt >= k) {
ans |= 1 << i;
}
}
return ans;
}
}
```

<!-- tabs:end -->

<!-- end -->
15 changes: 15 additions & 0 deletions solution/2900-2999/2917.Find the K-or of an Array/Solution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
public class Solution {
public int FindKOr(int[] nums, int k) {
int ans = 0;
for (int i = 0; i < 32; ++i) {
int cnt = 0;
foreach (int x in nums) {
cnt += (x >> i & 1);
}
if (cnt >= k) {
ans |= 1 << i;
}
}
return ans;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,29 @@ function minSum(nums1: number[], nums2: number[]): number {
}
```

```cs
public class Solution {
public long MinSum(int[] nums1, int[] nums2) {
long s1 = 0, s2 = 0;
bool hasZero = false;
foreach (int x in nums1) {
hasZero |= x == 0;
s1 += Math.Max(x, 1);
}
foreach (int x in nums2) {
s2 += Math.Max(x, 1);
}
if (s1 > s2) {
return MinSum(nums2, nums1);
}
if (s1 == s2) {
return s1;
}
return hasZero ? s2 : -1;
}
}
```

<!-- tabs:end -->

<!-- end -->
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,29 @@ function minSum(nums1: number[], nums2: number[]): number {
}
```

```cs
public class Solution {
public long MinSum(int[] nums1, int[] nums2) {
long s1 = 0, s2 = 0;
bool hasZero = false;
foreach (int x in nums1) {
hasZero |= x == 0;
s1 += Math.Max(x, 1);
}
foreach (int x in nums2) {
s2 += Math.Max(x, 1);
}
if (s1 > s2) {
return MinSum(nums2, nums1);
}
if (s1 == s2) {
return s1;
}
return hasZero ? s2 : -1;
}
}
```

<!-- tabs:end -->

<!-- end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
public class Solution {
public long MinSum(int[] nums1, int[] nums2) {
long s1 = 0, s2 = 0;
bool hasZero = false;
foreach (int x in nums1) {
hasZero |= x == 0;
s1 += Math.Max(x, 1);
}
foreach (int x in nums2) {
s2 += Math.Max(x, 1);
}
if (s1 > s2) {
return MinSum(nums2, nums1);
}
if (s1 == s2) {
return s1;
}
return hasZero ? s2 : -1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,21 @@ function minIncrementOperations(nums: number[], k: number): number {
}
```

```cs
public class Solution {
public long MinIncrementOperations(int[] nums, int k) {
long f = 0, g = 0, h = 0;
foreach (int x in nums) {
long hh = Math.Min(Math.Min(f, g), h) + Math.Max(k - x, 0);
f = g;
g = h;
h = hh;
}
return Math.Min(Math.Min(f, g), h);
}
}
```

<!-- tabs:end -->

<!-- end -->
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,21 @@ function minIncrementOperations(nums: number[], k: number): number {
}
```

```cs
public class Solution {
public long MinIncrementOperations(int[] nums, int k) {
long f = 0, g = 0, h = 0;
foreach (int x in nums) {
long hh = Math.Min(Math.Min(f, g), h) + Math.Max(k - x, 0);
f = g;
g = h;
h = hh;
}
return Math.Min(Math.Min(f, g), h);
}
}
```

<!-- tabs:end -->

<!-- end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
public class Solution {
public long MinIncrementOperations(int[] nums, int k) {
long f = 0, g = 0, h = 0;
foreach (int x in nums) {
long hh = Math.Min(Math.Min(f, g), h) + Math.Max(k - x, 0);
f = g;
g = h;
h = hh;
}
return Math.Min(Math.Min(f, g), h);
}
}