This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public bool EquationsPossible(string[] equations) { | |
var uf = new UnionFind(26); | |
static (int x, int y, bool isEqual) Parse(in string equation) => | |
(equation[0] - 'a', equation[3] - 'a', equation[1] == '='); | |
foreach(var equation in equations){ | |
var (x, y, isEqual) = Parse(equation); | |
if(!isEqual) | |
continue; | |
if(x == y && !isEqual) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyCircularQueue { | |
private int[] _elements; | |
private int _start = -1; | |
private int _end = -1; | |
private int _capacity = 0; | |
private int _count = 0; | |
public MyCircularQueue(int k) { | |
_elements = new int[k]; | |
_capacity = k; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
const int Mod = 1_000_000_007; | |
public int ConcatenatedBinary(int n) { | |
long converted = 0; | |
var msbPosition = 0; | |
for(var i = 1; i <= n; ++i){ | |
if((i & (i - 1)) == 0) | |
msbPosition++; | |
converted = ((converted << msbPosition) | i) % Mod; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public int CoinChange(int[] coins, int amount){ | |
var dp = new int[amount + 1]; | |
Array.Fill(dp, int.MaxValue); | |
dp[0] = 0; | |
foreach(var coin in coins){ | |
for(var change = 1; change <= amount; change++){ | |
if(coin > change || dp[change - coin] == int.MaxValue) | |
continue; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public string ReverseWords(string s) { | |
var chars = s.ToCharArray(); | |
void Reverse(char[] word, int left, int right){ | |
for(; left < right; ++left, --right) | |
(word[left], word[right]) = (word[right], word[left]); | |
} | |
int left = 0, right = 0; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public int[] SumEvenAfterQueries(int[] nums, int[][] queries) { | |
var res = new int[nums.Length]; | |
var evenSum = 0; | |
for(var j = 0; j < nums.Length; ++j){ | |
res[j] = nums[j]; | |
if(nums[j] % 2 == 0) | |
evenSum += nums[j]; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
const char Separator = ' '; | |
const char StartContentCharacter = '('; | |
const char EndContentCharacter = ')'; | |
public IList<IList<string>> FindDuplicate(string[] paths) { | |
// [full file path, content] | |
static string GetFileName(in string fileAndContent) => | |
fileAndContent[0..(fileAndContent.IndexOf(StartContentCharacter))]; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public int FindLength(int[] nums1, int[] nums2) { | |
if(nums2.Length > nums1.Length) | |
(nums1, nums2) = (nums2, nums1); | |
var root = CreateTrie(nums1); | |
var maxLength = 0; | |
for(var i = 0; i < nums2.Length; ++i){ | |
var current = root; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public int LengthOfLongestSubstringTwoDistinct(string s) { | |
var i = 0; | |
var longest = 0; | |
var n= s.Length; | |
while(i < n){ | |
var start = i; | |
var firstDistinct = s[i]; | |
while(i + 1 < n && s[i + 1] == firstDistinct) | |
i++; |