Delete Columns to Make Sorted

🏠 ⬅️ ➑️

You are given an array of n strings strs, all of the same length.

The strings can be arranged such that there is one on each line, making a grid.

  • For example, strs = [ "abc ", "bce ", "cae "] can be arranged as follows:

abc bce cae

You want to delete the columns that are not sorted lexicographically. In the above example (0-indexed), columns 0 ('a', 'b', 'c') and 2 ('c', 'e', 'e') are sorted, while column 1 ('b', 'c', 'a') is not, so you would delete column 1.

Return the number of columns that you will delete.

Example 1:

Input: strs = [ "cba ", "daf ", "ghi "] Output: 1 Explanation: The grid looks as follows: cba daf ghi Columns 0 and 2 are sorted, but column 1 is not, so you only need to delete 1 column.

Example 2:

Input: strs = [ "a ", "b "] Output: 0 Explanation: The grid looks as follows: a b Column 0 is the only column and is sorted, so you will not delete any columns.

Example 3:

Input: strs = [ "zyx ", "wvu ", "tsr "] Output: 3 Explanation: The grid looks as follows: zyx wvu tsr All 3 columns are not sorted, so you will delete all 3.

Constraints:

  • n == strs.length
  • 1 <= n <= 100
  • 1 <= strs[i].length <= 1000
  • strs[i] consists of lowercase English letters.

Note: This problem is from LeetCode.
❌ Compiled
❌ Executed
❌ Correct
program main
    implicit none

    integer :: i, j, k, n, m, num_cols_to_delete
    character(len=100) :: strs(100)

    ! read input
    read(*,*) n
    do i = 1, n
        read(*,*) strs(i)
    end do

    ! solve problem
    num_cols_to_delete = 0
    do i = 1, n
        if (any(strs(i)(j+1:) < strs(i)(j:))) then
            num_cols_to_delete = num_cols_to_delete + 1
        end if
    end do

    ! print output
    write(*,*) num_cols_to_delete

end program main
❌ Compiled
❌ Executed
❌ Correct
program delete_unsorted_columns
      implicit none

      integer, parameter :: n = 3
      character(len=1000), dimension(n) :: strs
      integer :: i, j, k, count

      ! Initialize the array with the input strings
      do i = 1, n
         read (*, *) strs(i)
      end do

      ! Check if each column is sorted lexicographically
      do i = 1, n
         do j = 1, n-1
            if (strs(i)(j:j) > strs(i)(j+1:j+1)) then
               count = count + 1
               exit
            end if
         end do
      end do

      ! Print the number of columns that will be deleted
      print *, count

      end program delete_unsorted_columns

This program uses a parameter `n` to represent the number of strings in the array. It then declares an array `strs` of length `n` to store the input strings.

The program reads in the input strings using a `do` loop, and then checks if each column is sorted lexicographically using another `do` loop. If a column is not sorted, the program increments a counter `count`.

Finally, the program prints the value of `count` to stdout.

Note that this program assumes that the input strings are all the same length, and that the length of the strings is less than or equal to 1000. If the input strings can be of different lengths, or if the length of the strings is greater than 1000, then the program will need to be modified accordingly.
🌐 Data from online sources
def minDifference(nums, k):
    if len(nums) <= 1: return 0
    nums.sort()
    min_diff = nums[-1] - nums[0]
    for i in range(1, len(nums)):
        high = max(nums[i-1] + k, nums[-1] - k)
        low = min(nums[i] - k, nums[0] + k)
        min_diff = min(min_diff, high - low)
    return min_diff

The algorithm first sorts the input array nums. If there is only 1 element or no element in the array, then the minimum difference becomes 0. Otherwise, the algorithm initializes the minimum difference as the difference between the maximum and minimum elements. Then, it iterates through the nums array and for each element, selects the highest and lowest values after applying the operation with k. Update the minimum difference as necessary by comparing the current high-low value difference with the minimum difference calculated so far. The algorithm does not modify the original nums array but rather focuses on updating the minimum difference variable. After finishing the iteration, the minimum difference is returned.

🌐 Data from online sources
int minDifference(vector<int>& nums, int k) {
    if(nums.size() <= 1) return 0;
    sort(nums.begin(), nums.end());
    int min_diff = nums.back() - nums.front();
    for(int i=1; i<nums.size(); i++) {
        int high = max(nums[i-1] + k, nums.back() - k);
        int low = min(nums[i] - k, nums.front() + k);
        min_diff = min(min_diff, high - low);
    }
    return min_diff;
}

The algorithm first sorts the input array nums. If there is only 1 element or no element in the array, then the minimum difference becomes 0. Otherwise, the algorithm initializes the minimum difference as the difference between the maximum and minimum elements. Then, it iterates through the nums array and for each element, selects the highest and lowest values after applying the operation with k. Update the minimum difference as necessary by comparing the current high-low value difference with the minimum difference calculated so far. The algorithm does not modify the original nums array but rather focuses on updating the minimum difference variable. After finishing the iteration, the minimum difference is returned.