Work with numbers. Consistency



  • Dana's sequence of chemical chips separated by a comma. We need to find a minimum negative sequence.

    Example for totals . 1.2,3,4,-1,-2,2,4,5,6,2,1,-2,-4,-5 - minimum -1-2.

    How do you do that?

    There's a code, his logic is the translation of all the numbers stat=0 into 0, the number PO0 to 1. Then process the row of the species 0000110000111, and look at the longest sequence of units.

    How do tie the line of the species 0000110000111 to the line of species 1.2,3,4,-1,-2,2,4,5,6,2,2,1,-2,-4,-5?

    We just need to get the numbers out of line two. And if there's two sequences of the same length, you have to choose the largest, which means you have to compare them.



  • public class Temp
    {
        private static class Pair
        {
            String sequence;
            Integer startIndex;
    
            public Pair(String s, Integer i)
            {
                this.sequence = s;
                this.startIndex = i;
            }
    
        }
    
        public static void main(String[] args)
        {
            int[] a = new int[]{1, 2, 3, 4, -1, -2, 2, 4, 5, 6, 2, 1, -2, -4, -5, 12, -1, -4};
            int[] b = new int[a.length];
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < a.length; i++)
            {
                b[i] = a[i] >= 0 ? 0 : 1;
                sb.append(b[i]);
            }
            //Получаем все последовательности
            List<Pair> list = getAllSequence(sb.toString());
            int min = list.get(0).sequence.length();
            //получаем список минимальных последовательностей
            List<Pair> newPair = list.stream().filter(p -> p.sequence.length() <= min).collect(Collectors.toList());
            Pair minPair = newPair.get(0);
            int minSum = getSum(minPair.startIndex, minPair.sequence.length(), a);
            //ищем последовательность с минимальной суммой
            for (int i = 1; i < newPair.size(); i++)
            {
                int w = getSum(newPair.get(i).startIndex, newPair.get(0).sequence.length(), a);
                if (w < minSum)
                {
                    minPair = newPair.get(i);
                }
            }
            int[] c = new int[minPair.sequence.length()];
            System.arraycopy(a, minPair.startIndex, c, 0, minPair.sequence.length());
            System.out.println(Arrays.toString(c)); // [-2, -4]
        }
    
        private static int getSum(int startIndex, int count, int[] a)
        {
            int s = 0;
            for (int i = 0; i < count; i++)
            {
                s += a[i + startIndex];
            }
            return s;
        }
    
        private static List<Pair> getAllSequence(String s)
        {
            List<Pair> list = new ArrayList<>();
            Matcher matcher = Pattern.compile("(1+)").matcher(s);
            int lastIndex = 0;
            while (matcher.find())
            {
                String group = matcher.group(1);
                int i = s.indexOf(group, lastIndex);
                lastIndex = i;
                list.add(new Pair(group, i));
            }
            return list;
        }
    }
    

    It may not be the simplest/best solution, but the working one.

    UPDATEStreamApi can be rewritten

    List<Pair> newPair = new ArrayList<>();
    

    for(Pair p : list) {
    if(p.sequence.length() <= min) {
    newPair.add(p);
    }
    }




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2