Skip to content

Commit 8034524

Browse files
authored
Merge pull request #290 from hellomrsun/master
Add October algos
2 parents 56e1745 + e9eec6c commit 8034524

File tree

14 files changed

+467
-0
lines changed

14 files changed

+467
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
3+
namespace _188_Best_Time_to_Buy_and_Sell_Stock_IV
4+
{
5+
public class Solution
6+
{
7+
public int MaxProfit(int k, int[] prices)
8+
{
9+
if (prices == null || prices.Length <= 1) return 0;
10+
11+
int len = prices.Length;
12+
13+
//You can make as many as operations on array prices
14+
if (k >= len / 2)
15+
{
16+
int max = 0;
17+
for (int i = 1; i < len; i++)
18+
{
19+
if (prices[i] > prices[i - 1])
20+
{
21+
max += prices[i] - prices[i - 1];
22+
}
23+
}
24+
return max;
25+
}
26+
27+
int[,] dp = new int[k + 1, len];
28+
29+
for (int i = 1; i <= k; i++)
30+
{
31+
int localMax = dp[i - 1, 0] - prices[0];
32+
for (int j = 1; j < len; j++)
33+
{
34+
dp[i, j] = Math.Max(dp[i, j - 1], prices[j] + localMax);
35+
localMax = Math.Max(localMax, dp[i - 1, j] - prices[j]);
36+
}
37+
}
38+
return dp[k, len - 1];
39+
}
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
3+
namespace _1007_Minimum_Domino_Rotations_For_Equal_Row
4+
{
5+
public class Solution
6+
{
7+
public int MinDominoRotations(int[] A, int[] B)
8+
{
9+
int[] countA = new int[7], countB = new int[7], same = new int[7];
10+
int len = A.Length;
11+
12+
//Count the occurence times of each number
13+
for (int i = 0; i < len; i++)
14+
{
15+
countA[A[i]]++;
16+
countB[B[i]]++;
17+
if (A[i] == B[i])
18+
{
19+
same[A[i]]++;
20+
}
21+
}
22+
23+
for (int i = 1; i < 7; i++)
24+
{
25+
if (countA[i] + countB[i] - same[i] == len)
26+
{
27+
return len - Math.Max(countA[i], countB[i]);
28+
}
29+
}
30+
31+
return -1;
32+
}
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Collections;
4+
5+
namespace _133_Clone_Graph
6+
{
7+
public class Solution
8+
{
9+
Dictionary<Node, Node> map = new Dictionary<Node, Node>();
10+
public Node CloneGraph(Node node)
11+
{
12+
if (node == null) return null;
13+
if (!map.ContainsKey(node))
14+
{
15+
map.Add(node, new Node(node.val));
16+
foreach (var nei in node.neighbors)
17+
{
18+
map[node].neighbors.Add(CloneGraph(nei));
19+
}
20+
}
21+
return map[node];
22+
}
23+
}
24+
25+
public class Node
26+
{
27+
public int val;
28+
public IList<Node> neighbors;
29+
30+
public Node()
31+
{
32+
val = 0;
33+
neighbors = new List<Node>();
34+
}
35+
36+
public Node(int _val)
37+
{
38+
val = _val;
39+
neighbors = new List<Node>();
40+
}
41+
42+
public Node(int _val, List<Node> _neighbors)
43+
{
44+
val = _val;
45+
neighbors = _neighbors;
46+
}
47+
}
48+
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Collections;
4+
5+
namespace _735_Asteroid_Collision
6+
{
7+
public class Solution
8+
{
9+
public int[] AsteroidCollision(int[] asteroids)
10+
{
11+
var stack = new Stack<int>();
12+
13+
for (int i = 0; i < asteroids.Length; i++)
14+
{
15+
if (asteroids[i] > 0)
16+
{
17+
//If element is positive, just push it into the stack
18+
stack.Push(asteroids[i]);
19+
}
20+
//element is negative
21+
else
22+
{
23+
//While the last element is positive, and it is smaller than absolute value of current element, then Pop the last element in the stack
24+
while (stack.Count > 0 && stack.Peek() > 0 && stack.Peek() < Math.Abs(asteroids[i]))
25+
{
26+
stack.Pop();
27+
}
28+
29+
//When the stack is empty or the last element is negative
30+
if (stack.Count == 0 || stack.Peek() < 0)
31+
{
32+
stack.Push(asteroids[i]);
33+
}
34+
//When the last element + current element equals 0
35+
else if (asteroids[i] + stack.Peek() == 0)
36+
{
37+
stack.Pop();
38+
}
39+
}
40+
}
41+
42+
var res = new int[stack.Count];
43+
for (int i = stack.Count - 1; i >= 0; i--)
44+
{
45+
res[i] = stack.Pop();
46+
}
47+
48+
return res;
49+
}
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using System;
4+
5+
namespace _111_Minimum_Depth_of_Binary_Tree
6+
{
7+
public class Solution
8+
{
9+
public int MinDepth(TreeNode root)
10+
{
11+
if (root == null) return 0;
12+
var queue = new Queue<TreeNode>();
13+
queue.Enqueue(root);
14+
int layer = 1;
15+
while (queue.Count > 0)
16+
{
17+
int size = queue.Count;
18+
//each tree layer
19+
for (int i = 0; i < size; i++)
20+
{
21+
var node = queue.Dequeue();
22+
if (node != null)
23+
{
24+
if (node.left == null && node.right == null) return layer;
25+
if (node.left != null) queue.Enqueue(node.left);
26+
if (node.right != null) queue.Enqueue(node.right);
27+
}
28+
}
29+
layer++;
30+
}
31+
32+
return layer;
33+
}
34+
}
35+
36+
public class TreeNode
37+
{
38+
public int val;
39+
public TreeNode left;
40+
public TreeNode right;
41+
public TreeNode(int val = 0, TreeNode left = null, TreeNode right = null)
42+
{
43+
this.val = val;
44+
this.left = left;
45+
this.right = right;
46+
}
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
4+
namespace _456_132_Pattern
5+
{
6+
public class Solution
7+
{
8+
public bool Find132pattern(int[] nums)
9+
{
10+
int n3 = int.MinValue;
11+
var stack = new Stack<int>();
12+
//Loop from the end of array
13+
for (int i = nums.Length - 1; i >= 0; i--)
14+
{
15+
if (nums[i] < n3) return true;
16+
else
17+
{
18+
//Pop all elements smaller than nums[i] from stack
19+
while (stack.Count > 0 && nums[i] > stack.Peek())
20+
{
21+
n3 = stack.Pop();
22+
}
23+
}
24+
//Push current element
25+
stack.Push(nums[i]);
26+
}
27+
return false;
28+
}
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
public class Solution {
2+
public int BagOfTokensScore(int[] tokens, int P) {
3+
Array.Sort(tokens);
4+
int lo=0, hi=tokens.Length-1, res=0, points=0;
5+
while(lo = hi){
6+
if(P = tokens[lo]){
7+
P -= tokens[lo];
8+
res = Math.Max(res, ++points);
9+
lo++;
10+
}
11+
else if(points0){
12+
points--;
13+
P += tokens[hi];
14+
hi--;
15+
}
16+
else{
17+
break;
18+
}
19+
}
20+
return res;
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace _1510_Stone_Game_IV
2+
{
3+
public class Solution
4+
{
5+
public bool WinnerSquareGame(int n)
6+
{
7+
bool[] dp = new bool[n+1];
8+
for(int i=1; i<=n; i++){
9+
for(int j=1; j*j<=i; j++){
10+
if(!dp[i -j*j]){
11+
dp[i] = true;
12+
break;
13+
}
14+
}
15+
}
16+
return dp[n];
17+
}
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
3+
namespace _799_Champgane_Tower
4+
{
5+
public class Solution
6+
{
7+
public double ChampagneTower(int poured, int query_row, int query_glass)
8+
{
9+
var res = new double[query_row + 2];
10+
res[0] = poured;
11+
12+
for (int row = 1; row <= query_row; row++)
13+
{
14+
for (int i = row; i >= 0; i--)
15+
{
16+
res[i] = Math.Max(0.0, (res[i] - 1) / 2);
17+
res[i + 1] += res[i];
18+
}
19+
}
20+
21+
return Math.Min(res[query_glass], 1.0);
22+
}
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
namespace _142_Linked_List_Cycle_II
2+
{
3+
public class Solution
4+
{
5+
public ListNode DetectCycle(ListNode head)
6+
{
7+
var fast = head;
8+
var slow = head;
9+
while (fast != null && fast.next != null)
10+
{
11+
fast = fast.next.next;
12+
slow = slow.next;
13+
if (fast == slow)
14+
{
15+
var slow2 = head;
16+
while (slow != slow2)
17+
{
18+
slow = slow.next;
19+
slow2 = slow2.next;
20+
}
21+
return slow;
22+
}
23+
}
24+
return null;
25+
}
26+
}
27+
28+
/* Definition for singly-linked list.*/
29+
public class ListNode
30+
{
31+
public int val;
32+
public ListNode next;
33+
public ListNode(int x)
34+
{
35+
val = x;
36+
next = null;
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)