Skip to content

Instantly share code, notes, and snippets.

@adamkorg
adamkorg / leetcode_115a.cpp
Created October 25, 2019 20:07
Leetcode 115: Distinct Subsequences (recursive)
#include <iostream>
#include <string>
using namespace std;
void match(const string& s, const string& t, int idxs, int idxt, int& matches) {
if (idxt == t.size()) {
matches++;
return;
}
@adamkorg
adamkorg / leetcode_181.sql
Created November 27, 2019 23:20
Leetcode 181: Employees Earning More Than Their Managers
Create table If Not Exists Employee (Id int, Name varchar(255), Salary int, ManagerId int);
insert into Employee (Id, Name, Salary, ManagerId) values ('1', 'Joe', '70000', '3');
insert into Employee (Id, Name, Salary, ManagerId) values ('2', 'Henry', '80000', '4');
insert into Employee (Id, Name, Salary, ManagerId) values ('3', 'Sam', '60000', NULL);
insert into Employee (Id, Name, Salary, ManagerId) values ('4', 'Max', '90000', NULL);
SELECT Emp.Name As Employee
FROM Employee As Emp, Employee As Manager
WHERE Emp.ManagerId = Manager.Id AND
Emp.Salary > Manager.Salary;
@adamkorg
adamkorg / leetcode_182.sql
Created November 28, 2019 11:44
Leetcode 182: Duplicate Emails
Create table If Not Exists Person (Id int, Email varchar(255));
insert into Person (Id, Email) values ('1', 'a@b.com');
insert into Person (Id, Email) values ('2', 'c@d.com');
insert into Person (Id, Email) values ('3', 'a@b.com');
SELECT Email FROM (
SELECT Email, Count(Email) As Cnt
FROM Person
GROUP By Email) As CountsTable
WHERE Cnt > 1;
@adamkorg
adamkorg / leetcode_185.sql
Created November 29, 2019 03:10
Leetcode 185: Department Top Three Salaries
Create table If Not Exists Employee (Id int, Name varchar(255), Salary int, DepartmentId int);
Create table If Not Exists Department (Id int, Name varchar(255));
insert into Employee (Id, Name, Salary, DepartmentId) values ('1', 'Joe', '85000', '1');
insert into Employee (Id, Name, Salary, DepartmentId) values ('2', 'Henry', '80000', '2');
insert into Employee (Id, Name, Salary, DepartmentId) values ('3', 'Sam', '60000', '2');
insert into Employee (Id, Name, Salary, DepartmentId) values ('4', 'Max', '90000', '1');
insert into Employee (Id, Name, Salary, DepartmentId) values ('5', 'Janet', '69000', '1');
insert into Employee (Id, Name, Salary, DepartmentId) values ('6', 'Randy', '85000', '1');
insert into Employee (Id, Name, Salary, DepartmentId) values ('7', 'Will', '70000', '1');
insert into Department (Id, Name) values ('1', 'IT');
@adamkorg
adamkorg / leetcode_175.sql
Created November 25, 2019 20:26
Leetcode 175: Combine Two Tables
# Write your MySQL query statement below
SELECT Person.FirstName, Person.LastName, Address.City, Address.State
FROM Person
LEFT JOIN Address
ON Person.PersonId = Address.PersonId;
@adamkorg
adamkorg / leetcode_278.cpp
Created April 30, 2020 16:02
Leetcode 278: First Bad Version
#include <iostream>
using namespace std;
//leetcode supplied function (I've created an example):
bool isBadVersion(int n) {
return (n >= 4);
}
int firstBadVersion(int n) {
@adamkorg
adamkorg / leetcode_277b.cpp
Created April 21, 2020 22:12
Leetcode 277: Find the Celebrity (O(n))
#include <iostream>
using namespace std;
bool knows(int a, int b);
int findCelebrity(int n) {
if (n==0) return -1;
int celeb = 0;
for (int i=1; i<n; ++i) {
@adamkorg
adamkorg / leetcode_277a.cpp
Created April 21, 2020 22:11
Leetcode 277: Find the Celebrity (O(n^2))
#include <iostream>
using namespace std;
bool knows(int a, int b);
int findCelebrity(int n) {
for (int b=0; b<n; ++b) {
int count=0;
for (int a=0; a<n; ++n) {
@adamkorg
adamkorg / leetcode_276c.cpp
Created April 11, 2020 12:53
Leetcode 276: Paint Fence (O(1) space)
#include <iostream>
#include <vector>
using namespace std;
int numWays(int n, int k) {
if (n==0 || k==0) return 0;
if (n==1) return k;
if (n==2) return k*k;
vector<int> dp {0,1,k};
@adamkorg
adamkorg / leetcode_276b.cpp
Created April 11, 2020 12:52
Leetcode 276: Paint Fence (O(n) space)
#include <iostream>
#include <vector>
using namespace std;
int numWays(int n, int k) {
if (n==0 || k==0) return 0;
vector<int> dp(n,1);
for (int c=1; c<n; ++c) {