This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <vector> | |
#include <unordered_map> | |
#include <set> | |
using namespace std; | |
struct MinMax { | |
int min; | |
int max; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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 } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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++) { |