Skip to content

Instantly share code, notes, and snippets.

public class SingletonSample{
private static SingletonSample instance= null;
private static Object mutex= new Object();
private String value;
private SingletonSample(){}
public static SingletonSample getInstance(){
import java.util.HashMap;
import java.util.Map;
public class SimilarNumberCounter {
private static Map<Integer, Integer> digitCounts = new HashMap<>();
private static void initializeDigitCounts() {
for (int i = 0; i <= 9; i++) {
digitCounts.put(i, 0);
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class CombinationCounter {
private static List<BigInteger> generateCombinationLimitedBy(int index) {
List<BigInteger> sequence = new ArrayList<>();
@elvismetaphor
elvismetaphor / CustomSetter.java
Last active July 7, 2022 07:15
Show how to implement MVVM with Data Binding while using Glide
package edu.self.binding;
import android.databinding.BindingAdapter;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
public class CustomSetter {
package com.example.retrofit;
import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import static junit.framework.TestCase.assertTrue;
// 一般來說,只要把名稱取為 ContributorAppTest 即可
package self.edu.observable;
import io.reactivex.Observable;
import io.reactivex.Scheduler;
import io.reactivex.observers.TestObserver;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.schedulers.TestScheduler;
import org.junit.Assert;
import org.junit.Test;

Concept:

Use HashSet to determine the result

Solution:

public class SubsetChecker {

    public boolean isSubset(char[] first, char[] second) {
        Set<Character> set = generateCharacterSet(first);
        return containsAll(set, second);

Answer

Let the size of the first array is N and the size of the second array is M. The big O of the time complexity of this algorithm is O(N + M)

Reason

In the beginning, we put all elements in the first array into a HashSet object, the time complexity is O(N) because we walk throught all elements, and saving a element into a hash set only costs a constant time.

In order to determine if the second array is a subset of the first array, we walk through all elements in the second array, and check if each element has already been in the hash set. Because finding a element in a hash set also costs a constant time, the time complexity is O(M)

After executing the above steps, we can know if the second array is a subset of the first array. The total time complexity is O(M + N)

Concept

  1. Find the largest number from the input
  2. Generate a necessary fibonaci numbers
  3. Walk through all elements in the input and find the next fibonacci number

Solutino

public class NextFibonacciGenerator {

    public void nextFibonacci(int[] input) {

Sympton

For this code

function createArrayOfFunctions(y) {
  var arr = [];
  for(var i = 0; i<y; i++) {
    arr[i] = function(x) { return x + i; }
  }
  return arr; 
}