diff --git a/Challenge-1/two_sum.cpp b/Challenge-1/two_sum.cpp new file mode 100644 index 0000000..9a936b8 --- /dev/null +++ b/Challenge-1/two_sum.cpp @@ -0,0 +1,14 @@ +class Solution { +public: + vector twoSum(vector& nums, int target) { + unordered_map mpp; + for (int i=0; i& nums, int target) { + + int low = 0; + int high = nums.size()-1; + while(low<=high){ + int mid = (low + high)/2; + if (nums[mid]==target) return mid; + if (nums[low]<= nums[mid]){ + if (nums[low]<= target && nums[mid]>target){ + high = mid-1; + } + else { + low = mid+1; + } + } + else{ + if(nums[mid]=target){ + low = mid+1; + } + else{ + high = mid-1; + } + } + } + return -1; + + } +}; \ No newline at end of file diff --git a/Challenge-11/3sum.cpp b/Challenge-11/3sum.cpp new file mode 100644 index 0000000..cc7083c --- /dev/null +++ b/Challenge-11/3sum.cpp @@ -0,0 +1,33 @@ +class Solution { +public: + vector> threeSum(vector& nums) { + + int n = nums.size(); + sort(nums.begin(),nums.end()); + vector> res; + for (int i=0; i0 && nums[i]==nums[i-1]) continue; + int j = i + 1; + int k = n - 1; + while (j0){ + k--; + } + else { + vector ans = {nums[i],nums[j],nums[k]}; + res.push_back(ans); + j++; + k--; + while(j& height) { + int left = 0; + int right = height.size()-1; + int maxVol = 0; + + for (int i = left;i0){ + n &= (n-1); + count ++; + } + + return count; + + } +}; \ No newline at end of file diff --git a/Challenge-14/counting_bits.cpp b/Challenge-14/counting_bits.cpp new file mode 100644 index 0000000..da15262 --- /dev/null +++ b/Challenge-14/counting_bits.cpp @@ -0,0 +1,10 @@ +class Solution { +public: + vector countBits(int n) { + vector ans(n+1); + for (int i=0;i<=n;i++){ + ans[i]=ans[i/2]+(i%2); + } + return ans; + } +}; \ No newline at end of file diff --git a/Challenge-15/missing_no.cpp b/Challenge-15/missing_no.cpp new file mode 100644 index 0000000..8ea65f7 --- /dev/null +++ b/Challenge-15/missing_no.cpp @@ -0,0 +1,14 @@ +class Solution { +public: + int missingNumber(vector& nums) { + int n = nums.size(); + int sum = (n*(n+1))/2; + int curr = 0; + + for (int num: nums){ + curr+=num; + } + return sum-curr; + + } +}; \ No newline at end of file diff --git a/Challenge-16/reverse_bits.cpp b/Challenge-16/reverse_bits.cpp new file mode 100644 index 0000000..5c7923f --- /dev/null +++ b/Challenge-16/reverse_bits.cpp @@ -0,0 +1,12 @@ +class Solution { +public: + uint32_t reverseBits(uint32_t n) { + uint32_t res = 0; + for (int i=0;i<32;i++){ + res = (res << 1) | (n&1); + n>>=1; + } + return res; + + } +}; \ No newline at end of file diff --git a/Challenge-17/climbing_stairs.cpp b/Challenge-17/climbing_stairs.cpp new file mode 100644 index 0000000..d483ae4 --- /dev/null +++ b/Challenge-17/climbing_stairs.cpp @@ -0,0 +1,15 @@ +class Solution { +public: + int climbStairs(int n) { + + static int memo[46] = {0}; + + if (n==1)return 1; + if(n==2)return 2; + if(memo[n]!=0)return memo[n]; + + memo[n] = climbStairs(n-1) + climbStairs(n-2); + return memo[n]; + + } +}; \ No newline at end of file diff --git a/Challenge-18/coin_change.cpp b/Challenge-18/coin_change.cpp new file mode 100644 index 0000000..f7c7c79 --- /dev/null +++ b/Challenge-18/coin_change.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + int coinChange(vector& coins, int amount) { + vector dp(amount + 1, INT_MAX); + dp[0] = 0; + + for (int coin : coins) { + for (int i = coin; i <= amount; i++) { + if (dp[i - coin] != INT_MAX) { + dp[i] = min(dp[i], dp[i - coin] + 1); + } + } + } + return (dp[amount] == INT_MAX) ? -1 : dp[amount]; + } +}; diff --git a/Challenge-19/longest_incr_subseq.cpp b/Challenge-19/longest_incr_subseq.cpp new file mode 100644 index 0000000..5a5fcea --- /dev/null +++ b/Challenge-19/longest_incr_subseq.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + int lengthOfLIS(vector& nums) { + int n = nums.size(); + vector temp; + temp.push_back(nums[0]); + + for (int i=1;itemp.back()){ + temp.push_back(nums[i]); + }else{ + int idx = lower_bound(temp.begin(),temp.end(),nums[i])-temp.begin(); + temp[idx] = nums[i]; + } + } + + return temp.size(); + + + } +}; \ No newline at end of file diff --git a/Challenge-2/two_sum.cpp b/Challenge-2/two_sum.cpp new file mode 100644 index 0000000..162745e --- /dev/null +++ b/Challenge-2/two_sum.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + vector twoSum(vector& nums, int target) { + + unordered_map mpp; + for (int i=0;i> &dp){ + if(i<0 || j<0) return 0; + + if(dp[i][j]!=-1) return dp[i][j]; + if(text1[i]==text2[j]) return dp[i][j] = 1 + f(i-1,j-1,text1,text2,dp); + + return dp[i][j] = max(f(i-1,j,text1,text2,dp),f(i,j-1,text1,text2,dp)); + } + + + int longestCommonSubsequence(string text1, string text2) { + + int n = text1.size(); + int m = text2.size(); + + vector>dp(n,vector(m,-1)); + + return f(n-1,m-1,text1,text2,dp); + + } +}; \ No newline at end of file diff --git a/Challenge-21/word_break.cpp b/Challenge-21/word_break.cpp new file mode 100644 index 0000000..8f79f62 --- /dev/null +++ b/Challenge-21/word_break.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + int dp[301]; + + int helper(int i, string s, set&wordDict){ + + if(s.size()==i) return 1; + string temp; + if(dp[i]!=-1) return dp[i]; + for (int j= i;j& wordDict) { + + setst; + memset(dp, -1, sizeof dp); + for (auto a:wordDict) st.insert(a); + return helper(0,s,st); + } +}; \ No newline at end of file diff --git a/Challenge-22/combination_sum_iv.cpp b/Challenge-22/combination_sum_iv.cpp new file mode 100644 index 0000000..5d3ec79 --- /dev/null +++ b/Challenge-22/combination_sum_iv.cpp @@ -0,0 +1,25 @@ +class Solution { +public: + int combinationSum4(vector& nums, int target) { + + vectordp (target+1,-1); + dp[0]=1; + return helper(nums,target,dp); + + } + + int helper (vector&nums, int target, vector& dp){ + if(dp[target]!=-1) return dp[target]; + + int res = 0; + for (int num : nums){ + if (target>=num){ + res += helper(nums,target-num, dp); + } + } + + dp[target] = res; + return res; + + } +}; \ No newline at end of file diff --git a/Challenge-23/house_robber.cpp b/Challenge-23/house_robber.cpp new file mode 100644 index 0000000..8799f87 --- /dev/null +++ b/Challenge-23/house_robber.cpp @@ -0,0 +1,15 @@ +class Solution { +public: + int rob(vector& nums) { + + int pre1,pre2 = 0; + for(int num : nums){ + int temp = pre1; + pre1 = max ((num + pre2),pre1); + pre2 = temp; + + } + return pre1; + + } +}; \ No newline at end of file diff --git a/Challenge-24/house_robber2.cpp b/Challenge-24/house_robber2.cpp new file mode 100644 index 0000000..1169158 --- /dev/null +++ b/Challenge-24/house_robber2.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + + int helper(vector &nums,int start, int end){ + int prev1 =0, prev2 = 0; + for (int i=start; i<=end; i++){ + int temp = prev1; + prev1 = max(prev2 + nums[i], prev1); + prev2 = temp; + } + return prev1; + } + int rob(vector& nums) { + + int n = nums.size(); + if (n==0) return 0; + if (n==1) return nums[0]; + if (n==2) return max (nums[0],nums[1]); + + return max(helper(nums,0,n-2), helper(nums,1,n-1)); + + + } +}; \ No newline at end of file diff --git a/Challenge-25/decode_ways.cpp b/Challenge-25/decode_ways.cpp new file mode 100644 index 0000000..6ab3455 --- /dev/null +++ b/Challenge-25/decode_ways.cpp @@ -0,0 +1,23 @@ +int numDecodings(string s) { + if (s.empty() || s[0] == '0') return 0; + + int prev = 1, curr = 1; + + for (int i = 1; i < s.length(); i++) { + int temp = 0; + + + if (s[i] != '0') { + temp += curr; + } + int twoDigit = stoi(s.substr(i - 1, 2)); + if (twoDigit >= 10 && twoDigit <= 26) { + temp += prev; + } + + prev = curr; + curr = temp; + } + + return curr; +} diff --git a/Challenge-26/unique_paths.cpp b/Challenge-26/unique_paths.cpp new file mode 100644 index 0000000..92d597f --- /dev/null +++ b/Challenge-26/unique_paths.cpp @@ -0,0 +1,18 @@ +class Solution { +public: + + int helper(int i,int j, vector> &dp){ + if (i==0 && j==0) return 1; + if (i<0 || j<0) return 0; + if(dp[i][j]!=-1) return dp[i][j]; + int up = helper(i,j-1,dp); + int left = helper(i-1,j,dp); + return dp[i][j] = up + left; + } + + int uniquePaths(int m, int n) { + vector> dp (m, vector (n, -1)); + return helper(m-1,n-1,dp); + + } +}; \ No newline at end of file diff --git a/Challenge-27/jump_game.cpp b/Challenge-27/jump_game.cpp new file mode 100644 index 0000000..97ec9f1 --- /dev/null +++ b/Challenge-27/jump_game.cpp @@ -0,0 +1,13 @@ +class Solution { +public: + bool canJump(vector& nums) { + + int maxIdx = 0; + for (int i=0; imaxIdx) return false; + maxIdx = max(maxIdx, i+nums[i]); + } + return true; + + } +}; \ No newline at end of file diff --git a/Challenge-28/clone_graph.cpp b/Challenge-28/clone_graph.cpp new file mode 100644 index 0000000..7399bfe --- /dev/null +++ b/Challenge-28/clone_graph.cpp @@ -0,0 +1,43 @@ +/* +// Definition for a Node. +class Node { +public: + int val; + vector neighbors; + Node() { + val = 0; + neighbors = vector(); + } + Node(int _val) { + val = _val; + neighbors = vector(); + } + Node(int _val, vector _neighbors) { + val = _val; + neighbors = _neighbors; + } +}; +*/ + +class Solution { +public: + unordered_map cloned; + Node* cloneGraph(Node* node) { + if (!node) return nullptr; + + if (cloned.find(node) != cloned.end()){ + return cloned[node]; + } + + Node* copy = new Node(node->val); + cloned[node] = copy; + + + for(Node* neighbor: node->neighbors){ + copy->neighbors.push_back(cloneGraph(neighbor)); + } + + return copy; + + } +}; \ No newline at end of file diff --git a/Challenge-29/course_schedule_1.cpp b/Challenge-29/course_schedule_1.cpp new file mode 100644 index 0000000..4fb78fd --- /dev/null +++ b/Challenge-29/course_schedule_1.cpp @@ -0,0 +1,35 @@ +class Solution { +public: + + bool dfs(int node, vector> &graph, vector &visited){ + if (visited[node]==1) return true; + if (visited[node]==2) return false; + + visited[node]=1; + + for(int neighbor : graph[node]){ + if (dfs(neighbor,graph,visited)) return true; + } + + visited[node] = 2; + return false; + } + + + + bool canFinish(int numCourses, vector>& prerequisites) { + + vector> graph(numCourses); + for (auto& p : prerequisites){ + graph[p[1]].push_back(p[0]); + } + vectorvisited(numCourses, 0); + for (int i=0; i& prices) { + + + int mini = prices[0]; + int maxProfit = 0; + for (int i=0; i dir = {0, 1, 0, -1, 0}; + + void dfs(vector>& heights, vector>& visited, int i, int j){ + visited[i][j] = true; + int m = heights.size(), n = heights[0].size(); + for (int d=0; d<4; d++){ + int x = i + dir[d]; + int y = j + dir[d+1]; + + if(x<0 || x>=m || y<0 || y>=n || visited[x][y] || heights[x][y] < heights[i][j]) continue; + + dfs(heights, visited, x, y); + } + + } + + vector> pacificAtlantic(vector>& heights) { + + int m = heights.size(); + int n = heights[0].size(); + + vector>pacific(m,vector(n,false)); + vector>atlantic(m,vector(n,false)); + vector>res; + + for (int i=0; i>& grid , vector>& vis, int row, int col){ + vis[row][col] = 1; + queue> q; + q.push({row,col}); + int n = grid.size(); + int m = grid[0].size(); + + + int drow[] = {-1, 1, 0, 0}; + int dcol[] = {0, 0, -1, 1}; + + while (!q.empty()){ + int row = q.front().first; + int col = q.front().second; + q.pop(); + + + + for (int i = 0; i < 4; i++){ + int nrow = row + drow[i]; + int ncol = col + dcol[i]; + + if (nrow >= 0 && nrow < n && ncol >= 0 && ncol < m && + !vis[nrow][ncol] && grid[nrow][ncol] == '1'){ + vis[nrow][ncol] = 1; + q.push({nrow, ncol}); + } + } + } + } + +public: + int numIslands(vector>& grid) { + int n = grid.size(); + int m = grid[0].size(); + vector> vis(n,vector(m,0)); + int cnt = 0; + for (int row = 0; row& nums) { + + int n = nums.size(); + if (n==0) return 0; + int longest = 1; + + unordered_set st; + + for (int i=0; i leftmostBuildingQueries(vector& heights, vector>& queries) { + + vector>> qs(heights.size()); + priority_queue, vector>, greater>> pq; + + vector res(queries.size(),-1); + + for (int i=0; i heights[mini]) res[i]=maxi; + else { + qs[maxi].push_back({ max(heights[mini], heights[maxi]), i }); + + } + + } + + for (int i=0; i& vals, vector>& edges, int k) { + int n = vals.size(); + vector> adj(n); + + for (auto edge: edges){ + int u = edge[0]; + int v = edge[1]; + + adj[u].push_back(vals[v]); + adj[v].push_back(vals[u]); + } + + int maxSum = INT_MIN; + + for (int i=0; ineighbors = adj[i]; + sort(neighbors.rbegin(), neighbors.rend()); + + int sum = vals[i]; + + for (int j =0; j< min(k, (int)neighbors.size()); j++){ + if (neighbors[j] > 0){ + sum += neighbors[j]; + } + else break; + } + + maxSum = max(sum, maxSum); + } + + + return maxSum; + + } +}; \ No newline at end of file diff --git a/Challenge-35/unreachable_pairs_count.cpp b/Challenge-35/unreachable_pairs_count.cpp new file mode 100644 index 0000000..744979c --- /dev/null +++ b/Challenge-35/unreachable_pairs_count.cpp @@ -0,0 +1,37 @@ +class Solution { + +private: + void dfs(vectoradj[], int node, vector&vis, long long &cnt){ + vis[node] = 1; + cnt++; + for(auto adjNode: adj[node]){ + if (!vis[adjNode]) dfs (adj,adjNode,vis,cnt); + } + } + + +public: + long long countPairs(int n, vector>& edges) { + + vectorvis(n,0); + vectoradj[n]; + for (auto edge: edges){ + int u = edge[0], v= edge[1]; + adj[u].push_back(v); + adj[v].push_back(u); + } + + long long ans = ((long long) n * (n-1)) / 2; + + for (int i=0; i> insert(vector>& intervals, vector& newInterval) { + + int n = intervals.size(); + int i = 0; + + vector> result; + + while(i> merge(vector>& intervals) { + int n = intervals.size(); + sort(intervals.begin(), intervals.end()); + vector> ans; + + for(int i=0; i>& intervals) { + if (intervals.empty()) return 0; + + + sort(intervals.begin(), intervals.end(), [](const vector& a, const vector& b) { + return a[1] < b[1]; + }); + + int count = 0; + int prevEnd = intervals[0][1]; + + for (int i = 1; i < intervals.size(); ++i) { + if (intervals[i][0] < prevEnd) { + ++count; + } else { + prevEnd = intervals[i][1]; + } + } + + return count; + } +}; diff --git a/Challenge-39/meeting_rooms.cpp b/Challenge-39/meeting_rooms.cpp new file mode 100644 index 0000000..bb5e9ad --- /dev/null +++ b/Challenge-39/meeting_rooms.cpp @@ -0,0 +1,19 @@ +bool compareIntervals(const Interval& a, const Interval& b) { + return a.start < b.start; +} + +class Solution { + +public: + bool canAttendMeetings(vector& intervals) { + sort(intervals.begin(), intervals.end(), compareIntervals); + + for (int i = 1; i < intervals.size(); ++i) { + if (intervals[i].start < intervals[i - 1].end) { + return false; + } + } + + return true; + } +}; \ No newline at end of file diff --git a/Challenge-4/contains_duplicates.cpp b/Challenge-4/contains_duplicates.cpp new file mode 100644 index 0000000..f9b31a1 --- /dev/null +++ b/Challenge-4/contains_duplicates.cpp @@ -0,0 +1,7 @@ +class Solution { +public: + bool containsDuplicate(vector& nums) { + unordered_setnumSet(nums.begin(),nums.end()); + return nums.size()!=numSet.size(); + } +}; \ No newline at end of file diff --git a/Challenge-40/meeting_rooms_2.cpp b/Challenge-40/meeting_rooms_2.cpp new file mode 100644 index 0000000..3add26c --- /dev/null +++ b/Challenge-40/meeting_rooms_2.cpp @@ -0,0 +1,40 @@ +/** + * Definition of Interval: + * class Interval { + * public: + * int start, end; + * Interval(int start, int end) { + * this->start = start; + * this->end = end; + * } + * } + */ + +class Solution { +public: + int minMeetingRooms(vector& intervals) { + if (intervals.empty()) return 0; + + vector starts, ends; + for (const auto& interval : intervals) { + starts.push_back(interval.start); + ends.push_back(interval.end); + } + + sort(starts.begin(), starts.end()); + sort(ends.begin(), ends.end()); + + int rooms = 0, end_ptr = 0; + + for (int i = 0; i < starts.size(); ++i) { + if (starts[i] < ends[end_ptr]) { + rooms++; + } else { + end_ptr++; + } + } + + return rooms; + } +}; + diff --git a/Challenge-41/reverse_linked_list.cpp b/Challenge-41/reverse_linked_list.cpp new file mode 100644 index 0000000..b440397 --- /dev/null +++ b/Challenge-41/reverse_linked_list.cpp @@ -0,0 +1,29 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + ListNode* reverseList(ListNode* head) { + + ListNode* temp = head; + ListNode* prev = NULL; + + while (temp!= NULL){ + ListNode* front = temp->next; + temp->next = prev; + prev = temp; + temp = front; + } + + return prev; + + + } +}; \ No newline at end of file diff --git a/Challenge-42/linked_list_cycle.cpp b/Challenge-42/linked_list_cycle.cpp new file mode 100644 index 0000000..615e591 --- /dev/null +++ b/Challenge-42/linked_list_cycle.cpp @@ -0,0 +1,25 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode(int x) : val(x), next(NULL) {} + * }; + */ +class Solution { +public: + bool hasCycle(ListNode *head) { + ListNode *slow = head; + ListNode *fast = head; + + while(fast!=NULL && fast->next != NULL){ + slow = slow->next; + fast = fast->next->next; + + if (slow==fast) return true; + + } + + return false; + } +}; \ No newline at end of file diff --git a/Challenge-43/merge_sorted_linked_list.cpp b/Challenge-43/merge_sorted_linked_list.cpp new file mode 100644 index 0000000..1f10167 --- /dev/null +++ b/Challenge-43/merge_sorted_linked_list.cpp @@ -0,0 +1,41 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) { + + ListNode* t1 = list1; + ListNode* t2 = list2; + + ListNode* dummy = new ListNode (-1); + ListNode* temp = dummy; + + + while(t1 != NULL && t2 != NULL){ + if (t1->val < t2->val){ + temp->next = t1; + temp = t1; + t1 = t1->next; + }else{ + temp->next = t2; + temp = t2; + t2 = t2->next; + } + } + + if (t1) temp->next = t1; + else temp->next = t2; + + + return dummy->next; + + } +}; \ No newline at end of file diff --git a/Challenge-44/merge_k_sorted_linked_list.cpp b/Challenge-44/merge_k_sorted_linked_list.cpp new file mode 100644 index 0000000..4780cf0 --- /dev/null +++ b/Challenge-44/merge_k_sorted_linked_list.cpp @@ -0,0 +1,36 @@ +class Solution { +public: + struct Compare { + bool operator()(ListNode* a, ListNode* b) { + return a->val > b->val; // Min-heap: smaller value comes first + } + }; + + ListNode* mergeKLists(vector& lists) { + priority_queue, Compare> minHeap; + + // Push the head of each non-null list into the heap + for (auto list : lists) { + if (list != nullptr) { + minHeap.push(list); + } + } + + ListNode dummy(0); // Dummy node to simplify list operations + ListNode* tail = &dummy; + + while (!minHeap.empty()) { + ListNode* node = minHeap.top(); + minHeap.pop(); + + tail->next = node; + tail = tail->next; + + if (node->next != nullptr) { + minHeap.push(node->next); + } + } + + return dummy.next; + } +}; diff --git a/Challenge-45/remove_node_from_end.cpp b/Challenge-45/remove_node_from_end.cpp new file mode 100644 index 0000000..2b5c878 --- /dev/null +++ b/Challenge-45/remove_node_from_end.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + ListNode* removeNthFromEnd(ListNode* head, int n) { + + ListNode* dummy = new ListNode(0, head); + ListNode* first = dummy; + ListNode* second = dummy; + + + for (int i = 0; i <= n; ++i) { + first = first->next; + } + + + while (first != nullptr) { + first = first->next; + second = second->next; + } + + + ListNode* nodeToDelete = second->next; + second->next = second->next->next; + delete nodeToDelete; + + + ListNode* newHead = dummy->next; + delete dummy; + return newHead; + } +}; diff --git a/Challenge-46/reorder_list.cpp b/Challenge-46/reorder_list.cpp new file mode 100644 index 0000000..ce515a2 --- /dev/null +++ b/Challenge-46/reorder_list.cpp @@ -0,0 +1,43 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + void reorderList(ListNode* head) { + if (!head || !head->next) return; + + ListNode* slow = head; + ListNode* fast = head; + while (fast->next && fast->next->next) { + slow = slow->next; + fast = fast->next->next; + } + + ListNode* prev = nullptr; + ListNode* curr = slow->next; + while (curr) { + ListNode* next_temp = curr->next; + curr->next = prev; + prev = curr; + curr = next_temp; + } + slow->next = nullptr; + ListNode* first = head; + ListNode* second = prev; + while (second) { + ListNode* tmp1 = first->next; + ListNode* tmp2 = second->next; + first->next = second; + second->next = tmp1; + first = tmp1; + second = tmp2; + } + } +}; diff --git a/Challenge-47/set_matrix_zeroes.cpp b/Challenge-47/set_matrix_zeroes.cpp new file mode 100644 index 0000000..0bac7db --- /dev/null +++ b/Challenge-47/set_matrix_zeroes.cpp @@ -0,0 +1,37 @@ +class Solution { +public: + void setZeroes(vector>& matrix) { + int n = matrix.size(); + int m = matrix[0].size(); + int col0 = 1; + for (int i=0; i spiralOrder(vector>& matrix) { + int n = matrix.size(); + int m = matrix[0].size(); + + int top = 0, bottom = n-1; + int left = 0, right = m-1; + + vectorans; + + + while (top<=bottom && left<=right){ + for (int i=left;i<=right;i++){ + ans.push_back(matrix[top][i]); + } + top++; + + for (int i= top;i <=bottom; i++){ + ans.push_back(matrix[i][right]); + } + right--; + + if(top<=bottom){ + for(int i= right; i>=left; i--){ + ans.push_back(matrix[bottom][i]); + } + bottom--; + } + + if(left<=right){ + for(int i=bottom; i>=top; i--){ + ans.push_back(matrix[i][left]); + } + left++; + } + } + + return ans; + + } +}; \ No newline at end of file diff --git a/Challenge-49/rotate_matrix.cpp b/Challenge-49/rotate_matrix.cpp new file mode 100644 index 0000000..daed7fe --- /dev/null +++ b/Challenge-49/rotate_matrix.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + void rotate(vector>& mat) { + + int n = mat.size(); + for (int i=0; i0){ + b--; + a++; + } + while(b<0){ + b++; + a--; + } + return a; + + } +}; \ No newline at end of file diff --git a/Challenge-50/word_search.cpp b/Challenge-50/word_search.cpp new file mode 100644 index 0000000..63730de --- /dev/null +++ b/Challenge-50/word_search.cpp @@ -0,0 +1,45 @@ +class Solution { +public: + bool exist(vector>& board, string word) { + int m = board.size(); + int n = board[0].size(); + + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + if (dfs(board, word, i, j, 0)) { + return true; + } + } + } + return false; + } + +private: + bool dfs(vector>& board, string& word, int i, int j, int idx) { + if (idx == word.length()) return true; + + + if (i < 0 || i >= board.size() || + j < 0 || j >= board[0].size() || + board[i][j] != word[idx]) { + return false; + } + + char temp = board[i][j]; + board[i][j] = '#'; + + + vector> directions = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; + bool found = false; + + for (auto [dx, dy] : directions) { + if (dfs(board, word, i + dx, j + dy, idx + 1)) { + found = true; + break; + } + } + + board[i][j] = temp; + return found; + } +}; diff --git a/Challenge-51/longest_substr_without_repeat.cpp b/Challenge-51/longest_substr_without_repeat.cpp new file mode 100644 index 0000000..6c8b3da --- /dev/null +++ b/Challenge-51/longest_substr_without_repeat.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + int lengthOfLongestSubstring(string s) { + unordered_map charIndexMap; + int maxLength = 0; + int left = 0; + + for (int right = 0; right < s.length(); ++right) { + char currentChar = s[right]; + + + if (charIndexMap.count(currentChar) && charIndexMap[currentChar] >= left) { + left = charIndexMap[currentChar] + 1; + } + + charIndexMap[currentChar] = right; + maxLength = max(maxLength, right - left + 1); + } + + return maxLength; + } +}; \ No newline at end of file diff --git a/Challenge-52/longest_repeat_char_replacement.cpp b/Challenge-52/longest_repeat_char_replacement.cpp new file mode 100644 index 0000000..60fcf8c --- /dev/null +++ b/Challenge-52/longest_repeat_char_replacement.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + int characterReplacement(string s, int k) { + vector count(26, 0); + int maxFreq = 0; + int left = 0; + int maxLength = 0; + + for (int right = 0; right < s.size(); ++right) { + count[s[right] - 'A']++; + maxFreq = max(maxFreq, count[s[right] - 'A']); + + while ((right - left + 1) - maxFreq > k) { + count[s[left] - 'A']--; + left++; + } + + maxLength = max(maxLength, right - left + 1); + } + + return maxLength; + } +}; diff --git a/Challenge-53/min_window_substring.cpp b/Challenge-53/min_window_substring.cpp new file mode 100644 index 0000000..291b2e1 --- /dev/null +++ b/Challenge-53/min_window_substring.cpp @@ -0,0 +1,45 @@ +class Solution { +public: + string minWindow(string s, string t) { + if (s.empty() || t.empty()) return ""; + + + int t_count[128] = {0}; + int window[128] = {0}; + + for (char c : t) t_count[c]++; + + int left = 0, right = 0; + int min_len = INT_MAX, min_start = 0; + int required = t.length(); + + while (right < s.length()) { + char c = s[right]; + if (t_count[c] > 0) { + if (window[c] < t_count[c]) + required--; + window[c]++; + } + + + while (required == 0) { + if (right - left + 1 < min_len) { + min_len = right - left + 1; + min_start = left; + } + + char l = s[left]; + if (t_count[l] > 0) { + window[l]--; + if (window[l] < t_count[l]) + required++; + } + left++; + } + + right++; + } + + return min_len == INT_MAX ? "" : s.substr(min_start, min_len); + } +}; diff --git a/Challenge-54/is_anagram.cpp b/Challenge-54/is_anagram.cpp new file mode 100644 index 0000000..8c31587 --- /dev/null +++ b/Challenge-54/is_anagram.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + bool isAnagram(string s, string t) { + if (s.length() != t.length()) return false; + + int count[26] = {0}; + + for (int i = 0; i < s.length(); i++) { + count[s[i] - 'a']++; + count[t[i] - 'a']--; + } + + for (int i = 0; i < 26; i++) { + if (count[i] != 0) return false; + } + + return true; + } +}; diff --git a/Challenge-55/group_anagram.cpp b/Challenge-55/group_anagram.cpp new file mode 100644 index 0000000..b22b319 --- /dev/null +++ b/Challenge-55/group_anagram.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + vector> groupAnagrams(vector& strs) { + unordered_map> anagramGroups; + + for (const string& str : strs) { + string sortedStr = str; + sort(sortedStr.begin(), sortedStr.end()); + anagramGroups[sortedStr].push_back(str); + } + + vector> result; + for (auto& pair : anagramGroups) { + result.push_back(move(pair.second)); + } + + return result; + } +}; diff --git a/Challenge-6/product_except_self.cpp b/Challenge-6/product_except_self.cpp new file mode 100644 index 0000000..ba13a20 --- /dev/null +++ b/Challenge-6/product_except_self.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + vector productExceptSelf(vector& nums) { + int n = nums.size(); + vectorres(n,1); + int prefix = 1; + int suffix = 1; + for (int i=0;i=0;i--){ + res[i]*=suffix; + suffix*=nums[i]; + } + + return res; + + } +}; \ No newline at end of file diff --git a/Challenge-7/max_sum_subarray.cpp b/Challenge-7/max_sum_subarray.cpp new file mode 100644 index 0000000..6d99736 --- /dev/null +++ b/Challenge-7/max_sum_subarray.cpp @@ -0,0 +1,18 @@ +class Solution { +public: + int maxSubArray(vector& nums) { + + int currSum = 0; + int maxSum = INT_MIN; + + for(int num : nums){ + currSum += num; + maxSum = max(maxSum,currSum); + + if(currSum<0) currSum = 0; + } + + return maxSum; + + } +}; \ No newline at end of file diff --git a/Challenge-8/max_prd_subarray.cpp b/Challenge-8/max_prd_subarray.cpp new file mode 100644 index 0000000..8215fff --- /dev/null +++ b/Challenge-8/max_prd_subarray.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + int maxProduct(vector& nums) { + + int prefix = 1, suffix = 1; + int n = nums.size(); + int ans = INT_MIN; + for(int i=0; i& nums) { + int n = nums.size(); + int low = 0; + int high = n-1; + int ans = INT_MAX; + + while(low<=high){ + int mid = (low+high)/2; + if(nums[low]<=nums[mid]){ + ans = min(nums[low],ans); + low = mid + 1; + } + else{ + ans = min(nums[mid],ans); + high = mid-1; + } + } + return ans; + + } +}; \ No newline at end of file diff --git a/README.md b/README.md index d922b9d..a72ef7c 100644 --- a/README.md +++ b/README.md @@ -1,148 +1,3 @@ -# GitHub Guide for Submitting Challenge Solutions - -Welcome to the GitHub repository for coding challenges! This guide will walk you through the process of creating a branch under your name and uploading your solution files step by step. If you are new to Git and GitHub, follow these instructions carefully. - ---- - -## πŸš€ Setting Up Git and GitHub - -### 1️⃣ Install Git -If you haven't installed Git yet, download and install it from [Git's official website](https://git-scm.com/downloads). - -### 2️⃣ Create a GitHub Account -If you don’t have a GitHub account, create one at [GitHub](https://github.com/). - -### 3️⃣ Fork the Repository -1. Go to the repository URL shared by your instructor. -2. Click on the **Fork** button in the top-right corner to create a copy in your GitHub account. - -### 4️⃣ Clone the Repository Locally -1. Open **Terminal** (Linux/macOS) or **Command Prompt/Powershell** (Windows). -2. Run the following command, replacing `your-username` with your GitHub username: - - ```sh - git clone https://github.com/WeCode-Community-Dev/blind-75-challenge.git - ``` - -3. Navigate into the repository folder: - - ```sh - cd blind-75-challenge - ``` - ---- - -## 🌿 Creating a Branch Under Your Name -Each student must create a separate branch with their name before making any changes. - -1. Check the current branches: - - ```sh - git branch - ``` - -2. Create a new branch with your name (make sure this is unique - add some unique number or characters to make sure it is not already there): - - ```sh - git checkout -b your-branch-name - ``` - -3. Verify the branch was created and switched to it: - - ```sh - git branch - ``` - - Your name should now be highlighted, meaning you are on that branch. - ---- - -## πŸ” Setting Upstream and Pulling Latest Changes -Before working on a new challenge, ensure your repository is up to date. - -1. Add the original repository as the upstream remote (only required once): - - ```sh - git remote add upstream https://github.com/WeCode-Community-Dev/blind-75-challenge.git - ``` - -2. Fetch the latest changes: - - ```sh - git fetch upstream - ``` - -3. Merge the latest changes into your branch: - - ```sh - git merge upstream/main - ``` - ---- - -## ✏️ Adding Your Challenge Solution -Upload only the solution file for each challenge (e.g., `solution.py`, `solution.cpp`, `solution.java`). -you can name the file .py or .cpp or .java etc. -example: two-sum.py or two-sum.cpp or two-sum.java as per the programming language you choose - -1. Navigate to the folder for the challenge you are working on. -2. Place your solution file inside the appropriate challenge folder. - -Example structure: - -``` -repository-name/ -β”‚-- Challenge-1/ -β”‚ β”‚-- solution.py # Your file -β”‚-- Challenge-2/ -β”‚ β”‚-- solution.cpp # Another file -``` - ---- - -## πŸ“€ Committing and Pushing Your Code - -Replace the "challenge-1" with problem name - -1. Add the file you modified: - - ```sh - git add Challenge-1/solution.py - ``` - -2. Commit your changes with a meaningful message: - - ```sh - git commit -m "Added solution for Challenge 1" - ``` - -3. Push your branch to your GitHub repository: - - ```sh - git push origin your-branch-name - ``` - ---- - -## πŸ”„ Creating a Pull Request (Not Required) -Once you've pushed your changes, you need to create a **Pull Request (PR)**. - -1. Go to your GitHub repository. -2. Switch to your branch (`your-branch-name`). -3. Click on **Compare & pull request**. -4. Add a meaningful **title** and **description**. -5. Click **Create pull request**. - -Your code will now be reviewed and merged by the instructor! πŸŽ‰ - ---- - -## ❗ Important Rules -βœ… Always create a branch under your name. -βœ… Upload only the solution file (no unnecessary files or folders). -βœ… Keep your repository updated by pulling the latest changes. -βœ… Use meaningful commit messages. -βœ… Create a pull request for every challenge. - -Happy coding! πŸš€ +**Name:** Lakshmi Swetha S +**Email:** lakshmisswetha@gmail.com