Skip to content

Add C# solutions #282

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 3 commits into from
Sep 12, 2020
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ Solutions in various programming languages are provided. Enjoy it.
7. [Word Pattern](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/07-Word-Pattern)
8. [Sum of Root To Leaf Binary Numbers](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/08-Sum-of-Root-To-Leaf-Binary-Numbers)
9. [Compare Version Numbers](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/09-Compare-Version-Numbers)
10. [Bulls and Cows](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/10-Bulls-and-Cows)
11. [Maximum Product Subarray](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/11-Maximum-Product-Subarray)
12. [Combination Sum III](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/12-Combination-Sum-III)

## August LeetCoding Challenge
Click [here](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/) for problem descriptions.
Expand Down
20 changes: 20 additions & 0 deletions September-LeetCoding-Challenge/10-Bulls-and-Cows/Bulls-and-Cows.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
public class Solution {
public string GetHint(string secret, string guess) {
var bulls = 0;
var cows = 0;
int[] secretarr = new int[10];
int[] guessarr = new int[10];
for(int i=0; i<secret.Length; i++){
if(secret[i] == guess[i]){
bulls++;
}else {
++secretarr[secret[i] - '0'];
++guessarr[guess[i] - '0'];
}
}
for (int i = 0; i < 10; ++i) {
cows += Math.Min(secretarr[i], guessarr[i]);
}
return bulls + "A" + cows + "B";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
public class Solution {
public int MaxProduct(int[] nums)
{
int s = nums[0];
int r = nums[0];

for(int i=1, max = s, min = s; i<nums.Length; i++){
int temp = max;
max = Math.Max(Math.Max(nums[i] * max, nums[i] * min), nums[i]);
min = Math.Min(Math.Min(nums[i] * temp, nums[i] * min), nums[i]);

r = Math.Max(max, r);
}

return r;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
public class Solution {
public IList<IList<int>> CombinationSum3(int k, int n)
{
var res = new List<IList<int>>();
var tmp = new List<int>();
Backtrack(res, tmp, k, n, 1);
return res;
}

public void Backtrack(List<IList<int>> res, List<int> tmp, int k, int n, int idx)
{
if (k == tmp.Count && n == 0)
{
res.Add(new List<int>(tmp)); //Deep clone
return;
}

for (int i = idx; i <= n && k > 0 && n > 0; i++)
{
if(i>9) break;
tmp.Add(i);
Backtrack(res, tmp, k, n - i, i + 1);
tmp.RemoveAt(tmp.Count - 1); //Remove last item when it doesn't find the correct combo
}
}
}