Sort by

recency

|

991 Discussions

|

  • + 0 comments

    Going from right to left, stop as soon as the predecessor character is greater than the current character. Then find the rightmost such such character and swap. Finally, sort the characters to the right of the current character. The effect of this procedure is to find the lowest possible lexicographic position which, when incremented by swapping, will be greater than the original string, and then to minimze all the lower positions. The resulting string is guaranteed to be lexicographically greater than the original, and any swap will be either greater than this string or smaller than the original string.

    def biggerIsGreater(w):
        w = list(w)
        right_end = len(w) - 1
        left, swapped = right_end - 1, False
        while not swapped and left > -1:
            right = left + 1
            if w[left] < w[right]:
                while right <= right_end and w[left] < w[right]:
                    right += 1
                w[left], w[right - 1], swapped = w[right - 1], w[left], True
            left -= 1
        if not swapped:
            return 'no answer'
        return ''.join(w[:left+2] + sorted(w[left+2:]))
    
  • + 0 comments

    RUST:

    fn biggerIsGreater(w: &str) -> String {
        let length = w.len();
        let mut temp = w.chars().collect::<Vec<char>>();
        let mut i = None; //
        let mut idx: Option<usize> = None;
        let mut smallest = 'z';
    
        for a in (0..length - 1).rev() {
            if temp[a] < temp[a + 1] {
                i = Some(a);
                break;
        }
    }
        if let Some(i) = i {
        let mut j = i + 1;
        for k in i + 1..length {
        if temp[k] > temp[i] && (j == i + 1 || temp[k] < temp[j]) {
            idx = Some(k);
        }
    }
            if let Some(j) = idx {
                temp.swap(i, j);
                temp[i + 1..].sort();
            }
            temp.iter().collect()
        } else {
            "no answer".into()
        }
    }
    
  • + 0 comments

    It's just The Narayana Panditha's algorithm - indian mathematician from XIV century

  • + 0 comments

    If you're looking for a bigger, better outdoor experience, a tilting outdoor umbrella is a must! It offers shade where you need it most and adjusts easily to keep you cool all day long. Perfect for those sunny afternoons or casual outdoor gatherings. You'll definitely enjoy the comfort and convenience!

  • + 0 comments

    Perl solution:

    sub sorted_descending {
        my $arr = shift;
        for (my $i = 0; $i < scalar(@$arr) - 1; $i++) {
            return 0 if ($arr->[$i] lt $arr->[$i + 1]);
        }
        return 1;
    }
    
    sub biggerIsGreater {
        my $w = shift;
        
        my @arr = split("", $w);
        my $max = $arr[1];
        my $ind = 0;
        my @res;
        if (sorted_descending(\@arr)) {
            return "no answer";
        } else {
            my $k = $#arr - 1;
            while ($k >= 0 && $arr[$k] ge $arr[$k + 1]) {
                $k--;
            }
            
            my $l = $#arr; 
            while ($arr[$l] le $arr[$k]) {
                $l--;
            }
            
            ($arr[$k], $arr[$l]) = ($arr[$l], $arr[$k]);
            push(@res, @arr[0..$k], reverse(splice(@arr, $k+1, $#arr)));
            
            return join("", @res);
        }
    }