Best Time to Buy and Sell Stock I, II and III

Description https://oj.leetcode.com/problems/combinations/ Difficulty: 1.0/5.0 star Analysis and solution class Solution { public: vector<vector<int> > combine(int n, int k) { vector<vector<int> > result; vector<int> current; generateCombine(0, n, k, result, current); return result; } void generateCombine(int start, int n, int k, vector<vector<int>...

Set Matrix Zeros

Description https://oj.leetcode.com/problems/set-matrix-zeroes/ Difficulty: 1.0/5.0 star. Analysis and solution The key to avoid using extra space is to reuse the first row and column as marking array. class Solution { public: void setZeroes(vector<vector<int> > &matrix) { int nRow = matrix.size(); if...

Search in Rotated Sorted Array ii

Description https://oj.leetcode.com/problems/search-in-rotated-sorted-array/ Difficulty: 2.5/5.0 Analysis and solution The solution to problem i almost works for all cases with redundancy except 1,1,1,1,2,3,1,1,1 where the left side equals to the right side. Thus we first eliminate all the 1s at the right...

Search in Rotated Sorted Array

Description https://oj.leetcode.com/problems/search-in-rotated-sorted-array/ Difficulty: 2.5/5.0 Analysis and solution We solve this problem by first locating the pivot and then do binary searches on each part seperately. The way to locate the pivot is to locate the $i$, where $A[i] > A[i+1]$....

Search a 2D Matrix

Description https://oj.leetcode.com/problems/search-a-2d-matrix/ Difficulty: 1.0/5.0 Analysis and solution class Solution { public: bool searchMatrix(vector<vector<int> > &matrix, int target) { vector<int>firstCol; for (int i = 0 ; i < matrix.size(); i ++) firstCol.push_back(matrix[i][0]); int indx = binary_search(firstCol, target); if (indx == -1)...