Skip to content
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

Add October algos #290

Merged
merged 1 commit into from
Nov 22, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;

namespace _188_Best_Time_to_Buy_and_Sell_Stock_IV
{
public class Solution
{
public int MaxProfit(int k, int[] prices)
{
if (prices == null || prices.Length <= 1) return 0;

int len = prices.Length;

//You can make as many as operations on array prices
if (k >= len / 2)
{
int max = 0;
for (int i = 1; i < len; i++)
{
if (prices[i] > prices[i - 1])
{
max += prices[i] - prices[i - 1];
}
}
return max;
}

int[,] dp = new int[k + 1, len];

for (int i = 1; i <= k; i++)
{
int localMax = dp[i - 1, 0] - prices[0];
for (int j = 1; j < len; j++)
{
dp[i, j] = Math.Max(dp[i, j - 1], prices[j] + localMax);
localMax = Math.Max(localMax, dp[i - 1, j] - prices[j]);
}
}
return dp[k, len - 1];
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;

namespace _1007_Minimum_Domino_Rotations_For_Equal_Row
{
public class Solution
{
public int MinDominoRotations(int[] A, int[] B)
{
int[] countA = new int[7], countB = new int[7], same = new int[7];
int len = A.Length;

//Count the occurence times of each number
for (int i = 0; i < len; i++)
{
countA[A[i]]++;
countB[B[i]]++;
if (A[i] == B[i])
{
same[A[i]]++;
}
}

for (int i = 1; i < 7; i++)
{
if (countA[i] + countB[i] - same[i] == len)
{
return len - Math.Max(countA[i], countB[i]);
}
}

return -1;
}
}
}
49 changes: 49 additions & 0 deletions October-LeetCoding-Challenge/20-Clone-Graph/Clone-Graph.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.Collections;

namespace _133_Clone_Graph
{
public class Solution
{
Dictionary<Node, Node> map = new Dictionary<Node, Node>();
public Node CloneGraph(Node node)
{
if (node == null) return null;
if (!map.ContainsKey(node))
{
map.Add(node, new Node(node.val));
foreach (var nei in node.neighbors)
{
map[node].neighbors.Add(CloneGraph(nei));
}
}
return map[node];
}
}

public class Node
{
public int val;
public IList<Node> neighbors;

public Node()
{
val = 0;
neighbors = new List<Node>();
}

public Node(int _val)
{
val = _val;
neighbors = new List<Node>();
}

public Node(int _val, List<Node> _neighbors)
{
val = _val;
neighbors = _neighbors;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Collections;

namespace _735_Asteroid_Collision
{
public class Solution
{
public int[] AsteroidCollision(int[] asteroids)
{
var stack = new Stack<int>();

for (int i = 0; i < asteroids.Length; i++)
{
if (asteroids[i] > 0)
{
//If element is positive, just push it into the stack
stack.Push(asteroids[i]);
}
//element is negative
else
{
//While the last element is positive, and it is smaller than absolute value of current element, then Pop the last element in the stack
while (stack.Count > 0 && stack.Peek() > 0 && stack.Peek() < Math.Abs(asteroids[i]))
{
stack.Pop();
}

//When the stack is empty or the last element is negative
if (stack.Count == 0 || stack.Peek() < 0)
{
stack.Push(asteroids[i]);
}
//When the last element + current element equals 0
else if (asteroids[i] + stack.Peek() == 0)
{
stack.Pop();
}
}
}

var res = new int[stack.Count];
for (int i = stack.Count - 1; i >= 0; i--)
{
res[i] = stack.Pop();
}

return res;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.Collections;
using System.Collections.Generic;
using System;

namespace _111_Minimum_Depth_of_Binary_Tree
{
public class Solution
{
public int MinDepth(TreeNode root)
{
if (root == null) return 0;
var queue = new Queue<TreeNode>();
queue.Enqueue(root);
int layer = 1;
while (queue.Count > 0)
{
int size = queue.Count;
//each tree layer
for (int i = 0; i < size; i++)
{
var node = queue.Dequeue();
if (node != null)
{
if (node.left == null && node.right == null) return layer;
if (node.left != null) queue.Enqueue(node.left);
if (node.right != null) queue.Enqueue(node.right);
}
}
layer++;
}

return layer;
}
}

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;
}
}
}
30 changes: 30 additions & 0 deletions October-LeetCoding-Challenge/23-132-Pattern/132-Pattern.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Collections;
using System.Collections.Generic;

namespace _456_132_Pattern
{
public class Solution
{
public bool Find132pattern(int[] nums)
{
int n3 = int.MinValue;
var stack = new Stack<int>();
//Loop from the end of array
for (int i = nums.Length - 1; i >= 0; i--)
{
if (nums[i] < n3) return true;
else
{
//Pop all elements smaller than nums[i] from stack
while (stack.Count > 0 && nums[i] > stack.Peek())
{
n3 = stack.Pop();
}
}
//Push current element
stack.Push(nums[i]);
}
return false;
}
}
}
22 changes: 22 additions & 0 deletions October-LeetCoding-Challenge/24-Bag-of-Tokens/Bag-of-Tokens.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
public class Solution {
public int BagOfTokensScore(int[] tokens, int P) {
Array.Sort(tokens);
int lo=0, hi=tokens.Length-1, res=0, points=0;
while(lo = hi){
if(P = tokens[lo]){
P -= tokens[lo];
res = Math.Max(res, ++points);
lo++;
}
else if(points0){
points--;
P += tokens[hi];
hi--;
}
else{
break;
}
}
return res;
}
}
19 changes: 19 additions & 0 deletions October-LeetCoding-Challenge/25-Stone-Game-IV/Stone-Game-IV.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace _1510_Stone_Game_IV
{
public class Solution
{
public bool WinnerSquareGame(int n)
{
bool[] dp = new bool[n+1];
for(int i=1; i<=n; i++){
for(int j=1; j*j<=i; j++){
if(!dp[i -j*j]){
dp[i] = true;
break;
}
}
}
return dp[n];
}
}
}
24 changes: 24 additions & 0 deletions October-LeetCoding-Challenge/26-Champagne-Tower/Champgane-Tower.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;

namespace _799_Champgane_Tower
{
public class Solution
{
public double ChampagneTower(int poured, int query_row, int query_glass)
{
var res = new double[query_row + 2];
res[0] = poured;

for (int row = 1; row <= query_row; row++)
{
for (int i = row; i >= 0; i--)
{
res[i] = Math.Max(0.0, (res[i] - 1) / 2);
res[i + 1] += res[i];
}
}

return Math.Min(res[query_glass], 1.0);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
namespace _142_Linked_List_Cycle_II
{
public class Solution
{
public ListNode DetectCycle(ListNode head)
{
var fast = head;
var slow = head;
while (fast != null && fast.next != null)
{
fast = fast.next.next;
slow = slow.next;
if (fast == slow)
{
var slow2 = head;
while (slow != slow2)
{
slow = slow.next;
slow2 = slow2.next;
}
return slow;
}
}
return null;
}
}

/* Definition for singly-linked list.*/
public class ListNode
{
public int val;
public ListNode next;
public ListNode(int x)
{
val = x;
next = null;
}
}
}
Loading