Greedy algorithm--1

Greedy Algorithm

Greedy algorithm is to find the optimal solution in the current state, while dynamic programming is to find the optimal solution in all states, and dynamic programming can often be associated with recursion.

For example, find the least lexicographical sequence of non-repetitive characters in a string of lowercase letters
For example, the target string is cbacdcbc, and our resulting character sequence is acdb

We need to use greedy algorithms to solve this problem.
The key to this problem is how to know that the dictionary order is the smallest. The smallest dictionary order is in the order in which we look up the dictionary. The smaller the number of dictionaries that appear before, the smaller the number of them.
First, we need to go through the array to record the characters and their numbers. Then we will find out the smallest characters in the character ascll encoding, and finally, we will string out all the characters we find.

Every time we need to find the smallest character in the current string that won't make the string missing, so we can solve this problem by using loops or recursion.

Recursive version

String remove(String s){
        if(s.length()<=1)
            return s;
        int[] flag = new int[26];
        char[] letter = s.toCharArray();
        //Number of identifying letters
        for(char l: letter){
            flag[l-'a']++;
        }
        int local = 0;
        for(int i=0;i<s.length();i++){
            //Find the position of the smallest dictionary order, that is, the first position of the smallest letter.
            if(s.charAt(i)<s.charAt(local))
                local = i;
            //The condition for withdrawal is that all the letters that have appeared have appeared before, and no longer exist after.
            //local is the position of the smallest letter in the minimum dictionary order.
            /**
             * Here's the key
             */
            if(--flag[s.charAt(i)-'a']<=0)
                break;
        }
        //This method is to find the smallest letter in the minimum dictionary order.
        //So we use recursion to find each letter one by one.
        //Remove the previous minimum letter from the recursive parameter
        return s.charAt(local) + remove(s.substring(local + 1).replace("" + s.charAt(local), ""));
    }

Circular version

String solution(String s){
        int[] flag = new int[26];
        char[] letter = s.toCharArray();
        for(char l : letter){
            flag[l-'a']++;
        }
        int num = 0;
        for(int number : flag){
            if(number!=0)
                num++;
        }

        String str = "";

        for(int i=0; i< num;i++){
            int local = 0;
            for(int j=0;j<s.length();j++){
                if(s.charAt(local)>s.charAt(j)){
                    local = j;
                }
                if(--flag[s.charAt(j)-'a']<=0)
                    break;
            }
            str = str + s.charAt(local);
            s = s.substring(local + 1).replace(s.charAt(local) + "", "");
        }
        return str;
    }

Tags: Programming encoding

Posted on Wed, 09 Oct 2019 14:38:16 -0400 by huckfinne