Skip to content

updating my repo #1

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 11 commits into from
Sep 12, 2020
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@

https://leetcode.com

## September LeetCoding Challenge
Click [here](https://leetcode.com/explore/challenge/card/september-leetcoding-challenge) for problem descriptions.

Solutions in various programming languages are provided. Enjoy it.

1. [Largest Time for Given Digits](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/01-Largest-Time-for-Given-Digits)
2. [Contains Duplicate III](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/02-Contains-Duplicate-III)
3. [Repeated Substring Pattern](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/03-Repeated-Substring-Pattern)
4. [Partition Labels](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/04-Partition-Labels)
5. [All Elements in Two Binary Search Trees](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/05-All-Elements-in-Two-Binary-Search-Trees)
6. [Image Overlap](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/06-Image-Overlap)
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
public class Solution {
public string LargestTimeFromDigits(int[] A) {
var l = new Queue<string>();
l.Enqueue("");

for(int n=0; n<A.Length; n++){
for(int size = l.Count; size>0; size--){
var s = l.Dequeue();
for(int i=0; i<=s.Length; i++){
l.Enqueue(s.Substring(0,i) + A[n] + s.Substring(i));
}
}
}

var largest = "";
foreach(var s in l){
var t = s.Substring(0, 2) + ":" + s.Substring(2);
if(t[3] < '6' && t.CompareTo("24:00") <0 && t.CompareTo(largest)>0){
largest = t;
}
}

return largest;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
public class Solution {
public bool ContainsNearbyAlmostDuplicate(int[] nums, int k, int t) {
if (t < 0) return false;
var d = new Dictionary<long,long>();
long w = (long)t + 1;
for (int i = 0; i < nums.Length; ++i) {
long m = getID(nums[i], w);
if (d.ContainsKey(m))
return true;
if (d.ContainsKey(m - 1) && Math.Abs(nums[i] - d[m - 1]) < w)
return true;
if (d.ContainsKey(m + 1) && Math.Abs(nums[i] - d[m + 1]) < w)
return true;
d.Add(m, (long)nums[i]);
if (i >= k) d.Remove(getID(nums[i - k], w));
}
return false;
}

private long getID(long i, long w) {
return i < 0 ? (i + 1) / w - 1 : i / w;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
public class Solution {
public bool RepeatedSubstringPattern(string s) {
var len = s.Length;
for (int i = len / 2; i >= 1; i--)
{
// Dividable
if (len % i == 0)
{
int m = len / i;
var sub = s.Substring(0, i);
int j;

//Check sub is repeated in each round, until m rounds.
for (j = 1; j < m; j++)
{
if (!sub.Equals(s.Substring(j * i, i))) break;
}

//If sub is repeated m rounds, then it's a repeated substring
if (j == m)
return true;
}
}
return false;
}

public void Test()
{
var s = "abcdabcdabcd";
var r = RepeatedSubstringPattern(s);
Console.WriteLine(r);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
public class Solution {
public IList<int> PartitionLabels(string S) {
if(string.IsNullOrEmpty(S)) return null;

int[] largestPositionByValue = new int[26];
for(int i=0; i<S.Length; i++){
largestPositionByValue[S[i] - 'a'] = i;
}

var res = new List<int>();
var start = 0;
var end=0;
for(int i=0; i<S.Length; i++){
end = Math.Max(end, largestPositionByValue[S[i]-'a']);
if(end == i){
res.Add(end-start+1);
start = end + 1;
}
}
return res;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
public class Solution {
public IList<int> GetAllElements(TreeNode root1, TreeNode root2) {
var l1 = new List<int>();
Traversal(root1, l1);
var l2 = new List<int>();
Traversal(root2, l2);

var res = Merge(l1, l2);
return res;
}

private void Traversal(TreeNode node, List<int> l){
if(node == null) return;
Traversal(node.left, l);
l.Add(node.val);
Traversal(node.right, l);
}

private List<int> Merge(List<int> l1, List<int> l2){
var res = new List<int>();
int i=0, j=0;
while(i<l1.Count && j<l2.Count){
if(l1[i] < l2[j]){
res.Add(l1[i]);
i++;
}else{
res.Add(l2[j]);
j++;
}
}
while(i<l1.Count) {
res.Add(l1[i]);
i++;
}
while(j<l2.Count) {
res.Add(l2[j]);
j++;
}
return res;
}
}
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
40 changes: 40 additions & 0 deletions September-LeetCoding-Challenge/06-Image-Overlap/Image-Overlap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
public class Solution {
public int LargestOverlap(int[][] A, int[][] B) {
if(A == null || A.Length ==0 || B==null || B.Length==0) return 0;

int rows = A.Length, cols = A[0].Length;
List<int[]> al = new List<int[]>(), bl = new List<int[]>();

for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
if(A[i][j] == 1) al.Add(new int[]{i, j});
if(B[i][j] == 1) bl.Add(new int[]{i, j });
}
}

var dict = new Dictionary<string, int>();
foreach (int[] a in al)
{
foreach (int[] b in bl)
{
var s = (a[0] - b[0]) + " " + (a[1] - b[1]);
if (dict.ContainsKey(s))
{
dict[s]++;
}
else
{
dict.Add(s, 1);
}
}
}

int c = 0;
foreach(var v in dict.Values){
c = Math.Max(c, v);
}
return c;
}
}
29 changes: 29 additions & 0 deletions September-LeetCoding-Challenge/07-Word-Pattern/Word-Pattern.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
public class Solution {
public bool WordPattern(string pattern, string str) {
var words = str.Split(" ");

if (pattern.Length != words.Length) return false;

var dict = new Dictionary<char, string>();

for (int i = 0; i < words.Length; i++)
{
char c = pattern[i]; //Positionning char

if (dict.ContainsKey(c))
{
if (dict[c] != words[i])
return false;
}
else
{
if (dict.ContainsValue(words[i]))
return false;

dict.Add(c, words[i]);
}
}

return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution {
public int SumRootToLeaf(TreeNode root) {
if(root == null) return 0;

return Traverse(root, 0);
}

private int Traverse(TreeNode node, int v){
if(node == null) {
return 0;
}

v = v * 2 + node.val;

return node.left == node.right ? v : Traverse(node.left, v) + Traverse(node.right, v);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
public class Solution {
public int CompareVersion(string version1, string version2) {
var v1parts = version1.Split(".").ToList();
var v2parts = version2.Split(".").ToList();

if(v1parts.Count != v2parts.Count){
while(v1parts.Count < v2parts.Count)
{
v1parts.Add("0");
}
while(v1parts.Count > v2parts.Count)
{
v2parts.Add("0");
}
}

for(int i=0; i<v1parts.Count; i++){
var v1 = int.Parse(v1parts[i]);
var v2 = int.Parse(v2parts[i]);
if(v1 > v2){
return 1;
}else if(v1 == v2){
continue;
}else if(v1 < v2){
return -1;
}
}

return 0;
}
}
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
}
}
}