From 9f5356b1dbf2cf53962e972bf3710387f663d3ce Mon Sep 17 00:00:00 2001 From: Eat-Swap Date: Thu, 12 May 2022 19:46:03 +0800 Subject: [PATCH] add: 220512-CN [cpp] --- cpp/2205/220512-CN.cpp | 28 ++++++++++++++++++++++++++++ cpp/2205/CMakeLists.txt | 2 +- 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 cpp/2205/220512-CN.cpp diff --git a/cpp/2205/220512-CN.cpp b/cpp/2205/220512-CN.cpp new file mode 100644 index 0000000..8e0c7ee --- /dev/null +++ b/cpp/2205/220512-CN.cpp @@ -0,0 +1,28 @@ +#include +#include + +/** + * 944. Delete Columns to Make Sorted + * You are given an array of n strings strs, all of the same length. + * 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. + */ + +class Solution { +public: + static int minDeletionSize(const std::vector& s) { + auto n = s.front().length(), m = s.size(); + int cnt = 0; + while (n--) { + bool ok = true; + for (int i = m - 1; i--; ) { + if (s[i][n] > s[1 + i][n]) { + ok = false; + break; + } + } + cnt += !ok; + } + return cnt; + } +}; diff --git a/cpp/2205/CMakeLists.txt b/cpp/2205/CMakeLists.txt index a7eb06f..4a7eb9c 100644 --- a/cpp/2205/CMakeLists.txt +++ b/cpp/2205/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2205) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2205 220511.cpp) +ADD_EXECUTABLE(2205 220512-CN.cpp)