【Double Week】No.24

C_01 Gradually sum to get the minimum value of positive number

Gives you an array of integers nums. You can choose any positive startValue as the initial value.

You need to traverse the nums array from left to right and accumulate the values ​​in the nums array in turn.

Please make sure that the cumulative sum is always greater than or equal to 1 and choose a smallest positive number as startValue.

输入:nums = [-3,2,-3,4,2]
输出:5
解释:如果你选择 startValue = 4,在第三次累加时,和小于 1 。
                累加求和
                startValue = 4 | startValue = 5 | nums
                  (4 -3 ) = 1  | (5 -3 ) = 2    |  -3
                  (1 +2 ) = 3  | (2 +2 ) = 4    |   2
                  (3 -3 ) = 0  | (4 -3 ) = 1    |  -3
                  (0 +4 ) = 4  | (1 +4 ) = 5    |   4
                  (4 +2 ) = 6  | (5 +2 ) = 7    |   2

Method 1: Enumeration

public int minStartValue(int[] nums) {
    int st = Integer.MAX_VALUE;
    int t = -1;

    for (int i = 1; i < Integer.MAX_VALUE; i++) {
        st = i;
        int sum = st;
        boolean flag = true;
        for (int j = 0; j < nums.length; j++) {
            sum += nums[j];
            if (sum < 1) {
                flag = false;
                break;
            }
        }
        if (flag) {
            return st;
        }
    }
    return st;
}

Complexity analysis

  • time complexity: O ( . . . ) O(...) ,
  • Space complexity: O ( 1 ) O (1)

Method 2: Prefix and

  • Think of it this way: because the goal is to make the smallest prefix and value appearing in the accumulation process greater than 1,
  • Therefore, we need to record the min value of the result obtained after each accumulation.
  • Finally, the formula x-min> = 1 gives: x = 1-min
public int minStartValue(int[] nums) {
    int prefix = 0, min = 0;
    for (int n : nums) {
        prefix += n;
        min = Math.min(min, prefix);
    }
    return 1 - min;
}

Complexity analysis

  • time complexity: O ( n ) O (n)
  • Space complexity: O ( 1 ) O (1)

The minimum number of Fibonacci numbers with B_02 and K

To give you the number k, please return the minimum number of Fibonacci numbers that are sum k, where each Fibonacci number can be used multiple times.

Fibonacci numbers are defined as:

  • F1 = 1
    F2 = 1
    Fn = Fn-1 + Fn-2, where n> 2.

The data guarantees that for a given k, a feasible solution must be found.

Tip: 1 <= k <= 10 ^ 9

输入:k = 7
输出:2 
解释:斐波那契数字为:1,1,2,3,5,8,13,……
对于 k = 7 ,我们可以得到 2 + 5 = 7 。

输入:k = 19
输出:3 
解释:对于 k = 19 ,我们可以得到 1 + 5 + 13 = 19 。

Method 1: Greed

The least number required is used, which means that we can only use a relatively large number close to k first. My approach is to pre-process the fab first, and then enumerate from back to front, try to use large numbers.

public int findMinFibonacciNumbers(int k) {
    List<Integer> fab = new ArrayList<>();
    fab.add(1);
    fab.add(1);
    while (fab.get(fab.size()-1) < k) {
        int t = fab.get(fab.size()-1) + fab.get(fab.size()-2); 
        fab.add(t);
    }
    int cnt = 0;
    for (int i = fab.size()-1; i >= 0; i--) {
        while (k >= fab.get(i)) {
            k -= fab.get(i);
            cnt++;
        } 
    }
    return cnt;
}

Complexity analysis

  • time complexity: O ( n ) O (n)
  • Space complexity: O ( n ) O (n)

B_03 The string with the kth smallest lexicographic order in the happy string of length n

A "happy string" is defined as:

  • Contains only lowercase letters ['a', 'b', 'c'].
  • For all i between 1 and s.length-1, satisfy s [i]! = S [i + 1] (the subscript of the string starts from 1).

For example, the strings "abc", "ac", "b" and "abcbabcbcb" are happy strings, but "aa", "baa" and "ababbc" are not happy strings.

To give you two integers n and k, you need to sort all happy strings of length n lexicographically.

Please return the kth happy string after sorting. If there are less than k happy strings with length n, then please return the empty string

prompt:

  • 1 <= n <= 10
    1 <= k <= 100
输入:n = 3, k = 9
输出:"cab"
解释:长度为 3 的开心字符串总共有 12 个
 ["aba", "abc", "aca", "acb", "bab", "bac", 
 "bca", "bcb", "cab", "cac", "cba", "cbc"] 。
 第 9 个字符串为 "cab"

Method 1: Backtracking

https://blog.csdn.net/qq_43539599/article/details/105610372


A_04 Restore array

A program should originally output an array of integers. But this program forgot to output spaces and output a string of numbers. The only information we know is: all integers in the array are between [1, k], and there are no leading 0s in the numbers in the array.

Gives you the string s and the integer k. There may be many different array recovery results.

According to the above procedure, please return the number of all possible array schemes of the output string s.

Since the number of array schemes may be large, please return the result after taking the remainder of 10 ^ 9 + 7.

输入:s = "1317", k = 2000
输出:8
解释:可行的数组方案为 [1317],[131,7],[13,17],[1,317],[13,1,7],[1,31,7],[1,3,17],[1,3,1,7]
输
入:s = "2020", k = 30
输出:1
解释:唯一可能的数组方案是 [20,20] 。 [2020] 不是可行的数组方案,原因是 2020 > 30 。
 [2,020] 也不是可行的数组方案,因为 020 含有前导 0 。

Method 1: None

Published 714 original articles · praised 199 · 50,000+ views

Guess you like

Origin blog.csdn.net/qq_43539599/article/details/105607829