Skip to content

Instantly share code, notes, and snippets.

View dluciano's full-sized avatar
🎯
Focusing

Dawlin dluciano

🎯
Focusing
  • UK
View GitHub Profile
@dluciano
dluciano / EquationsPossible.cs
Last active September 26, 2022 22:35
990. Satisfiability of Equality Equations
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)
@dluciano
dluciano / MyCircularQueue.cs
Created September 25, 2022 07:21
622. Design Circular Queue
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;
@dluciano
dluciano / PathSum.cs
Created September 24, 2022 09:03
113. Path Sum II
/**
* 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;
@dluciano
dluciano / ConcatenatedBinary.cs
Last active September 23, 2022 20:16
1680. Concatenation of Consecutive Binary Numbers
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;
@dluciano
dluciano / CoinChange.cs
Last active September 22, 2022 19:02
322. Coin Change
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;
@dluciano
dluciano / ReverseWords.cs
Created September 22, 2022 08:35
557. Reverse Words in a String III
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;
@dluciano
dluciano / SumEvenAfterQueries.cs
Last active September 21, 2022 07:21
985. Sum of Even Numbers After Queries
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];
}
@dluciano
dluciano / FindDuplicate.cs
Created September 20, 2022 20:49
609. Find Duplicate File in System
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))];
@dluciano
dluciano / FindLength.cs
Created September 20, 2022 19:44
718. Maximum Length of Repeated Subarray
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;
@dluciano
dluciano / LengthOfLongestSubstringTwoDistinct.cs
Created September 16, 2022 10:03
159. Longest Substring with At Most Two Distinct 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++;