From be65fbb53973d69c9f2bdb1162e64a94cebedf9d Mon Sep 17 00:00:00 2001 From: Vinay Harwani Date: Wed, 12 Oct 2022 13:00:48 +0530 Subject: [PATCH] Added leetcode solutions for some problems --- .../322-coin-change/322-coin-change.cpp | 35 +++++++++++++++++++ ...ongest-repeating-character-replacement.cpp | 33 +++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 CPP/ProblemsAndSolutions/leetcode_question_solution/Leet_Code_Submission-main/322-coin-change/322-coin-change.cpp create mode 100644 CPP/ProblemsAndSolutions/leetcode_question_solution/Leet_Code_Submission-main/424-longest-repeating-character-replacement/424-longest-repeating-character-replacement.cpp diff --git a/CPP/ProblemsAndSolutions/leetcode_question_solution/Leet_Code_Submission-main/322-coin-change/322-coin-change.cpp b/CPP/ProblemsAndSolutions/leetcode_question_solution/Leet_Code_Submission-main/322-coin-change/322-coin-change.cpp new file mode 100644 index 0000000..df6f9f4 --- /dev/null +++ b/CPP/ProblemsAndSolutions/leetcode_question_solution/Leet_Code_Submission-main/322-coin-change/322-coin-change.cpp @@ -0,0 +1,35 @@ +class Solution +{ +public: + int coinChange(vector &a, int amount) + { + if (amount == 0) + { + return 0; + } + vector dp(amount + 1, 100001); + int n = a.size(); + for (int i = 0; i < n; i++) + { + if (a[i] <= amount) + { + dp[a[i]] = 1; + } + } + for (int tar = 1; tar <= amount; tar++) + { + for (int j = 0; j < n; j++) + { + if (a[j] < tar && (tar - a[j]) <= amount) + { + dp[tar] = min(dp[tar], dp[tar - a[j]] + 1); + } + } + } + if (dp[amount] == 100001) + { + return -1; + } + return dp[amount]; + } +}; \ No newline at end of file diff --git a/CPP/ProblemsAndSolutions/leetcode_question_solution/Leet_Code_Submission-main/424-longest-repeating-character-replacement/424-longest-repeating-character-replacement.cpp b/CPP/ProblemsAndSolutions/leetcode_question_solution/Leet_Code_Submission-main/424-longest-repeating-character-replacement/424-longest-repeating-character-replacement.cpp new file mode 100644 index 0000000..b7596a6 --- /dev/null +++ b/CPP/ProblemsAndSolutions/leetcode_question_solution/Leet_Code_Submission-main/424-longest-repeating-character-replacement/424-longest-repeating-character-replacement.cpp @@ -0,0 +1,33 @@ +class Solution +{ +public: + int characterReplacement(string s, int k) + { + int n = s.length(); + int ans = 0; + for (int i = 0; i < 26; i++) + { + char c = i + 'A'; + int start = 0, end = 0; + int count = 0; + while (end < n) + { + if (s[end] != c) + { + count++; + } + while (count > k) + { + if (s[start] != c) + { + count--; + } + start++; + } + ans = max(end - start + 1, ans); + end++; + } + } + return ans; + } +}; \ No newline at end of file