From 88fe8cc916926752a034509ce5b048838bd54134 Mon Sep 17 00:00:00 2001 From: Lam Haoyin Date: Mon, 21 Mar 2022 15:10:54 +0800 Subject: [PATCH] add: 220321 [cpp] --- cpp/2203/220321.cpp | 43 +++++++++++++++++++++++++++++++++++++++++ cpp/2203/CMakeLists.txt | 2 +- 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 cpp/2203/220321.cpp diff --git a/cpp/2203/220321.cpp b/cpp/2203/220321.cpp new file mode 100644 index 0000000..41ba05c --- /dev/null +++ b/cpp/2203/220321.cpp @@ -0,0 +1,43 @@ +#include +#include +#include + +/** + * 763. Partition Labels + * You are given a string s. We want to partition the string into as many parts as possible so that each letter appears in at most one part. + * Note that the partition is done so that after concatenating all the parts in order, the resultant string should be s. + * Return a list of integers representing the size of these parts. + */ + +class Solution { +public: + static std::vector partitionLabels(const std::string& s) { + int n = s.length(), lastSeen[26]{}; + for (int i = 0; i < n; ++i) + lastSeen[s[i] - 'a'] = i; + + std::vector ret; + + // Each loop, Select [L, R]. + for (int i = 0; i < n;) { + int L = i, R = i; + for (int j = i; j <= R; ++j) { + if (lastSeen[s[j] - 'a'] > j) { + R = std::max(R, lastSeen[s[j] - 'a']); + } + } + ret.push_back(R - L + 1); + i = R + 1; + } + + return ret; + } +}; + +int main() { + // auto ret = Solution::partitionLabels("ababcbacadefegdehijhklij"); + auto ret = Solution::partitionLabels("eccbbbbdec"); + for (int i : ret) + std::cout << i << ' '; + return 0; +} diff --git a/cpp/2203/CMakeLists.txt b/cpp/2203/CMakeLists.txt index 88e3414..f8b6e44 100644 --- a/cpp/2203/CMakeLists.txt +++ b/cpp/2203/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2203) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2203 220320-CN.cpp) +ADD_EXECUTABLE(2203 220321.cpp)