From 84a29e7114b8f5145a0bf7238b1af869fd062d53 Mon Sep 17 00:00:00 2001 From: Nehal Ali <42276697+nehalalifayed@users.noreply.github.com> Date: Thu, 6 Oct 2022 17:51:45 +0200 Subject: [PATCH 1/2] Create 1299-Replace-Elements-with-Greatest-Element-on-Right-Side.cpp --- ...ents-with-Greatest-Element-on-Right-Side.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 cpp/1299-Replace-Elements-with-Greatest-Element-on-Right-Side.cpp diff --git a/cpp/1299-Replace-Elements-with-Greatest-Element-on-Right-Side.cpp b/cpp/1299-Replace-Elements-with-Greatest-Element-on-Right-Side.cpp new file mode 100644 index 000000000..0116ad06a --- /dev/null +++ b/cpp/1299-Replace-Elements-with-Greatest-Element-on-Right-Side.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + vector replaceElements(vector& arr) { + // O(N) Time Complexity , O(1) Space complexity + int n = arr.size(); + int maxSoFar = arr[n-1]; + arr[n-1] = -1; + + for(int i=n-2;i>=0;i--) + { + int temp = maxSoFar; + if(maxSoFar < arr[i]) maxSoFar = arr[i]; + arr[i] = temp; + } + return arr; + } +}; From 1e8a8aa96386c4b7a92e44e2dc835ba618d22fc7 Mon Sep 17 00:00:00 2001 From: Nehal Ali <42276697+nehalalifayed@users.noreply.github.com> Date: Thu, 6 Oct 2022 18:16:04 +0200 Subject: [PATCH 2/2] Create 392-Is-Subsequence.cpp Add C++ Solution to leetcode problem no.392 --- cpp/392-Is-Subsequence.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 cpp/392-Is-Subsequence.cpp diff --git a/cpp/392-Is-Subsequence.cpp b/cpp/392-Is-Subsequence.cpp new file mode 100644 index 000000000..7bf6aa9df --- /dev/null +++ b/cpp/392-Is-Subsequence.cpp @@ -0,0 +1,19 @@ +// Time Complexity is O(N) where n is the size of the target string. +// Space Complexity is O(1) + +class Solution { +public: + bool isSubsequence(string s, string t) { + int i = 0 , j = 0; + while(j < s.size() && i < t.size()) + { + if(s[j] == t[i]) + j++; + + i++; + } + + if(j >= s.size()) return true; + return false; + } +};