Skip to content

Instantly share code, notes, and snippets.

package NumberSolitaire;
class Solution {
public int solution(int[] A) {
// main idea:
// using "dynamic programming" to build up the solution
// (bottom up)
int[] dp = new int[A.length];
@Mickey0521
Mickey0521 / Codility_Lession_16-2_TieRopes.java
Created September 25, 2017 10:44
Codility my solutions
package TieRopes;
class Solution {
public int solution(int K, int[] A) {
// notice that only "adjacent ropes" can be tied
// so, the problem is simple; we can use "greedy" method
int total =0;
int currentLength=0;
package MaxNonoverlappingSegments;
class Solution {
public int solution(int[] A, int[] B) {
// main idea:
// Using "greedy" method to find non-overlapping segments
// because the segments are sorted by their rightEnds
// we use "for loop" from rightEnd to left
package CountTriangles;
import java.util.*;
class Solution {
public int solution(int[] A) {
int numTriangle = 0;
// important: sort the edges
// This solution is simple and correct, but with low performance (100% correct, 40% performance)
package CountDistinctSlices;
import java.util.*;
class Solution {
public int solution(int M, int[] A) {
// key point: using "set" for "each leftEnd"
package CountDistinctSlices;
class Solution {
public int solution(int M, int[] A) {
// This solution is more clever, and much faster O(n)
// main idea:
// use "boolean[]" to record if an integer is already seen
// also use "leftEnd" and "rightEnd"
package AbsDistinct;
import java.util.*;
class Solution {
public int solution(int[] A) {
// using "Set"
Set<Integer> set = new HashSet<>();
package MinMaxDivision;
class Solution {
public int solution(int K, int M, int[] A) {
// main idea:
// The goal is to find the "minimal large sum"
// We use "binary search" to find it (so, it can be fast)
// We assume that the "min max Sum" will be
@Mickey0521
Mickey0521 / Codility_Lession_13-2_FibFrog.java
Created September 23, 2017 20:48
Codility my solutions
package FibFrog;
import java.util.*;
// for using "point" (java.awt.*)
import java.awt.*;
class Solution {
public int solution(int[] A) {
// note: cannot use "List" (both java.util.* and java.awt.* have "List")
@Mickey0521
Mickey0521 / Codility_Lession_13-1_Ladder.java
Created September 23, 2017 17:13
Codility my solutions
package Ladder;
class Solution {
public int[] solution(int[] A, int[] B) {
// The task is to find out the number of ways
// someone can climb up a ladder of N rungs
// by ascending one or two rungs at a time.
// It is not very hard to see that
// this number is just the "Fibonacci number of order N"