首页 > 其他分享 >leetcode-944-easy

leetcode-944-easy

时间:2023-04-14 22:36:27浏览次数:34  
标签:strs 944 column int easy sorted leetcode columns delete

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.

思路一:双层循环遍历

    public int minDeletionSize(String[] strs) {
        int m = strs.length;
        int n = strs[0].length();

        int count = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 1; j < m; j++) {
                if (strs[j].charAt(i) < strs[j - 1].charAt(i)) {
                    count++;
                    break;
                }
            }
        }

        return count;
    }

标签:strs,944,column,int,easy,sorted,leetcode,columns,delete
From: https://www.cnblogs.com/iyiluo/p/17320136.html

相关文章

  • 代码随想录算法训练营Day01 | LeetCode704 二分查找、Leetcode27 移除元素
    今日学习的视频和文章代码随想录数组基础复习基础知识代码随想录二分查找代码随想录移除元素LeetCode704二分查找题目链接:704.二分查找-力扣(Leetcode)以前学二分查找的时候,真的一直搞不清楚怎么操作左边界和有边界,以及循环的终止条件是什么,总是自己慢慢调试出来,......
  • LeetCode 周赛 340,质数 / 前缀和 / 极大化最小值 / 最短路 / 平衡二叉树
    本文已收录到AndroidFamily,技术和职场问题,请关注公众号[彭旭锐]提问。大家好,我是小彭。上周跟大家讲到小彭文章风格的问题,和一些朋友聊过以后,至少在算法题解方面确定了小彭的风格。虽然竞赛算法题的文章受众非常小,但却有很多像我一样的初学者,他们有兴趣参加但容易被题目难......
  • 视频直播点播EasyDSS迁移至新服务器,启动正常但无法访问是什么原因?
    EasyDSS能实现视频流媒体的上传、转码、存储、录像、推拉流、直播、点播等功能,具备超低延迟、超高画质、超大并发访问量等特点,可应用在多样化的场景中,如:在线课堂、教育直播、校园活动直播、企业培训、游戏直播等。平台支持HTTP、HLS、RTMP等播出协议,并且兼容多终端,如:Windows、Andro......
  • 在EasyCVR中点击电子地图,出现快照不消失情况是什么原因?
    EasyCVR视频融合平台基于云边端一体化架构,部署轻快、功能灵活,平台可支持多协议、多类型设备接入,包括:国标GB28181、RTMP、RTSP/Onvif、海康Ehome、海康SDK、大华SDK、宇视SDK等(具体见下图)。在视频能力上,可实现视频直播、录像、回放、检索、云存储、告警上报、语音对讲、电子地图、集......
  • LeetCode 108.将有序数组转换成二叉搜索树
    1.题目:给你一个整数数组nums,其中元素已经按升序排列,请你将其转换为一棵高度平衡二叉搜索树。高度平衡二叉树是一棵满足「每个节点的左右两个子树的高度差的绝对值不超过1」的二叉树。示例1:输入:nums=[-10,-3,0,5,9]输出:[0,-3,9,-10,null,5]解释:[0,-10,5,null,-3,null,......
  • [LeetCode] 1440. Jump Game V 跳跃游戏之五
    Givenanarrayof integers arr andaninteger d.Inonestepyoucanjumpfromindex i toindex:i+x where: i+x<arr.length and 0< x<=d.i-x where: i-x>=0 and 0< x<=d.Inaddition,youcanonlyjumpfromindex i toi......
  • 【前缀和】LeetCode 1031. 两个非重叠子数组的最大和
    题目链接1031.两个非重叠子数组的最大和思路代码classSolution{publicintmaxSumTwoNoOverlap(int[]nums,intfirstLen,intsecondLen){//求一个前缀和for(inti=1;i<nums.length;++i){nums[i]+=nums[i-1];}......
  • [oeasy]python0133_[趣味拓展]颜文字_流石兄弟_表情文字_2ch_kaomoji
    颜文字回忆上次内容上次我们了解unicode里面有各种字体甚至还有emoji emoji本质上也是文字按照unicode的方式编码存储时按照utf-8的方式编码显示时按照系统定义的方式进行显示 还有什么好玩的亚文化吗?......
  • 【DP】【分治】LeetCode 53. 最大子数组和
    题目链接[https://leetcode.cn/problems/maximum-subarray/description/](53.最大子数组和"https://leetcode.cn/problems/maximum-subarray/description/")思路分析动态规划题目的时候只需要考虑最后一个阶段,因为所有的阶段转化都是相同的,考虑最后一个阶段容易发现规律在数......
  • 剑指 Offer 09. 用两个栈实现队列 && leetcode225.用队列实现栈
     剑指Offer09.用两个栈实现队列 classCQueue{private:stack<int>inStack,outStack;voidin2out(){//这里必须是while循环,如果是if判断,则输出栈日常只有一个值,没有起到先入后出的作用while(!inStack.empty()){//将输入栈栈顶......