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 / MaximumScore.cs
Created September 16, 2022 09:40
1770. Maximum Score from Performing Multiplication Operations
public class Solution {
public int MaximumScore(int[] nums, int[] multipliers) {
var m = multipliers.Length;
var n = nums.Length;
var dp = new int[m + 1, m + 1];
dp[m, m] = 0;
for(var i = m - 1; i >= 0; i--){
for(var l = i; l >= 0; l--){
var r = n - 1 - (i - l);
var c = multipliers[i];
@dluciano
dluciano / FindOriginalArray.cs
Created September 15, 2022 18:39
2007. Find Original Array From Doubled Array
public class Solution {
public int[] FindOriginalArray(int[] changed) {
if(changed.Length % 2 != 0)
return Array.Empty<int>();
Array.Sort(changed);
var array = new int[100_000 + 1];
foreach(var number in changed)
@dluciano
dluciano / SwimInWater.cs
Created September 14, 2022 16:26
778. Swim in Rising Water
public class Solution {
private readonly int[][] Directions = new int[][]{
new int[]{ -1, 0 },
new int[]{ 0, 1 },
new int[]{ 1, 0 },
new int[]{ 0, -1 },
};
public int SwimInWater(int[][] grid) {
var ROWS = grid.Length;
var COLS = grid[0].Length;
@dluciano
dluciano / PseudoPalindromicPaths.cs
Created September 14, 2022 13:51
1457. Pseudo-Palindromic Paths in a Binary Tree
public class Solution {
public int PseudoPalindromicPaths (TreeNode root) {
var dict = new bool[9];
// false -> even
// true -> odd
bool IsPsuedoPalindromic(){
var oddCount = 0;
foreach(var isOdd in dict){
if(isOdd)
oddCount++;
@dluciano
dluciano / ValidUtf8.cs
Last active September 13, 2022 09:38
393. UTF-8 Validation
public class Solution {
public bool ValidUtf8(int[] data) {
int GetBytesCount(in int number)=>
number switch {
<= 0b01111111 => 1, // 1 byte
>= 0b11000000 and <= 0b11011111 => 2, // 2 bytes
>= 0b11100000 and <= 0b11101111 => 3, // 3 bytes
>= 0b11110000 and <= 0b11110111 => 4, // 4 bytes
_ => -1 // invalid
};
@dluciano
dluciano / BagOfTokensScore.cs
Last active September 12, 2022 23:45
948. Bag of Tokens
public class Solution {
public int BagOfTokensScore(int[] tokens, int power) {
var minPq = new PriorityQueue<int, int>();
var maxPq = new PriorityQueue<int, int>();
var score = 0;
var maxScore = 0;
foreach(var token in tokens){
minPq.Enqueue(token, token);
maxPq.Enqueue(token, -token);
@dluciano
dluciano / EvalRPN.cs
Created September 12, 2022 10:51
150. Evaluate Reverse Polish Notation
public class Solution {
public static readonly Stack<int> s = new Stack<int>();
public static readonly HashSet<string> opers = new HashSet<string>(){
{"+"},
{"-"},
{"*"},
{"/"},
};
public int EvalRPN(string[] tokens) {
for(var i = 0; i < tokens.Length; i++){
@dluciano
dluciano / MaxProfit.cs
Created September 10, 2022 09:56
188. Best Time to Buy and Sell Stock IV
public class Solution {
public int MaxProfit(int k, int[] prices){
if (k == 0) return 0;
var profit = new int[k + 1];
var cost = new int[k + 1];
profit[0] = 0;
Array.Fill(cost, int.MaxValue);
foreach (int price in prices) {
for (int i = 0; i < k; i++) {
@dluciano
dluciano / NumberOfWeakCharacters.cs
Created September 9, 2022 18:40
1996. The Number of Weak Characters in the Game
public class Solution {
private sealed class AttackComparerDesc : IComparer<int> {
public int Compare(int a, int b) => b - a;
}
public int NumberOfWeakCharacters(int[][] properties) {
var sortedList = new SortedList<int, List<int[]>>(new AttackComparerDesc());
var maxAttack = int.MinValue;
foreach(var property in properties) {
@dluciano
dluciano / HitCounter.cs
Created September 8, 2022 09:34
362. Design Hit Counter
public class HitCounter {
private readonly Dictionary<int, int> _dict = new();
public void Hit(int timestamp) {
if(!_dict.ContainsKey(timestamp))
_dict[timestamp] = 0;
++_dict[timestamp];
}
public int GetHits(int timestamp) {