package com.fishercoder.solutions;

import com.fishercoder.common.classes.ListNode;

/**
 * 725. Split Linked List in Parts
 *
 * Given a (singly) linked list with head node root,
 * write a function to split the linked list into k consecutive linked list "parts".
 * The length of each part should be as equal as possible:
 * no two parts should have a size differing by more than 1. This may lead to some parts being null.
 * The parts should be in order of occurrence in the input list,
 * and parts occurring earlier should always have a size greater than or equal parts occurring later.
 * Return a List of ListNode's representing the linked list parts that are formed.

 Examples 1->2->3->4, k = 5 // 5 equal parts [ [1], [2], [3], [4], null ]

 Example 1:
 Input:
 root = [1, 2, 3], k = 5
 Output: [[1],[2],[3],[],[]]
 Explanation:
 The input and each element of the output are ListNodes, not arrays.
 For example, the input root has root.val = 1, root.next.val = 2, \root.next.next.val = 3, and root.next.next.next = null.
 The first element output[0] has output[0].val = 1, output[0].next = null.
 The last element output[4] is null, but it's string representation as a ListNode is [].

 Example 2:
 Input:
 root = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 3
 Output: [[1, 2, 3, 4], [5, 6, 7], [8, 9, 10]]
 Explanation:
 The input has been split into consecutive parts with size difference at most 1, and earlier parts are a larger size than the later parts.

 Note:
 The length of root will be in the range [0, 1000].
 Each value of a node in the input will be an integer in the range [0, 999].
 k will be an integer in the range [1, 50].

 */
public class _725 {
    public static class Solution1 {
        /**My very original solution, but verbose.*/
        public ListNode[] splitListToParts(ListNode root, int k) {
            int len = getLength(root);
            int aveSize = len / k;
            int extra = len % k;
            ListNode[] result = new ListNode[k];
            for (int i = 0; i < k; i++) {
                result[i] = root;
                int aveSizeTmp = aveSize;
                aveSizeTmp += extra-- > 0 ? 1 : 0;
                int aveSizeTmp2 = aveSizeTmp;
                while (aveSizeTmp-- > 0) {
                    root = root.next;
                }
                if (result[i] != null) {
                    ListNode tmp = result[i];
                    while (tmp.next != null && aveSizeTmp2-- > 1) {
                        tmp = tmp.next;
                    }
                    tmp.next = null;
                }
            }
            return result;
        }

        private int getLength(ListNode root) {
            int len = 0;
            ListNode tmp = root;
            while (tmp != null) {
                len++;
                tmp = tmp.next;
            }
            return len;
        }
    }

    public static class Solution2 {
        /**More concise version*/
        public ListNode[] splitListToParts(ListNode root, int k) {
            int len = getLength(root);
            int aveSize = len / k;
            int extra = len % k;
            ListNode[] result = new ListNode[k];
            ListNode prev = null;
            for (int i = 0; i < k; i++, extra--) {
                result[i] = root;
                for (int j = 0; j < aveSize + (extra > 0 ? 1 : 0); j++) {
                    prev = root;
                    root = root.next;
                }
                if (prev != null) {
                    prev.next = null;
                }
            }
            return result;
        }

        private int getLength(ListNode root) {
            int len = 0;
            ListNode tmp = root;
            while (tmp != null) {
                len++;
                tmp = tmp.next;
            }
            return len;
        }
    }
}

@@ -82,1572 +82,8 @@
 e;%0A%0A
-/**%0A * 725. Split Linked List in Parts%0A *%0A * Given a (singly) linked list with head node root,%0A * write a function to split the linked list into k consecutive linked list %22parts%22.%0A * The length of each part should be as equal as possible:%0A * no two parts should have a size differing by more than 1. This may lead to some parts being null.%0A * The parts should be in order of occurrence in the input list,%0A * and parts occurring earlier should always have a size greater than or equal parts occurring later.%0A * Return a List of ListNode's representing the linked list parts that are formed.%0A%0A Examples 1-%3E2-%3E3-%3E4, k = 5 // 5 equal parts %5B %5B1%5D, %5B2%5D, %5B3%5D, %5B4%5D, null %5D%0A%0A Example 1:%0A Input:%0A root = %5B1, 2, 3%5D, k = 5%0A Output: %5B%5B1%5D,%5B2%5D,%5B3%5D,%5B%5D,%5B%5D%5D%0A Explanation:%0A The input and each element of the output are ListNodes, not arrays.%0A For example, the input root has root.val = 1, root.next.val = 2, %5Croot.next.next.val = 3, and root.next.next.next = null.%0A The first element output%5B0%5D has output%5B0%5D.val = 1, output%5B0%5D.next = null.%0A The last element output%5B4%5D is null, but it's string representation as a ListNode is %5B%5D.%0A%0A Example 2:%0A Input:%0A root = %5B1, 2, 3, 4, 5, 6, 7, 8, 9, 10%5D, k = 3%0A Output: %5B%5B1, 2, 3, 4%5D, %5B5, 6, 7%5D, %5B8, 9, 10%5D%5D%0A Explanation:%0A The input has been split into consecutive parts with size difference at most 1, and earlier parts are a larger size than the later parts.%0A%0A Note:%0A The length of root will be in the range %5B0, 1000%5D.%0A Each value of a node in the input will be an integer in the range %5B0, 999%5D.%0A k will be an integer in the range %5B1, 50%5D.%0A%0A */%0A
 publ
@@ -133,16 +133,28 @@
 ution1 %7B
+%0A        /**
 %0A       
@@ -154,19 +154,19 @@
         
-/**
+ * 
 My very 
@@ -196,16 +196,26 @@
 verbose.
+%0A         
 */%0A     
@@ -1366,16 +1366,28 @@
      /**
+%0A         * 
 More con
@@ -1398,16 +1398,26 @@
  version
+%0A         
 */%0A     

refactor 725