Skip to content

Instantly share code, notes, and snippets.

@svaza
svaza / Solution.cs
Last active April 13, 2022 16:58
String to Integer (atoi)
public class Solution {
public int MyAtoi(string s) {
sbyte sign = 1;
int runningNumber = 0;
bool isReadNumber = false;
bool isOverflow = false;
for(int i = 0; i < s.Length; i++)
{
if(Char.IsWhiteSpace(s[i]) && !isReadNumber) continue;
@svaza
svaza / solution.ts
Last active February 14, 2022 02:52
Directed Graph - Detect Cycle
export function cycleInGraph(edges: number[][]) {
// 1. loop through all vertices of the graph
for (let v = 0; v < edges.length; v++) {
if (hasCycle(edges, v, new Set<Number>()))
return true;
}
return false;
}
@svaza
svaza / Solution.ts
Created January 29, 2022 16:15
1277. Count Square Submatrices with All Ones
// https://leetcode.com/problems/count-square-submatrices-with-all-ones/
function countSquares(matrix: number[][]): number {
let count = 0;
for(let row = 0; row < matrix.length; row++) {
for(let column = 0; column < matrix[row].length; column++) {
let squareLength = 0;
while(squareLength < matrix.length) {
if(isOneMatrix(matrix, row, column, squareLength)) {
count++;
}
@svaza
svaza / Solution.ts
Created January 29, 2022 14:31
1641. Count Sorted Vowel Strings
// https://leetcode.com/problems/count-sorted-vowel-strings/
function countVowelStrings(n: number): number {
return traverse(n, 0, 0);;
};
const vowels: string[] = [ 'a', 'e', 'i', 'o', 'u' ];
function traverse(n: number, runningCombination: number, runningIdx: number): number {
if(runningCombination == n) {
return 1;
}
@svaza
svaza / Solution.js
Created January 28, 2022 03:54
maximum subarray
/**
* https://leetcode.com/problems/maximum-subarray/
* @param {number[]} nums
* @return {number}
*/
var maxSubArray = function(nums) {
let maxSum = Number.MIN_SAFE_INTEGER;
let runningSum = 0;
@svaza
svaza / solution.js
Created January 28, 2022 03:03
best time to buy stock
// https://leetcode.com/problems/best-time-to-buy-and-sell-stock/submissions/
var maxProfit = function(prices) {
let buyIndex = 0;
let lastProfit = 0;
for(let i = 1; i < prices.length; i++) {
let currentProfit = prices[i] - prices[buyIndex];
// stock down
@svaza
svaza / Solution.cs
Created January 26, 2022 03:52
Pascal Triangle 2
// https://leetcode.com/problems/pascals-triangle-ii/
public class Solution {
public IList<int> GetRow(int rowIndex) {
return PascalTriangle2(rowIndex);
}
public List<int> PascalTriangle2(int row)
{
if(row == 0)
return new List<int>() { 1 };
@svaza
svaza / Solution.cs
Last active January 26, 2022 03:19
pascal triangle
// https://leetcode.com/problems/pascals-triangle/submissions/
public class Solution {
public IList<IList<int>> Generate(int numRows) {
return PascalTriangle(numRows - 1,numRows - 1).Cast<IList<int>>().ToList();
}
public List<List<int>> PascalTriangle(int row, int columns)
{
if(row == 0)
@svaza
svaza / Solution.cs
Created January 25, 2022 02:53
Divisor Game
// https://leetcode.com/problems/divisor-game/
public class Solution {
public enum Player { Alice = 1, Bob= 2 }
private const int X = 1;
public bool DivisorGame(int n) {
return WhoLooses(Player.Alice, n) != Player.Alice;
}
@svaza
svaza / Solution.cs
Created January 23, 2022 20:52
Counting Bits
public class Solution {
public int[] CountBits(int n) {
if(n == 0) return new int[]{ 0 };
int[] output = new int[n + 1];
output[0] = 0;
output[1] = 1;
for(int i = 2; i < output.Length; i++)
{