From 819a76b55d0d6aeb43335a08bbd1aa5c918fa5c5 Mon Sep 17 00:00:00 2001 From: Anomasingh <118592802+Anomasingh@users.noreply.github.com> Date: Sat, 20 Jan 2024 19:23:46 +0000 Subject: [PATCH 1/6] solved Day-18/q2 --- .../Anomasingh--C.md | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Day-18/q2: Search in Rotated Sorted Array/Anomasingh--C.md diff --git a/Day-18/q2: Search in Rotated Sorted Array/Anomasingh--C.md b/Day-18/q2: Search in Rotated Sorted Array/Anomasingh--C.md new file mode 100644 index 00000000..02568372 --- /dev/null +++ b/Day-18/q2: Search in Rotated Sorted Array/Anomasingh--C.md @@ -0,0 +1,40 @@ +class Solution { +public: + int search(std::vector& nums, int target) + { + int l = 0; + int h = nums.size() - 1; + + while (l <= h) + { + int m = l + (h - l) / 2; + + if (nums[m] == target) + { + return m; + } + + if (nums[l] <= nums[m]) + { + if (nums[l] <= target && target < nums[m]) + { + h = m - 1; + } else + { + l = m + 1; + } + } + else + { + if (nums[m] < target && target <= nums[h]) + { + l = m + 1; + } else { + h = m - 1; + } + } + } + + return -1; + } +}; \ No newline at end of file From 8f2e3c14cc013f0b7c022fb143ef2e43ccab7b1d Mon Sep 17 00:00:00 2001 From: Anomasingh <118592802+Anomasingh@users.noreply.github.com> Date: Sun, 21 Jan 2024 12:20:01 +0000 Subject: [PATCH 2/6] Solved Day-18/q2 Search In Rotated Sorted Array Issue 409 --- .../Anomasingh--C.md | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/Day-18/q2: Search in Rotated Sorted Array/Anomasingh--C.md b/Day-18/q2: Search in Rotated Sorted Array/Anomasingh--C.md index 02568372..97a9a23a 100644 --- a/Day-18/q2: Search in Rotated Sorted Array/Anomasingh--C.md +++ b/Day-18/q2: Search in Rotated Sorted Array/Anomasingh--C.md @@ -1,3 +1,8 @@ +Solution in C++ + +Solved using Binary Search-- + +Code: class Solution { public: int search(std::vector& nums, int target) @@ -7,16 +12,16 @@ public: while (l <= h) { - int m = l + (h - l) / 2; + int m = l + (h - l) / 2; //Middle element in a array - if (nums[m] == target) + if (nums[m] == target) //When middle element is the target element { return m; } - if (nums[l] <= nums[m]) + if (nums[l] <= nums[m]) //When last element is smaller than mid element { - if (nums[l] <= target && target < nums[m]) + if (nums[l] <= target && target < nums[m]) { h = m - 1; } else @@ -26,7 +31,7 @@ public: } else { - if (nums[m] < target && target <= nums[h]) + if (nums[m] < target && target <= nums[h]) // When middle element is less than target element { l = m + 1; } else { From 3440da5b4df2cda84aa1d5d2af98e8da912be53d Mon Sep 17 00:00:00 2001 From: Anomasingh <118592802+Anomasingh@users.noreply.github.com> Date: Tue, 23 Jan 2024 18:40:31 +0000 Subject: [PATCH 3/6] Solved Day26 q2: Score of Parentheses Issue#473 --- .../q2: Score of Parentheses/Anomasingh--C.md | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Day-26/q2: Score of Parentheses/Anomasingh--C.md diff --git a/Day-26/q2: Score of Parentheses/Anomasingh--C.md b/Day-26/q2: Score of Parentheses/Anomasingh--C.md new file mode 100644 index 00000000..001cbeee --- /dev/null +++ b/Day-26/q2: Score of Parentheses/Anomasingh--C.md @@ -0,0 +1,23 @@ +Solution in C++ -> + +class Solution { +public: + int scoreOfParentheses(string s) { + stackst; + int score = 0; + for(auto ch: s) //checking every character + { + if(ch =='(') // if there is open parentheses + { + st.push(score); + score = 0; //Score will be 0 + } + else + { + score = st.top()+max(2*score,1); //condition given in question to calculate score + st.pop(); + } + } + return score; + } +}; \ No newline at end of file From e79be9e2a2efb6beab5d7d8130bf80526f7d5cde Mon Sep 17 00:00:00 2001 From: Anomasingh <118592802+Anomasingh@users.noreply.github.com> Date: Tue, 23 Jan 2024 18:41:57 +0000 Subject: [PATCH 4/6] Solved Day26 q2: Score of Parentheses Issue#473 --- Day-26/q2: Score of Parentheses/question.md | 27 +++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Day-26/q2: Score of Parentheses/question.md diff --git a/Day-26/q2: Score of Parentheses/question.md b/Day-26/q2: Score of Parentheses/question.md new file mode 100644 index 00000000..fe7296a8 --- /dev/null +++ b/Day-26/q2: Score of Parentheses/question.md @@ -0,0 +1,27 @@ + +Given a balanced parentheses string s, return the score of the string. + +The score of a balanced parentheses string is based on the following rule: + +"()" has score 1. +AB has score A + B, where A and B are balanced parentheses strings. +(A) has score 2 * A, where A is a balanced parentheses string. +Example 1: +Input: s = "()" +Output: 1 + +Example 2: +Input: s = "(())" +Output: 2 + +Example 3: +Input: s = "()()" +Output: 2 + +Constraints: +2 <= s.length <= 50 +s consists of only '(' and ')'. +s is a balanced parentheses string. + +Problem link-> +https://leetcode.com/problems/score-of-parentheses/ \ No newline at end of file From ec938246f019e2d53f928e6957da08bcf34f5750 Mon Sep 17 00:00:00 2001 From: Anomasingh <118592802+Anomasingh@users.noreply.github.com> Date: Tue, 23 Jan 2024 18:57:39 +0000 Subject: [PATCH 5/6] deleted day 26 q2 --- .../q2: Score of Parentheses/Anomasingh--C.md | 23 ---------------- Day-26/q2: Score of Parentheses/question.md | 27 ------------------- 2 files changed, 50 deletions(-) delete mode 100644 Day-26/q2: Score of Parentheses/Anomasingh--C.md delete mode 100644 Day-26/q2: Score of Parentheses/question.md diff --git a/Day-26/q2: Score of Parentheses/Anomasingh--C.md b/Day-26/q2: Score of Parentheses/Anomasingh--C.md deleted file mode 100644 index 001cbeee..00000000 --- a/Day-26/q2: Score of Parentheses/Anomasingh--C.md +++ /dev/null @@ -1,23 +0,0 @@ -Solution in C++ -> - -class Solution { -public: - int scoreOfParentheses(string s) { - stackst; - int score = 0; - for(auto ch: s) //checking every character - { - if(ch =='(') // if there is open parentheses - { - st.push(score); - score = 0; //Score will be 0 - } - else - { - score = st.top()+max(2*score,1); //condition given in question to calculate score - st.pop(); - } - } - return score; - } -}; \ No newline at end of file diff --git a/Day-26/q2: Score of Parentheses/question.md b/Day-26/q2: Score of Parentheses/question.md deleted file mode 100644 index fe7296a8..00000000 --- a/Day-26/q2: Score of Parentheses/question.md +++ /dev/null @@ -1,27 +0,0 @@ - -Given a balanced parentheses string s, return the score of the string. - -The score of a balanced parentheses string is based on the following rule: - -"()" has score 1. -AB has score A + B, where A and B are balanced parentheses strings. -(A) has score 2 * A, where A is a balanced parentheses string. -Example 1: -Input: s = "()" -Output: 1 - -Example 2: -Input: s = "(())" -Output: 2 - -Example 3: -Input: s = "()()" -Output: 2 - -Constraints: -2 <= s.length <= 50 -s consists of only '(' and ')'. -s is a balanced parentheses string. - -Problem link-> -https://leetcode.com/problems/score-of-parentheses/ \ No newline at end of file From f452b72e724e712898b156cd700f6f3685010874 Mon Sep 17 00:00:00 2001 From: Anomasingh <118592802+Anomasingh@users.noreply.github.com> Date: Sat, 27 Jan 2024 18:36:11 +0000 Subject: [PATCH 6/6] Solved Day-26 q3: Find first and last position of element in sorted array in c++ --- .../Anomasingh--C.md | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 Day-26/q3: Find first and last position of element in sorted array/Anomasingh--C.md diff --git a/Day-26/q3: Find first and last position of element in sorted array/Anomasingh--C.md b/Day-26/q3: Find first and last position of element in sorted array/Anomasingh--C.md new file mode 100644 index 00000000..b9c4d64a --- /dev/null +++ b/Day-26/q3: Find first and last position of element in sorted array/Anomasingh--C.md @@ -0,0 +1,61 @@ +``` +Solution: +In c++ + +#include +using namespace std; +int firstOcc(vector& arr, int n, int key) { // finding the first occurrence of that element + + int s = 0, e = n-1; + int mid = s + (e-s)/2; + int ans = -1; + while(s<=e) { + + if(arr[mid] == key){ + ans = mid; + e = mid - 1; + } + else if(key > arr[mid]) { //Shifting to right + s = mid + 1; + } + else if(key < arr[mid]) { //Shifting to left + e = mid - 1; + } + + mid = s + (e-s)/2; + } + return ans; +} + +int lastOcc(vector& arr, int n, int key) { //finding the last occurrence of that element + + int s = 0, e = n-1; + int mid = s + (e-s)/2; + int ans = -1; + while(s<=e) { + + if(arr[mid] == key){ + ans = mid; + s = mid + 1; + } + else if(key > arr[mid]) { // Shifting to right + s = mid + 1; + } + else if(key < arr[mid]) { //Shifting to left + e = mid - 1; + } + + mid = s + (e-s)/2; + } + return ans; +} + +pair firstAndLastPosition(vector& arr, int n, int k) +{ + pair p; + p.first = firstOcc(arr, n, k); + p.second = lastOcc(arr, n, k); + + return p; +} +``` \ No newline at end of file