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 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); |
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 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>(); |
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 { | |
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; |
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 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]); |
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 { | |
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; |
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 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') |
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 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; | |
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 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]); |
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 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; |
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 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); | |