Skip to content

Instantly share code, notes, and snippets.

View thieunguyencrystal's full-sized avatar
🎯
iOS Developer

Thieu thieunguyencrystal

🎯
iOS Developer
  • Crystal
View GitHub Profile
@thieunguyencrystal
thieunguyencrystal / Solution.java
Last active March 16, 2019 12:31
a+b+c+d=target
public class Solution {
public List<List<Integer>> fourSum(int[] num, int target) {
ArrayList <List<Integer>> resultList = new ArrayList<>();
int length = num.length;
if (length < 4)
return resultList;
Arrays.sort(num);
for (int i = 0; i < length - 3; i++) {
@thieunguyencrystal
thieunguyencrystal / Solution.swift
Created March 17, 2019 11:57
Given a list of numbers and a number k, return whether any two numbers from the list add up to k.
/*
Given a list of numbers and a number k, return whether any two numbers from the list add up to k.
For example, given [10, 15, 3, 7] and k of 17, return true since 10 + 7 is 17.
*/
import UIKit
var array = [10, 15, 3, 7]
let k = 17
@thieunguyencrystal
thieunguyencrystal / Solution.swift
Last active March 19, 2019 07:48
Given an array of integers, return a new array such that each element at index i of the new array is the product of all the numbers in the original array except the one at i. For example, if our input was [1, 2, 3, 4, 5], the expected output would be [120, 60, 40, 30, 24]. If our input was [3, 2, 1], the expected output would be [2, 3, 6].
/*
Given an array of integers, return a new array such that each element at index i of the new array is the product of all the numbers in the original array except the one at i.
For example, if our input was [1, 2, 3, 4, 5], the expected output would be [120, 60, 40, 30, 24]. If our input was [3, 2, 1], the expected output would be [2, 3, 6].
*/
import UIKit
// 01 a1 a2 a3
// a0 01 a2 a3
// a0 a1 01 a3
// a0 a1 a2 01
var array = [1, 2, 3]
@thieunguyencrystal
thieunguyencrystal / SmallestPositiveNumber.swift
Created March 21, 2019 11:06
Find the smallest positive number missing from an unsorted array
/*
Given an array of integers, find the first missing positive integer in linear time and constant space. In other words, find the lowest positive integer that does not exist in the array. The array can contain duplicates and negative numbers as well.
For example, the input [3, 4, -1, 1] should give 2. The input [1, 2, 0] should give 3.
You can modify the input array in-place.
*/
func solution(_ array: [Int]) -> Int {
guard let max = array.max(), max > 0 else { return 1 }
@thieunguyencrystal
thieunguyencrystal / LowestInt.cpp
Created March 23, 2019 09:59
Find Smallest Positive Number
#include <stdio.h>
#include <vector>
#include <unordered_map>
#include <set>
using namespace std;
struct MinMax {
int min;
int max;
@thieunguyencrystal
thieunguyencrystal / FindMaxSum.swift
Last active March 28, 2019 03:31
Maximum sum such that no two elements are adjacent
/* Maximum sum such that no two elements are adjacent */
func solution(_ array: [Int]) -> Int? {
let count = array.count
guard count >= 0 else { return nil }
var inclusion = array[0]
var exclusion = 0
var newExclusion = 0
for i in (1..<count) {
newExclusion = inclusion > exclusion ? inclusion : exclusion