ARTS 第33周:翻转图像算法、倒排索引、聚合模式与数据库设计范式化

#coding#arts
目录

Algorithm

package org.nocoder.leetcode.solution;

/**
 * 832. Flipping an Image
 * Given a binary matrix A, we want to flip the image horizontally,
 * then invert it, and return the resulting image.
 *
 * To flip an image horizontally means that each row of the image is reversed.
 * For example, flipping [1, 1, 0] horizontally results in [0, 1, 1].
 *
 * To invert an image means that each 0 is replaced by 1, and each 1 is replaced by 0.
 * For example, inverting [0, 1, 1] results in [1, 0, 0].
 *
 * Example 1:
 *
 * Input: [[1,1,0],[1,0,1],[0,0,0]]
 * Output: [[1,0,0],[0,1,0],[1,1,1]]
 * Explanation: First reverse each row: [[0,1,1],[1,0,1],[0,0,0]].
 * Then, invert the image: [[1,0,0],[0,1,0],[1,1,1]]
 * Example 2:
 *
 * Input: [[1,1,0,0],[1,0,0,1],[0,1,1,1],[1,0,1,0]]
 * Output: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
 * Explanation: First reverse each row: [[0,0,1,1],[1,0,0,1],[1,1,1,0],[0,1,0,1]].
 * Then invert the image: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
 * Notes:
 *
 * 1 <= A.length = A[0].length <= 20
 * 0 <= A[i][j] <= 1
 * @author yangjinlong
 */
public class FlippingAnImage {
    public static void main(String[] args) {
        int[][] arr = new int[][]{{1,1,0},{1,0,1},{0,0,0}};
        int[][] result = flipAndInvertImage(arr);
        for (int i = 0; i < result.length; i++) {
            for (int j = 0; j < result[i].length ; j++) {
                System.out.print(result[i][j]);
            }
            System.out.print(", ");
        };
    }
    public static int[][] flipAndInvertImage(int[][] arr) {
        if(arr == null || arr.length < 1 || arr[0].length > 20){
            throw new IllegalArgumentException();
        }

        for (int i = 0; i < arr.length; i++) {
            int[] reverseArray = reverseArray(arr[i]);
            for (int j = 0; j < reverseArray.length; j++) {
                if(reverseArray[j] == 0){
                    reverseArray[j] = 1;
                }else{
                    reverseArray[j] = 0;
                }
                arr[i] = reverseArray;
            }
        }
        return arr;
    }
    public static int[] reverseArray(int[] arr){
        if(arr == null || arr.length < 1){
            return arr;
        }
        int[] result = new int[arr.length];
        int j = 0;
        for (int i = arr.length-1; i >= 0; i--) {
            result[j++] = arr[i];
        }
        return result;
    }
}

Review

Inverted Index

倒排索引是索引数据结构,其存储从诸如单词或数字的内容到其在文档或一组文档中的位置的映射。简单来说,它是一个类似于数据结构的散列图,可以将您从单词引导到文档或网页。

假设我们要搜索文本“hello everyone, ” “this article is based on inverted index, ” “which is hashmap like data structure”。按照(文本,文本中的单词的位置)来构建索引,带有文本位置的索引如下:

 hello                (1, 1)
 everyone             (1, 2)
 this                 (2, 1)
 article              (2, 2)
 is                   (2, 3); (3, 2)
 based                (2, 4)
 on                   (2, 5)
 inverted             (2, 6)
 index                (2, 7)
 which                (3, 1)
 hashmap              (3, 3)
 like                 (3, 4)
 data                 (3, 5)
 structure            (3, 6)

构建倒排索引的步骤:

倒排索引的优点:

倒排索引的缺点:

Tip

Aggregator Pattern

Problem

We have talked about resolving the aggregating data problem in the API Gateway Pattern. However, we will talk about it here holistically. When breaking the business functionality into several smaller logical pieces of code, it becomes necessary to think about how to collaborate the data returned by each service. This responsibility cannot be left with the consumer, as then it might need to understand the internal implementation of the producer application.

Solution

The Aggregator pattern helps to address this. It talks about how we can aggregate the data from different services and then send the final response to the consumer. This can be done in two ways:

  1. A composite microservice will make calls to all the required microservices, consolidate the data, and transform the data before sending back.

  2. An API Gateway can also partition the request to multiple microservices and aggregate the data before sending it to the consumer.

It is recommended if any business logic is to be applied, then choose a composite microservice. Otherwise, the API Gateway is the established solution.

Share

数据库设计的范式化和反范式化

范式设计需要考虑应用类型以及数据分布情况,灵活选择范式化或者反范式化

1、范式化的优点是:更新速度快,没有或者很少冗余数据,通常表也比较小;缺点是 :通常的查询读需要进行一次甚至是多次关联,代价十分昂贵。

2、反范式化的优点是:数据都在一张表里,可以避免关联查询,最差的情况下数据比内存大的多的情况下也会比关联查询效率更高,因为全表扫描基本上是顺序 IO。

3、因此单纯的范式化或者反范式化都是不现实的,灵活选择结合使用才是王道。


评论区