class Solution {
public int solution(int[] A) { // write your code in Java SE 8 int n=A.length; int maxSum=A[0]; int localMax=A[0]; for (int i =1;i<n;i++){ if(localMax>0) localMax=Math.max(0, localMax+A[i]); else localMax=Math.max(localMax, A[i]); maxSum= Math.max(maxSum, localMax); } return maxSum; }}quarta-feira, 13 de julho de 2022
terça-feira, 12 de julho de 2022
Codility Fish problem
After some research I did find this exercice is meant to be solved with stacks. But for me in the context of the problem it doesn't make any sense.
I solved it with a queue, a new fish arrives downstream, then we should include it in the **tail**.
In problem for some reason they want us to make it in the head, althoug they specify the fishs all swim at the same speed. More anoying is that the samples work perfectly well like this.
50% solution
import java.util.*;
return survUpstrean + downstream.size();
}
100% replacing queue with stack
// you can also use imports, for example: import java.util.*; // you can write to stdout for debugging purposes, e.g. // System.out.println("this is a debug message"); class Solution { public static int solution(int[] A, int[] B) { // write your code in Java SE 8 int n = A.length; Stack<Integer> downstream = new Stack<>(); int survUpstrean = 0; for (int i = 0; i < n; i++) { if (B[i] == 1) { downstream.push(A[i]); } if (B[i] == 0 && downstream.size()>0) { while (downstream.size() > 0) { int currFish=downstream.pop(); if (A[i] < currFish) { downstream.push(currFish); break; } } } if (downstream.size() == 0) { survUpstrean++; } } return survUpstrean + downstream.size(); } }
segunda-feira, 11 de julho de 2022
Codility various from lessons
// you can also use imports, for example: import java.util.*; // you can write to stdout for debugging purposes, e.g. // System.out.println("this is a debug message"); class Solution { public int solution(String S) { // write your code in Java SE 8 int res=0; Stack s = new Stack(); int n= S.length(); if (n%2!=0) return 0; for (char c: S.toCharArray()) { if (isOpenBracket(c)) s.push(c); else{ if (s.isEmpty()) return 0; char l = (char) s.pop(); if (!isPair(l,c)) return 0; } } if (s.isEmpty()) return 1; else return 0; } static Boolean isOpenBracket( char c){ if ( c=='(') return true; else return false; } public static Boolean isPair(char l, char r){ return l == '(' && r == ')'; } }
MaxProductOfThree
// you can also use imports, for example: import java.util.*; // you can write to stdout for debugging purposes, e.g. // System.out.println("this is a debug message"); class Solution { public int solution(int[] A) { // write your code in Java SE 8 int n=A.length; Arrays.sort(A); int max = Math.max((A[n-1]*A[n-2]*A[n-3]), (A[0]*A[1]*A[n-1])); max = Math.max(max, (A[0]*A[1]*A[n-2])); max = Math.max(max, (A[0]*A[1]*A[n-3])); return max; } }
O(N) 100% solution
sábado, 9 de julho de 2022
PermCheck - Codility
A permutation is a sequence containing each element from 1 to N once, and only once.
A set of integers will be a permutation if there are no duplicates or values bigger then the sequence.
import java.util.Arrays;
class Solution { public int solution(int[] A) { // write your code in Java SE 8 int size= A.length; int [] holder= new int [size+1]; Arrays.fill(holder, 0); for (int t=0; t<size; t++){ if (A[t]>size) return 0; if(holder[A[t]]==0){ holder[A[t]]=A[t]; }else return 0; } return 1; } }quarta-feira, 6 de julho de 2022
Hacker rank caesarCipher
a-> 97
z-> 122
A -> 65
Z -> 90
public static String caesarCipher(String s, int k) {
sábado, 2 de julho de 2022
Hacker Rank Time Conversion
Taking and approach using Java java.util.Calendar, java.util.Date and java.time.format.DateTimeFormatter.
Hacker Rank - Binary tree (Java)
This was my approach, it can be improved (I saw other better solution), but this works.
- Get parents by searching the node in tree recursivelly for each value
- Compare the list of parent, the first to be found is common lesser ancestor