zoukankan      html  css  js  c++  java
  • [LeetCode 1250] Check If It Is a Good Array

    Given an array nums of positive integers. Your task is to select some subset of nums, multiply each element by an integer and add all these numbers. The array is said to be good if you can obtain a sum of 1 from the array by any possible subset and multiplicand.

    Return True if the array is good otherwise return False.

     

    Example 1:

    Input: nums = [12,5,7,23]
    Output: true
    Explanation: Pick numbers 5 and 7.
    5*3 + 7*(-2) = 1
    

    Example 2:

    Input: nums = [29,6,10]
    Output: true
    Explanation: Pick numbers 29, 6 and 10.
    29*1 + 6*(-3) + 10*(-1) = 1
    

    Example 3:

    Input: nums = [3,6]
    Output: false
    

     

    Constraints:

    • 1 <= nums.length <= 10^5
    • 1 <= nums[i] <= 10^9

    This problem is an application of the Bezout's lemma: if gcd(A1, A2,...., An) = d, then there are integers X1, X2,..., Xn such that d = A1 * X1 + A2 * X2 + ... + An * Xn. d is the smallest positive integer of this form. Every number of this form is a multiple of d. This lemma is also a generalization of the extended euclid's algorithm: we can always get integer x and y such that a * x + b * y = gcd(a, b). 

    Applying the above lemma, we know that we just need to check if there exists two numbers X and Y that are coprime, gcd(X, Y) == 1. Here X and Y can be a single array number or a series of numbers' gcd. So we can just compute the entire array's gcd. If at any point the gcd becomes 1, we know that either we have a number of 1 or we have 2 co-prime numbers and we can find use these numbers to get a sum of 1 for sure.

    Euclid's GCD algorithms takes O(log(min(a, b))). So the runtime is O(N * log(min(a, b))). 

    class Solution {
        public boolean isGoodArray(int[] nums) {
            int g = nums[0];
            for(int i = 1; i < nums.length; i++) {
                g = gcd(g, nums[i]);
                if(g == 1) {
                    break;
                }
            }
            return g == 1;
        }
        private int gcd(int a, int b) {
            if(b == 0) {
                return a;
            }
            return gcd(b, a % b);
        }
    }
  • 相关阅读:
    音视频入门-15-手动生成一张JPEG图片
    音视频入门-14-JPEG文件格式详解
    音视频入门-13-使用开源库生成PNG图片
    微信小程序笔记
    JS找到嵌套在iframe内联框架中的table表头元素
    京东 PC 首页 2019 改版前端操作总结
    web前端开发中各种图片引入方式及其优缺点
    10个面向前端开发人员的安全提示
    HTML/CSS switch开关 (包括JS控制checked选择)
    Echarts常用属性含义
  • 原文地址:https://www.cnblogs.com/lz87/p/13733316.html
Copyright © 2011-2022 走看看