Skip to content

Instantly share code, notes, and snippets.

View tiagopereira17's full-sized avatar

Tiago Pereira tiagopereira17

View GitHub Profile
@tiagopereira17
tiagopereira17 / MaxSliceSum.java
Created July 11, 2016 15:58
Find a maximum sum of a compact subsequence of array elements.
public class MaxSliceSum {
public int maxSliceSum(int[] A) {
if(A == null || A.length == 0) {
return 0;
}
int maxSum = Integer.MIN_VALUE;
int tempMaxSum = 0;
for(int i = 0; i < A.length; i++) {
tempMaxSum = Math.max(A[i], A[i] + tempMaxSum);
@tiagopereira17
tiagopereira17 / MaxProfit.java
Created July 11, 2016 14:45
Given a log of stock prices compute the maximum possible earning
public class MaxProfit {
public int solution(int[] A) {
if(A == null || A.length < 2) {
return 0;
}
int maxProfit = 0;
int min = -1;
for(int i = 0; i < A.length; i++) {
@tiagopereira17
tiagopereira17 / Dominator.java
Created July 9, 2016 14:13
Find an index of an array such that its value occurs at more than half of indices in the array.
package leader;
import java.util.Stack;
public class Dominator {
public int solution(int[] A) {
if(A == null || A.length == 0) {
return -1;
}
@tiagopereira17
tiagopereira17 / EquiLeader.java
Created July 9, 2016 13:59
Find the index S such that the leaders of the sequences A[0], A[1], ..., A[S] and A[S + 1], A[S + 2], ..., A[N - 1] are the same.
import java.util.Stack;
public class EquiLeader {
public int solution(int[] A) {
if(A == null || A.length == 0) {
return 0;
}
Stack<Integer> stack = new Stack<>();
for(int i = 0; i < A.length; i++) {
@tiagopereira17
tiagopereira17 / Brackets.java
Created July 8, 2016 15:39
Determine whether a given string of parentheses is properly nested.
package stacksandqueues;
import java.util.Stack;
public class Brackets {
public int solution(String S) {
if(!S.isEmpty() && S.length() % 2 != 0) {
return 0;
}
@tiagopereira17
tiagopereira17 / Fish.java
Created July 8, 2016 14:39
N voracious fish are moving along a river. Calculate how many fish are alive.
import java.util.Stack;
public class Fish {
public int solution(int[] A, int[] B) {
if(A == null || B == null || A.length != B.length) {
return -1;
}
Stack<Integer> downstream = new Stack<>();
int alive = 0;
@tiagopereira17
tiagopereira17 / MaxProductOfThree.java
Created July 6, 2016 16:06
Maximize A[P] * A[Q] * A[R] for any triplet (P, Q, R).
import java.util.Arrays;
public class MaxProductOfThree {
public int solution(int[] A) {
if(A == null || A.length < 3) {
return 0;
}
Arrays.sort(A);
@tiagopereira17
tiagopereira17 / Distinct.java
Created July 6, 2016 15:44
Compute number of distinct values in an array.
import java.util.Arrays;
public class Distinct {
public int distinctElements(int[] A) {
if(A == null || A.length == 0) {
return 0;
}
Arrays.sort(A);
@tiagopereira17
tiagopereira17 / Triangle.java
Created July 6, 2016 15:12
Determine whether a triangle can be built from a given set of edges.
import java.util.Arrays;
public class Triangle {
// A triplet (P, Q, R) is triangular if 0 ≤ P < Q < R < N and:
//
// A[P] + A[Q] > A[R],
// A[Q] + A[R] > A[P],
// A[R] + A[P] > A[Q].
public int hasTriangle(int[] A) {
@tiagopereira17
tiagopereira17 / PassingCars.java
Created July 4, 2016 18:25
Count the number of passing cars on the road.
public class PassingCars {
public int solution(int[] A) {
// 0 - east, 1 - west
int west = 0;
int passing = 0;
for(int i = A.length - 1; i >= 0; i--) {
if(A[i] == 0) {
passing += west;
if(passing > 1000000000) {