All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0801_0900.s0842_split_array_into_fibonacci_sequence.Solution Maven / Gradle / Ivy

There is a newer version: 1.38
Show newest version
package g0801_0900.s0842_split_array_into_fibonacci_sequence;

// #Medium #String #Backtracking #2022_03_24_Time_4_ms_(62.81%)_Space_43.6_MB_(32.65%)

import java.util.ArrayList;
import java.util.List;

/**
 * 842 - Split Array into Fibonacci Sequence\.
 *
 * Medium
 *
 * You are given a string of digits `num`, such as `"123456579"`. We can split it into a Fibonacci-like sequence `[123, 456, 579]`.
 *
 * Formally, a **Fibonacci-like** sequence is a list `f` of non-negative integers such that:
 *
 * *   0 <= f[i] < 231, (that is, each integer fits in a **32-bit** signed integer type),
 * *   `f.length >= 3`, and
 * *   `f[i] + f[i + 1] == f[i + 2]` for all `0 <= i < f.length - 2`.
 *
 * Note that when splitting the string into pieces, each piece must not have extra leading zeroes, except if the piece is the number `0` itself.
 *
 * Return any Fibonacci-like sequence split from `num`, or return `[]` if it cannot be done.
 *
 * **Example 1:**
 *
 * **Input:** num = "1101111"
 *
 * **Output:** [11,0,11,11]
 *
 * **Explanation:** The output [110, 1, 111] would also be accepted.
 *
 * **Example 2:**
 *
 * **Input:** num = "112358130"
 *
 * **Output:** []
 *
 * **Explanation:** The task is impossible.
 *
 * **Example 3:**
 *
 * **Input:** num = "0123"
 *
 * **Output:** []
 *
 * **Explanation:** Leading zeroes are not allowed, so "01", "2", "3" is not valid.
 *
 * **Constraints:**
 *
 * *   `1 <= num.length <= 200`
 * *   `num` contains only digits.
**/
@SuppressWarnings("java:S5413")
public class Solution {
    public List splitIntoFibonacci(String num) {
        List res = new ArrayList<>();
        solve(num, res, 0);
        return res;
    }

    private boolean solve(String s, List res, int idx) {
        if (idx == s.length() && res.size() >= 3) {
            return true;
        }
        for (int i = idx; i < s.length(); i++) {
            if (s.charAt(idx) == '0' && i > idx) {
                return false;
            }
            long num = Long.parseLong(s.substring(idx, i + 1));
            if (num > Integer.MAX_VALUE) {
                return false;
            }
            int size = res.size();
            if (size >= 2 && num > res.get(size - 1) + res.get(size - 2)) {
                return false;
            }
            if (size <= 1 || num == res.get(size - 1) + res.get(size - 2)) {
                res.add((int) num);
                if (solve(s, res, i + 1)) {
                    return true;
                }
                res.remove(res.size() - 1);
            }
        }
        return false;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy