Skip to content

Instantly share code, notes, and snippets.

@elvismetaphor
elvismetaphor / main.kt
Last active August 2, 2021 04:57
Try coroutines
import kotlinx.coroutines.experimental.*
fun main(args: Array<String>) {
val start = System.currentTimeMillis()
exampleBlocking()
val end = System.currentTimeMillis()
println("executing time: ${end - start}")
@elvismetaphor
elvismetaphor / LocalBarcodeScanner.java
Last active July 18, 2018 07:07
Add on-device barcode scanner
public void scanBarcode(Bitmap image) {
FirebaseVisionImage image = FirebaseVisionImage.fromBitmap(image);
FirebaseVisionBarcodeDetectorOptions options =
new FirebaseVisionBarcodeDetectorOptions.Builder()
.setBarcodeFormats(
FirebaseVisionBarcode.FORMAT_QR_CODE,
FirebaseVisionBarcode.FORMAT_AZTEC
)
.build();
@elvismetaphor
elvismetaphor / CloudTextRecognition.java
Created July 12, 2018 16:44
Add cloud text recognition (Android)
public void runCloudTextRecognition(Bitmap selectedImage) {
FirebaseVisionCloudDetectorOptions options = new FirebaseVisionCloudDetectorOptions.Builder()
.setModelType(FirebaseVisionCloudDetectorOptions.LATEST_MODEL)
.setMaxResults(15)
.build();
FirebaseVisionImage image = FirebaseVisionImage.fromBitmap(selectedImage);
FirebaseVisionCloudDocumentTextDetector detector = FirebaseVision.getInstance()
.getVisionCloudDocumentTextDetector(options);
@elvismetaphor
elvismetaphor / CloudTextRecognition.java
Created July 12, 2018 16:44
Add cloud text recognition (Android)
public void runCloudTextRecognition(Bitmap selectedImage) {
FirebaseVisionCloudDetectorOptions options = new FirebaseVisionCloudDetectorOptions.Builder()
.setModelType(FirebaseVisionCloudDetectorOptions.LATEST_MODEL)
.setMaxResults(15)
.build();
FirebaseVisionImage image = FirebaseVisionImage.fromBitmap(selectedImage);
FirebaseVisionCloudDocumentTextDetector detector = FirebaseVision.getInstance()
.getVisionCloudDocumentTextDetector(options);
@elvismetaphor
elvismetaphor / LocalTextRecognition.java
Created July 12, 2018 16:42
Add on-device text recognition (Android)
public void runTextRecognition(Bitmap selectedImage) {
FirebaseVisionImage image = FirebaseVisionImage.fromBitmap(selectedImage);
FirebaseVisionTextDetector detector = FirebaseVision.getInstance().getVisionTextDetector();
detector.detectInImage(image)
.addOnSuccessListener(new OnSuccessListener<FirebaseVisionText>() {
@Override
public void onSuccess(FirebaseVisionText text) {
processTextRecognitionResult(text);
@elvismetaphor
elvismetaphor / CloudTextRecognition.swift
Created July 12, 2018 16:33
Add cloud text recognition (iOS)
laze var cloudTextDetector: VisionCloudTextDetector = Vision.vision().cloudTextDetector()
func runCloudTextRecognition(with image: UIImage) {
let visionImage = VisionImage(image: image)
cloudTextDetector.detect(in: visionImage) { (features, error) in
if let error = error {
print("Received error: \(error)")
}
@elvismetaphor
elvismetaphor / LocalTextRecognition.swift
Last active July 12, 2018 16:36
Add on-device text recognition (iOS)
lazy var textDetector: VisionTextDetector = Vision.vision().textDetector()
func runTextRecognition(with image: UIImage) {
let visionImage = VisionImage(image: image)
textDetector.detect(in: visionImage) { (features, error) in
if let error = error {
print("Received error: \(error)")
}

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; 
}

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) {

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)