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 / TopKFrequent.cs
Created October 19, 2022 08:23
692. Top K Frequent Words
public class Solution {
public IList<string> TopKFrequent(string[] words, int k) {
var freq = words.Freq();
var sortedList = new SortedList<int, SortedSet<string>>();
foreach(var kv in freq){
var key = -kv.Value;
if(!sortedList.ContainsKey(key))
sortedList.Add(key, new());
sortedList[key].Add(kv.Key);
@dluciano
dluciano / AddOneRow.cs
Created October 5, 2022 10:31
623. Add One Row to Tree
public class Solution {
public TreeNode AddOneRow(TreeNode root, int val, int depth) {
if(depth == 1){
var tempLeft = root;
root = new TreeNode(val);
root.left = tempLeft;
return root;
}
var queue = new Queue<TreeNode>();
@dluciano
dluciano / HasPathSum.cs
Created October 4, 2022 08:14
112. Path Sum
public class Solution {
private int acc;
public bool HasPathSum(TreeNode root, int targetSum) {
if(root is null)
return false;
acc += root.val;
if(root.left is null && root.right is null && acc == targetSum)
return true;
if(HasPathSum(root.left, targetSum))
return true;
@dluciano
dluciano / MinCost.cs
Created October 3, 2022 10:50
1578. Minimum Time to Make Rope Colorful
public class Solution {
public int MinCost(string colors, int[] neededTime) {
var min = 0;
(int, PriorityQueue<int, int>?) GetEqualColourBallons(int index){
var pq = new PriorityQueue<int, int>();
if(index + 1 >= colors.Length || colors[index] != colors[index + 1])
return (index + 1, null);
var colour = colors[index];
while(index < colors.Length && colors[index] == colour){
pq.Enqueue(neededTime[index], neededTime[index]);
@dluciano
dluciano / NumRollsToTarget.cs
Created October 2, 2022 19:24
1155. Number of Dice Rolls With Target Sum
public class Solution {
private const long MOD = 1000000007;
public int NumRollsToTarget(int dices, int faces, int target) {
var dp = new long[dices + 1, target + 1];
dp[0,0] = 1;
for(var dice = 1; dice <= dices; ++dice){
for(var acc = 0; acc <= target; ++acc){
for(var face = 1; face <= faces; ++face){
if(acc < face)
break;
@dluciano
dluciano / FindLonelyPixel.cs
Created October 1, 2022 08:37
531. Lonely Pixel I
public class Solution {
public int FindLonelyPixel(char[][] picture) {
var ROWS = picture.Length;
var COLS = picture[0].Length;
var lonelyColumns = new BitArray(COLS);
for(var col = 0; col < COLS; ++col){
var lonelyCounter = 0;
for(var row = 0; row < ROWS; ++row){
if(picture[row][col] == 'B')
@dluciano
dluciano / NumDecodings.cs
Created October 1, 2022 08:04
91. Decode Ways
public class Solution {
public int NumDecodings(string s) {
var memo = new int?[s.Length];
int Counter(in int index = 0){
if(index >= s.Length)
return 1;
if(s[index] == '0')
return 0;
@dluciano
dluciano / KillProcess.cs
Created September 30, 2022 08:37
582. Kill Process
public class Solution {
public IList<int> KillProcess(IList<int> pid, IList<int> ppid, int kill) {
var adj = new Dictionary<int, List<int>>();
for(var i = 0; i < pid.Count; i++){
if(!adj.ContainsKey(pid[i]))
adj[pid[i]] = new();
if(!adj.ContainsKey(ppid[i]))
adj[ppid[i]] = new();
adj[ppid[i]].Add(pid[i]);
@dluciano
dluciano / GetSkyline.cs
Created September 30, 2022 07:19
218. The Skyline Problem
public class Solution {
public IList<IList<int>> GetSkyline(int[][] buildings) {
var result = new List<IList<int>>();
var buildingSortedByHeights = buildings.OrderByDescending(b => b[2]).ToArray();
var x1s = buildings.Select(b => b[0]).ToArray();
var x2s = buildings.Select(b => b[1]).ToArray();
var xs = x1s.Concat(x2s).Distinct().OrderBy(x => x).ToArray();
var curHeight = int.MinValue;
@dluciano
dluciano / RemoveNthFromEnd.cs
Created September 28, 2022 09:55
19. Remove Nth Node From End of List
public class Solution {
public ListNode RemoveNthFromEnd(ListNode head, int n) {
ListNode slow = head, fast = head;
for(var i = 0; i < n; i++)
fast = fast.next;
if(fast == null)
return head.next;
while(fast.next != null)
(fast, slow) = (fast.next, slow.next);