Skip to content

Commit 140201a

Browse files
authored
Merge pull request #1 from AlgoStudyGroup/master
updating my repo
2 parents 3c10b1c + 87bc969 commit 140201a

File tree

13 files changed

+368
-0
lines changed

13 files changed

+368
-0
lines changed

README.md

+18
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,24 @@
22

33
https://leetcode.com
44

5+
## September LeetCoding Challenge
6+
Click [here](https://leetcode.com/explore/challenge/card/september-leetcoding-challenge) for problem descriptions.
7+
8+
Solutions in various programming languages are provided. Enjoy it.
9+
10+
1. [Largest Time for Given Digits](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/01-Largest-Time-for-Given-Digits)
11+
2. [Contains Duplicate III](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/02-Contains-Duplicate-III)
12+
3. [Repeated Substring Pattern](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/03-Repeated-Substring-Pattern)
13+
4. [Partition Labels](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/04-Partition-Labels)
14+
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)
15+
6. [Image Overlap](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/06-Image-Overlap)
16+
7. [Word Pattern](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/07-Word-Pattern)
17+
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)
18+
9. [Compare Version Numbers](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/09-Compare-Version-Numbers)
19+
10. [Bulls and Cows](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/10-Bulls-and-Cows)
20+
11. [Maximum Product Subarray](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/11-Maximum-Product-Subarray)
21+
12. [Combination Sum III](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/12-Combination-Sum-III)
22+
523
## August LeetCoding Challenge
624
Click [here](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/) for problem descriptions.
725

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
public class Solution {
2+
public string LargestTimeFromDigits(int[] A) {
3+
var l = new Queue<string>();
4+
l.Enqueue("");
5+
6+
for(int n=0; n<A.Length; n++){
7+
for(int size = l.Count; size>0; size--){
8+
var s = l.Dequeue();
9+
for(int i=0; i<=s.Length; i++){
10+
l.Enqueue(s.Substring(0,i) + A[n] + s.Substring(i));
11+
}
12+
}
13+
}
14+
15+
var largest = "";
16+
foreach(var s in l){
17+
var t = s.Substring(0, 2) + ":" + s.Substring(2);
18+
if(t[3] < '6' && t.CompareTo("24:00") <0 && t.CompareTo(largest)>0){
19+
largest = t;
20+
}
21+
}
22+
23+
return largest;
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
public class Solution {
2+
public bool ContainsNearbyAlmostDuplicate(int[] nums, int k, int t) {
3+
if (t < 0) return false;
4+
var d = new Dictionary<long,long>();
5+
long w = (long)t + 1;
6+
for (int i = 0; i < nums.Length; ++i) {
7+
long m = getID(nums[i], w);
8+
if (d.ContainsKey(m))
9+
return true;
10+
if (d.ContainsKey(m - 1) && Math.Abs(nums[i] - d[m - 1]) < w)
11+
return true;
12+
if (d.ContainsKey(m + 1) && Math.Abs(nums[i] - d[m + 1]) < w)
13+
return true;
14+
d.Add(m, (long)nums[i]);
15+
if (i >= k) d.Remove(getID(nums[i - k], w));
16+
}
17+
return false;
18+
}
19+
20+
private long getID(long i, long w) {
21+
return i < 0 ? (i + 1) / w - 1 : i / w;
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
public class Solution {
2+
public bool RepeatedSubstringPattern(string s) {
3+
var len = s.Length;
4+
for (int i = len / 2; i >= 1; i--)
5+
{
6+
// Dividable
7+
if (len % i == 0)
8+
{
9+
int m = len / i;
10+
var sub = s.Substring(0, i);
11+
int j;
12+
13+
//Check sub is repeated in each round, until m rounds.
14+
for (j = 1; j < m; j++)
15+
{
16+
if (!sub.Equals(s.Substring(j * i, i))) break;
17+
}
18+
19+
//If sub is repeated m rounds, then it's a repeated substring
20+
if (j == m)
21+
return true;
22+
}
23+
}
24+
return false;
25+
}
26+
27+
public void Test()
28+
{
29+
var s = "abcdabcdabcd";
30+
var r = RepeatedSubstringPattern(s);
31+
Console.WriteLine(r);
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
public class Solution {
2+
public IList<int> PartitionLabels(string S) {
3+
if(string.IsNullOrEmpty(S)) return null;
4+
5+
int[] largestPositionByValue = new int[26];
6+
for(int i=0; i<S.Length; i++){
7+
largestPositionByValue[S[i] - 'a'] = i;
8+
}
9+
10+
var res = new List<int>();
11+
var start = 0;
12+
var end=0;
13+
for(int i=0; i<S.Length; i++){
14+
end = Math.Max(end, largestPositionByValue[S[i]-'a']);
15+
if(end == i){
16+
res.Add(end-start+1);
17+
start = end + 1;
18+
}
19+
}
20+
return res;
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
public class Solution {
2+
public IList<int> GetAllElements(TreeNode root1, TreeNode root2) {
3+
var l1 = new List<int>();
4+
Traversal(root1, l1);
5+
var l2 = new List<int>();
6+
Traversal(root2, l2);
7+
8+
var res = Merge(l1, l2);
9+
return res;
10+
}
11+
12+
private void Traversal(TreeNode node, List<int> l){
13+
if(node == null) return;
14+
Traversal(node.left, l);
15+
l.Add(node.val);
16+
Traversal(node.right, l);
17+
}
18+
19+
private List<int> Merge(List<int> l1, List<int> l2){
20+
var res = new List<int>();
21+
int i=0, j=0;
22+
while(i<l1.Count && j<l2.Count){
23+
if(l1[i] < l2[j]){
24+
res.Add(l1[i]);
25+
i++;
26+
}else{
27+
res.Add(l2[j]);
28+
j++;
29+
}
30+
}
31+
while(i<l1.Count) {
32+
res.Add(l1[i]);
33+
i++;
34+
}
35+
while(j<l2.Count) {
36+
res.Add(l2[j]);
37+
j++;
38+
}
39+
return res;
40+
}
41+
}
42+
/**
43+
* Definition for a binary tree node.
44+
* public class TreeNode {
45+
* public int val;
46+
* public TreeNode left;
47+
* public TreeNode right;
48+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
49+
* this.val = val;
50+
* this.left = left;
51+
* this.right = right;
52+
* }
53+
* }
54+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
public class Solution {
2+
public int LargestOverlap(int[][] A, int[][] B) {
3+
if(A == null || A.Length ==0 || B==null || B.Length==0) return 0;
4+
5+
int rows = A.Length, cols = A[0].Length;
6+
List<int[]> al = new List<int[]>(), bl = new List<int[]>();
7+
8+
for (int i = 0; i < rows; i++)
9+
{
10+
for (int j = 0; j < cols; j++)
11+
{
12+
if(A[i][j] == 1) al.Add(new int[]{i, j});
13+
if(B[i][j] == 1) bl.Add(new int[]{i, j });
14+
}
15+
}
16+
17+
var dict = new Dictionary<string, int>();
18+
foreach (int[] a in al)
19+
{
20+
foreach (int[] b in bl)
21+
{
22+
var s = (a[0] - b[0]) + " " + (a[1] - b[1]);
23+
if (dict.ContainsKey(s))
24+
{
25+
dict[s]++;
26+
}
27+
else
28+
{
29+
dict.Add(s, 1);
30+
}
31+
}
32+
}
33+
34+
int c = 0;
35+
foreach(var v in dict.Values){
36+
c = Math.Max(c, v);
37+
}
38+
return c;
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
public class Solution {
2+
public bool WordPattern(string pattern, string str) {
3+
var words = str.Split(" ");
4+
5+
if (pattern.Length != words.Length) return false;
6+
7+
var dict = new Dictionary<char, string>();
8+
9+
for (int i = 0; i < words.Length; i++)
10+
{
11+
char c = pattern[i]; //Positionning char
12+
13+
if (dict.ContainsKey(c))
14+
{
15+
if (dict[c] != words[i])
16+
return false;
17+
}
18+
else
19+
{
20+
if (dict.ContainsValue(words[i]))
21+
return false;
22+
23+
dict.Add(c, words[i]);
24+
}
25+
}
26+
27+
return true;
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* public int val;
5+
* public TreeNode left;
6+
* public TreeNode right;
7+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
8+
* this.val = val;
9+
* this.left = left;
10+
* this.right = right;
11+
* }
12+
* }
13+
*/
14+
public class Solution {
15+
public int SumRootToLeaf(TreeNode root) {
16+
if(root == null) return 0;
17+
18+
return Traverse(root, 0);
19+
}
20+
21+
private int Traverse(TreeNode node, int v){
22+
if(node == null) {
23+
return 0;
24+
}
25+
26+
v = v * 2 + node.val;
27+
28+
return node.left == node.right ? v : Traverse(node.left, v) + Traverse(node.right, v);
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
public class Solution {
2+
public int CompareVersion(string version1, string version2) {
3+
var v1parts = version1.Split(".").ToList();
4+
var v2parts = version2.Split(".").ToList();
5+
6+
if(v1parts.Count != v2parts.Count){
7+
while(v1parts.Count < v2parts.Count)
8+
{
9+
v1parts.Add("0");
10+
}
11+
while(v1parts.Count > v2parts.Count)
12+
{
13+
v2parts.Add("0");
14+
}
15+
}
16+
17+
for(int i=0; i<v1parts.Count; i++){
18+
var v1 = int.Parse(v1parts[i]);
19+
var v2 = int.Parse(v2parts[i]);
20+
if(v1 > v2){
21+
return 1;
22+
}else if(v1 == v2){
23+
continue;
24+
}else if(v1 < v2){
25+
return -1;
26+
}
27+
}
28+
29+
return 0;
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
public class Solution {
2+
public string GetHint(string secret, string guess) {
3+
var bulls = 0;
4+
var cows = 0;
5+
int[] secretarr = new int[10];
6+
int[] guessarr = new int[10];
7+
for(int i=0; i<secret.Length; i++){
8+
if(secret[i] == guess[i]){
9+
bulls++;
10+
}else {
11+
++secretarr[secret[i] - '0'];
12+
++guessarr[guess[i] - '0'];
13+
}
14+
}
15+
for (int i = 0; i < 10; ++i) {
16+
cows += Math.Min(secretarr[i], guessarr[i]);
17+
}
18+
return bulls + "A" + cows + "B";
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
public class Solution {
2+
public int MaxProduct(int[] nums)
3+
{
4+
int s = nums[0];
5+
int r = nums[0];
6+
7+
for(int i=1, max = s, min = s; i<nums.Length; i++){
8+
int temp = max;
9+
max = Math.Max(Math.Max(nums[i] * max, nums[i] * min), nums[i]);
10+
min = Math.Min(Math.Min(nums[i] * temp, nums[i] * min), nums[i]);
11+
12+
r = Math.Max(max, r);
13+
}
14+
15+
return r;
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
public class Solution {
2+
public IList<IList<int>> CombinationSum3(int k, int n)
3+
{
4+
var res = new List<IList<int>>();
5+
var tmp = new List<int>();
6+
Backtrack(res, tmp, k, n, 1);
7+
return res;
8+
}
9+
10+
public void Backtrack(List<IList<int>> res, List<int> tmp, int k, int n, int idx)
11+
{
12+
if (k == tmp.Count && n == 0)
13+
{
14+
res.Add(new List<int>(tmp)); //Deep clone
15+
return;
16+
}
17+
18+
for (int i = idx; i <= n && k > 0 && n > 0; i++)
19+
{
20+
if(i>9) break;
21+
tmp.Add(i);
22+
Backtrack(res, tmp, k, n - i, i + 1);
23+
tmp.RemoveAt(tmp.Count - 1); //Remove last item when it doesn't find the correct combo
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)