Instantly share code, notes, and snippets.

# Tiago Pereira tiagopereira17

• Switzerland
Created July 11, 2016 15:58
Find a maximum sum of a compact subsequence of array elements.
This file contains 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 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);
Created July 11, 2016 14:45
Given a log of stock prices compute the maximum possible earning
This file contains 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 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++) {
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.
This file contains 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
 package leader; import java.util.Stack; public class Dominator { public int solution(int[] A) { if(A == null || A.length == 0) { return -1; }
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.
This file contains 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
 import java.util.Stack; public class EquiLeader { public int solution(int[] A) { if(A == null || A.length == 0) { return 0; } Stack stack = new Stack<>(); for(int i = 0; i < A.length; i++) {
Created July 8, 2016 15:39
Determine whether a given string of parentheses is properly nested.
This file contains 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
 package stacksandqueues; import java.util.Stack; public class Brackets { public int solution(String S) { if(!S.isEmpty() && S.length() % 2 != 0) { return 0; }
Created July 8, 2016 14:39
N voracious fish are moving along a river. Calculate how many fish are alive.
This file contains 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
 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 downstream = new Stack<>(); int alive = 0;
Created July 6, 2016 16:06
Maximize A[P] * A[Q] * A[R] for any triplet (P, Q, R).
This file contains 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
 import java.util.Arrays; public class MaxProductOfThree { public int solution(int[] A) { if(A == null || A.length < 3) { return 0; } Arrays.sort(A);
Created July 6, 2016 15:44
Compute number of distinct values in an array.
This file contains 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
 import java.util.Arrays; public class Distinct { public int distinctElements(int[] A) { if(A == null || A.length == 0) { return 0; } Arrays.sort(A);
Created July 6, 2016 15:12
Determine whether a triangle can be built from a given set of edges.
This file contains 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
 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) {
Created July 4, 2016 18:25
Count the number of passing cars on the road.
This file contains 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 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) {