Skip to content

Instantly share code, notes, and snippets.

@svaza
svaza / Solution.cs
Created August 14, 2022 04:11
2374. Node With Highest Edge Score
public class Solution {
public int EdgeScore(int[] edges) {
long[] deg = new long[edges.Length];
long max = -1;
for(int i = 0; i < edges.Length; i++)
{
deg[edges[i]]+=i;
max = Math.Max(max, deg[edges[i]]);
}
@svaza
svaza / Solution.cs
Created August 7, 2022 19:09
aritmmetic triplets
public class Solution {
public int ArithmeticTriplets(int[] nums, int diff) {
var set = new HashSet<string>();
int i = 0;
int n = nums.Length-1;
int c = 0;
while(i <= n-2)
{
@svaza
svaza / Solution.cs
Created August 7, 2022 18:18
The Latest Time to Catch a Bus
public class Solution {
public int LatestTimeCatchTheBus(int[] buses, int[] passengers, int capacity) {
PriorityQueue<int, int> qbus = new();
PriorityQueue<int, int> qpass = new();
HashSet<int> set = new();
foreach(var bus in buses) qbus.Enqueue(bus, bus);
foreach(var p in passengers)
{
@svaza
svaza / Solution.cs
Created May 8, 2022 02:13
Permutations
using System.Collections.Generic;
public class Solution {
public IList<IList<int>> Permute(int[] nums) {
IList<IList<int>> solution = new List<IList<int>>();
Traverse(nums, new HashSet<int>(), new List<int>(), solution);
return solution;
}
@svaza
svaza / solution.ts
Created May 8, 2022 02:10
Combination sum
function combinationSum(candidates: number[], target: number): number[][] {
let allStack: number[][] = [];
traverse(candidates, target, [], allStack, 0);
return allStack;
};
function traverse(candidates: number[], target: number, currentStack: number[], allStack: number[][], startIter: number) {
if (target === 0) {
allStack.push([...currentStack]);
@svaza
svaza / Solution.cs
Created May 8, 2022 02:07
Rotate Image
using System;
public class Solution {
public void Rotate(int[][] matrix) {
for(int i = 0; i < matrix.Length/2; i++)
{
for(int j = i; j < matrix[i].Length - 1 - i; j++)
{
int b = matrix[j][matrix[i].Length-1-i];
@svaza
svaza / Solution.cs
Created April 28, 2022 03:41
ZigZag conversion
using System.Text;
using System.Collections.Generic;
public class Solution {
public string Convert(string s, int numRows) {
StringBuilder sb = new StringBuilder();
List<int> pillarIndexes = new List<int>();
int i = 0;
int h = numRows - 1;
@svaza
svaza / Solution.cs
Created April 27, 2022 03:21
Count N Say
using System.Text;
public class Solution {
public string CountAndSay(int n) {
if(n == 1) return "1";
var previousSay = CountAndSay(n - 1);
StringBuilder sb = new StringBuilder();
int runningOcc = 1;
@svaza
svaza / Solution.cs
Created April 27, 2022 02:32
Valid Sudoku
using System;
using System.Collections.Generic;
using System.Linq;
public class Solution {
public bool IsValidSudoku(char[][] board) {
List<List<HashSet<int>>> sudokuSection = Enumerable.Range(1, 3)
.Select(x => Enumerable.Range(1, 3).Select(x1 => new HashSet<int>(9)).ToList()).ToList();
List<HashSet<int>> rows = Enumerable.Range(1, 9).Select(x => new HashSet<int>(9)).ToList();
@svaza
svaza / Solution.cs
Created April 15, 2022 03:31
3 sum leetcode
using System.Collections.Generic;
public class Solution {
public IList<IList<int>> ThreeSum(int[] nums) {
var tripletsCollection = new List<IList<int>>(nums.Length);
var tripletSet = new HashSet<string>(nums.Length);
var numOccurance = new Dictionary<int, HashSet<int>>(nums.Length);
for(int i = 0; i < nums.Length; i++)
AddNumOccurance(numOccurance, nums[i], i);