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 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]; |
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[] 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) |
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 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; |
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 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++; |
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 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 | |
}; |
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 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); |
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 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++){ |
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 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++) { |
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 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) { |
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 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) { |