LeetCode 842. Split Array into Fibonacci Sequence

原题链接在这里:https://leetcode.com/problems/split-array-into-fibonacci-sequence/

题目:

Given a string S of digits, such as S = "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] <= 2^31 - 1, (that is, each integer fits 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.

Also, 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 S, or return [] if it cannot be done.

Example 1:

Input: "123456579"
Output: [123,456,579]

Example 2:

Input: "11235813"
Output: [1,1,2,3,5,8,13]

Example 3:

Input: "112358130"
Output: []
Explanation: The task is impossible.

Example 4:

Input: "0123"
Output: []
Explanation: Leading zeroes are not allowed, so "01", "2", "3" is not valid.

Example 5:

Input: "1101111"
Output: [110, 1, 111]
Explanation: The output [11, 0, 11, 11] would also be accepted.

Note:

  1. 1 <= S.length <= 200
  2. S contains only digits.

题解:

The quesiton is asking for any sequence, not all of them.

Thus DFS state needs s, current starting index, current list item. DFS returns if s could be cut into Fibonacci sequence.

If any DFS comes to true, then just return current list, there is no need to do further more calculation.

For i >= start index, get the candidate from starting index, if it exceeds Integer, return false.

If current list item already has >= 2 items, but candidate != last 2 values sum, return false.

If current list item has 0 or 1 item, or has >= 2 items, but candidate == last 2 values sum, add candidate to res and continue DFS.

When gettting candidate, starting index could point to ‘0‘, ‘0‘ as candidate is fine, but ‘01‘ is not.

Thus here it needs to check when i > start && s.charAt(start) == ‘0‘, return false.

Time Complexity: O(expontenial).

Space: O(n). n = s.length(). stack space.

AC Java:

 1 class Solution {
 2     public List<Integer> splitIntoFibonacci(String S) {
 3         List<Integer> res = new ArrayList<>();
 4         if(S == null || S.length() == 0){
 5             return res;
 6         }
 7
 8         dfs(S, 0, res);
 9         return res;
10     }
11
12     private boolean dfs(String s, int start, List<Integer> res){
13         if(start == s.length() && res.size() > 2){
14             return true;
15         }
16
17         for(int i = start; i<s.length(); i++){
18             if(s.charAt(start) == ‘0‘ && i > start){
19                 return false;
20             }
21
22             long candidate = Long.valueOf(s.substring(start, i+1));
23             if(candidate > Integer.MAX_VALUE){
24                 return false;
25             }
26
27             int size = res.size();
28             if(size >= 2 && candidate > res.get(size-1)+res.get(size-2)){
29                 return false;
30             }
31
32             if(size < 2 || candidate == res.get(size-1)+res.get(size-2)){
33                 res.add((int)candidate);
34                 if(dfs(s, i+1, res)){
35                     return true;
36                 }
37
38                 res.remove(res.size()-1);
39             }
40         }
41
42         return false;
43     }
44 }

原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/11829368.html

时间: 2024-10-03 19:38:34

LeetCode 842. Split Array into Fibonacci Sequence的相关文章

842. Split Array into Fibonacci Sequence —— weekly contest 86

题目链接:https://leetcode.com/problems/split-array-into-fibonacci-sequence/description/ 占坑. string 的数值转换函数(c++11)介绍 :https://blog.csdn.net/calmreason/article/details/41204211 答案可参考:https://leetcode.com/problems/split-array-into-fibonacci-sequence/discuss

[Swift]LeetCode842. 将数组拆分成斐波那契序列 | Split Array into Fibonacci Sequence

Given a string S of digits, such as S = "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] <= 2^31 - 1, (that is, each

[LeetCode] 659. Split Array into Consecutive Subsequences 将数组分割成连续子序列

You are given an integer array sorted in ascending order (may contain duplicates), you need to split them into several subsequences, where each subsequences consist of at least 3 consecutive integers. Return whether you can make such a split. Example

Leetcode 410. Split Array Largest Sum

Problem: Given an array which consists of non-negative integers and an integer m, you can split the array into m non-empty continuous subarrays. Write an algorithm to minimize the largest sum among these m subarrays. Note: If n is the length of array

leetcode 659. Split Array into Consecutive Subsequences

You are given an integer array sorted in ascending order (may contain duplicates), you need to split them into several subsequences, where each subsequences consist of at least 3 consecutive integers. Return whether you can make such a split. Example

LeetCode 548. Split Array with Equal Sum (分割数组使得子数组的和都相同)$

Given an array with n integers, you need to find if there are triplets (i, j, k) which satisfies following conditions: 0 < i, i + 1 < j, j + 1 < k < n - 1 Sum of subarrays (0, i - 1), (i + 1, j - 1), (j + 1, k - 1) and (k + 1, n - 1) should be

[LeetCode] 805. Split Array With Same Average 用相同均值拆分数组

In a given integer array A, we must move every element of A to either list B or list C. (B and C initially start empty.) Return true if and only if after such a move, it is possible that the average value of B is equal to the average value of C, and

LeetCode 659: Split Array into Consecutive Subsequence

Note: 1. If it does not belong any sequences : append.getOrDefault(num, 0) == 0, create a new sequence. It requires num + 1 and num + 2 count > 0. 2. Once it has sequence, move sequece to num + 1. class Solution { public boolean isPossible(int[] nums

[LeetCode] Merge Sorted Array [22]

题目 Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B. The number of elements initialized in A and B ar