Skip to content

Instantly share code, notes, and snippets.

View primaryobjects's full-sized avatar

Kory Becker primaryobjects

View GitHub Profile
// O(n)
public class Solution {
public void Merge(int[] nums1, int m, int[] nums2, int n) {
int i = nums1.Length - 1;
int p1 = m-1;
int p2 = n-1;
while (p2 >= 0)
{
if (p1 >= 0 && nums1[p1] > nums2[p2])
public class Solution {
/* O(nlogn) + O(n) + O(n) = O(nlogn + 2n) => O(nlogn) */
public int[] SmallerNumbersThanCurrent(int[] nums) {
List<int> result = new List<int>();
Dictionary<int, int> hash = new Dictionary<int, int>();
List<int> sorted = nums.ToList();
sorted.Sort();
for (int i=0; i<sorted.Count; i++)
{
public class Solution {
public string MakeFancyString(string s) {
string result = s[0].ToString();
int count = 1;
char lastChar = s[0];
for (int i=1; i<s.Length; i++)
{
if (s[i] == lastChar)
{
@primaryobjects
primaryobjects / step2.py
Last active February 25, 2022 20:31
Learning Python with easy examples.
# What is the output of this program?
a = 6
b = 100
c = 12
d = 20
print(a + 4)
print(b - 25)
function gcdOfStrings(str1: string, str2: string): string {
// Start with the entire substring and look for matches, decreasing by 1 character each time.
for (let len=str2.length; len>0; len--) {
let part = str2.substr(0, len);
//console.log(`Trying ${part}`);
// Check if the string is evenly divisible by the part.
if (str1.length % part.length === 0) {
// Check if both str1 and str2 are evenly divisible by the part.
@primaryobjects
primaryobjects / pivotIndex.js
Last active April 4, 2023 17:24
Find the pivot index or middle of the array where the sum of values to the left equal the sum to the right of the index. https://leetcode.com/problems/find-pivot-index/
var pivotIndex = function(nums) {
let leftSum = 0;
let rightSum = nums.reduce((sum, value) => sum + value, 0);
for (let i=0; i<nums.length; i++) {
// Subtract this value from the rightSum, as we move towards the right.
rightSum -= nums[i];
if (rightSum === leftSum) {
// Found the middle point.
var commonChars = function(words) {
hash1 = {};
for (let i=0; i<words.length; i++) {
const word = words[i];
const hash2 = {}; // stores the counts
for (let ch of word) {
// Increment the key for this letter within this word, used for multiple occurrences of the same letter in the word.
hash2[ch] = hash2[ch] ? hash2[ch] + 1 : 1;
class MyQueue:
def __init__(self):
# Initialize two stacks.
self.stack1 = []
self.stack2 = []
def _flip(self, stack1, stack2, is_pop = False) -> int:
# Flips stack1 into stack2, with the top value from stack1 becoming the bottom value in stack2.
while (len(stack1) > 0):
stack2.append(stack1.pop())
@primaryobjects
primaryobjects / daysInYear.cs
Last active February 21, 2022 03:57
Find the number of days in the year for a given date YYYY-MM-DD. https://leetcode.com/problems/day-of-the-year/
//
// Solution using string splitting with fall-through switch statement.
//
public class Solution {
public int DayOfYear(string date)
{
String[] split= date.Split("-");
int year= Convert.ToInt32(split[0]);
int month= Convert.ToInt32(split[1]);
@primaryobjects
primaryobjects / index.html
Created February 18, 2022 04:28
Loading anime from a REST API using ReactJs. https://jsbin.com/bagotuyusu/edit?html,css,js,output
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script src="https://code.jquery.com/jquery.min.js"></script>
<script crossorigin src="https://unpkg.com/react@17/umd/react.production.min.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.production.min.js"></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.23/browser-polyfill.min.js'></script>