madhu242

LC - 1

Oct 27th, 2022
1,071
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.46 KB | Source Code | 0 0
  1. // First missing positive
  2.  
  3. class Solution {
  4. public:
  5.     int firstMissingPositive(vector<int>& nums) {
  6.         if (nums.empty()) {
  7.             return 1;
  8.         }
  9.        
  10.         int low = 0, high = nums.size() - 1;
  11.         while (low < high) {
  12.             while (low < high && nums[low] > 0) low++;
  13.             while (low < high && nums[high] <= 0) high--;
  14.             if (low < high)
  15.                 swap(nums, low, high);
  16.         }
  17.        
  18.         int endOfPositives = nums[low] > 0 ? low : low - 1;
  19.         int start = 0;
  20.        
  21.         for (int i = 0; i <= endOfPositives; i++) {
  22.             if (nums[i] > endOfPositives + 1) {
  23.                 continue;
  24.             }
  25.  
  26.             int idxToNegate = nums[i];
  27.             if (idxToNegate < 0) {
  28.                 idxToNegate *= -1;
  29.             }
  30.            
  31.             if (nums[idxToNegate - 1] > 0) {
  32.                 nums[idxToNegate - 1] *= -1;
  33.             }
  34.         }
  35.        
  36.         for (int i = 0; i <= endOfPositives; i++) {
  37.             if (nums[i] > 0) {
  38.                 return i + 1;
  39.             }
  40.         }
  41.        
  42.         return endOfPositives + 2;
  43.     }
  44.    
  45. private:
  46.     void swap(vector<int> &nums, int i, int j) {
  47.         int k = nums[i];
  48.         nums[i] = nums[j];
  49.         nums[j] = k;
  50.     }
  51. };
  52.  
  53.  
  54. // Group anagrams
  55. class Solution {
  56. public:
  57.     vector<vector<string>> groupAnagrams(vector<string>& strs) {
  58.         vector<vector<string>> result;
  59.         unordered_map<string, vector<string>> um;
  60.         for (string str: strs) {
  61.             string sortedString = str;
  62.             sort(sortedString.begin(), sortedString.end());
  63.             um[sortedString].push_back(str);
  64.         }
  65.        
  66.         for (auto e: um) {
  67.             result.push_back(e.second);
  68.         }
  69.         return result;
  70.     }
  71. };
  72.  
  73. // Find duplicate with constant space and time complexity.
  74. class Solution {
  75. public:
  76.    
  77. // [1, 2, 3, 4, 5, 4]
  78. int findDuplicate(vector<int>& nums) {
  79.         // Fast and Slow pointers:
  80.         if (nums.empty() || nums.size() == 1) {
  81.             return -1;  
  82.         }
  83.         int slowIdx = nums[0], fastIdx = nums[0];
  84.         do {
  85.             slowIdx = nums[slowIdx];
  86.             fastIdx = nums[nums[fastIdx]];
  87.         } while (slowIdx != fastIdx);
  88.        
  89.         slowIdx = nums[0];
  90.         while (slowIdx != fastIdx) {
  91.             slowIdx = nums[slowIdx];
  92.             fastIdx = nums[fastIdx];
  93.         }
  94.         return slowIdx;
  95.     }
  96. };
  97.  
  98.  
  99. // Add two numbers
  100. /**
  101.  * Definition for singly-linked list.
  102.  * struct ListNode {
  103.  *     int val;
  104.  *     ListNode *next;
  105.  *     ListNode() : val(0), next(nullptr) {}
  106.  *     ListNode(int x) : val(x), next(nullptr) {}
  107.  *     ListNode(int x, ListNode *next) : val(x), next(next) {}
  108.  * };
  109.  */
  110. class Solution {
  111. public:
  112.     ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
  113.         ListNode *head = new ListNode(0);
  114.         ListNode *prevNode = nullptr;
  115.         int carry = 0;
  116.        
  117.         while (l1 != nullptr || l2 != nullptr || carry != 0) {
  118.             int sum = carry;
  119.             if (l1 != nullptr) {
  120.                 sum += l1->val;
  121.                 l1 = l1->next;
  122.             }
  123.             if (l2 != nullptr) {
  124.                 sum += l2->val;
  125.                 l2 = l2->next;
  126.             }
  127.             if (prevNode == nullptr) {
  128.                 head->val = sum % 10;
  129.                 prevNode = head;
  130.             } else {
  131.                 prevNode->next = new ListNode(sum % 10);
  132.                 prevNode = prevNode->next;
  133.             }
  134.             carry = sum / 10;
  135.         }
  136.         return head;
  137.     }
  138. };
  139.  
  140. // Merge intervals - sort by startTime
  141. class Solution {
  142. public:
  143.     vector<vector<int>> merge(vector<vector<int>>& intervals) {
  144.         vector<vector<int>> result;
  145.         sort(intervals.begin( ), intervals.end( ), []( const auto& lhs, const auto& rhs)
  146. {
  147.    return lhs[0] < rhs[0];
  148. });
  149.     vector<int> curInterval = intervals[0];
  150.     for (int i = 1; i < intervals.size(); i++) {
  151.         if (curInterval[1] >= intervals[i][0]) {
  152.             curInterval[1] = max(curInterval[1], intervals[i][1]);
  153.             curInterval[0] = min(curInterval[0], intervals[i][0]);
  154.         } else {
  155.             result.push_back(curInterval);
  156.             curInterval = intervals[i];
  157.         }
  158.     }
  159.     result.push_back(curInterval);
  160.     return result;
  161. }
  162. };
  163.  
  164. // Max sub array
  165. class Solution {
  166. public:
  167.     int maxSubArray(vector<int>& nums) {
  168.         if (nums.empty()) {
  169.             return 0;
  170.         }
  171.         int sum = 0, maxSum = numeric_limits<int>::min();
  172.         for (int i = 0; i < nums.size(); i++) {
  173.             sum += nums[i];
  174.             if (sum > maxSum) {
  175.                 maxSum = sum;    
  176.             }
  177.             if (sum < 0) {
  178.                 sum = 0;
  179.             }
  180.         }
  181.         return maxSum;
  182.     }
  183. };
  184.  
  185.  
  186. // Insert Delete GetRandom in O(1) time
  187. class RandomizedSet {
  188. public:
  189.     RandomizedSet() {
  190.        
  191.     }
  192.     bool insert(int val) {
  193.         if (m.find(val) != m.end()) return false;
  194.         nums.emplace_back(val);
  195.         m[val] = nums.size() - 1;
  196.         return true;
  197.     }
  198.     bool remove(int val) {
  199.         if (m.find(val) == m.end()) return false;
  200.         int last = nums.back();
  201.         m[last] = m[val];
  202.         nums[m[val]] = last;
  203.         nums.pop_back();
  204.         m.erase(val);
  205.         return true;
  206.     }
  207.     int getRandom() {
  208.         return nums[rand() % nums.size()];
  209.     }
  210. private:
  211.     vector<int> nums;
  212.     unordered_map<int, int> m;
  213. };
Advertisement
Add Comment
Please, Sign In to add comment