This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int maxProfit(vector<int>& prices) { | |
int min_i = -1, max_profit = INT_MIN; | |
for(int i = 0; i < prices.size(); i++) { | |
if (min_i == -1 || prices[i] < prices[min_i]) { | |
min_i = i; | |
continue; | |
} | |
if (prices[i] - prices[min_i] > max_profit) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int findUnsortedSubarray(vector<int>& nums) { | |
vector<int> nums2 = nums; | |
int n = nums.size(), left = 0, right = nums.size() - 1; | |
sort(nums.begin(), nums.end()); | |
for(left = 0; left < n; ++left) { | |
//cout << "left: "<< left << ", " << nums[left] << ", " << nums2[left] << endl; | |
if (nums[left] != nums2[left]) break; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) { | |
int o_r = nums.size(); | |
int o_c = nums[0].size(); | |
int n = o_c * o_r; | |
if (n == r*c) { | |
vector<vector<int>> results(r, vector<int>(c, 0)); | |
for(int i = 0; i < n; i++) { | |
results[i / c][i % c] = nums[i / o_c][i % o_c]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int findLengthOfLCIS(vector<int>& nums) { | |
int res = 0, cnt = 0; | |
for(int i = 0; i < nums.size(); i++) { | |
if (i == 0 || nums[i-1] < nums[i]) res = max(res, ++cnt); | |
else cnt = 1; | |
} | |
return res; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int findShortestSubArray(vector<int>& nums) { | |
unordered_map<int, int> counts; | |
unordered_map<int, int> starts; | |
unordered_map<int, int> ends; | |
int maxCount = INT_MIN; | |
for(int i = 0; i < nums.size(); i++) { | |
int num = nums[i]; | |
if (starts.count(num) == 0) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int findDuplicate(vector<int>& nums) { | |
int n = nums.size(); | |
int slow = n; | |
int fast = n; | |
do { | |
//cout << "1> slow: " << slow << ", fast: " << fast << endl; | |
slow = nums[slow-1]; | |
fast = nums[nums[fast-1]-1]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int islandPerimeter(vector<vector<int>>& grid) { | |
int count = 0, repeat = 0, n = grid.size(), m = grid[0].size(); | |
for(int i = 0; i < n; i++) { | |
for(int j = 0; j < m; j++) { | |
if (grid[i][j] == 1) { | |
count++; | |
if (i != 0 && grid[i-1][j] == 1) repeat++; | |
if (j != 0 && grid[i][j-1] == 1) repeat++; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int maxAreaOfIsland(vector<vector<int>>& grid) { | |
int area = 0; | |
for(int i = 0; i < grid.size(); i++) { | |
for(int j = 0; j < grid[i].size(); j++) { | |
if (grid[i][j] != 0) { | |
area = max(area, areaOfIsland(grid, i, j)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
bool isOneBitCharacter(vector<int>& bits) { | |
int i = 0; | |
while(i < bits.size() - 1) { | |
if (bits[i] == 1) { | |
i += 2; | |
} else { | |
i += 1; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int pivotIndex(vector<int>& nums) { | |
if (nums.size() == 0) return -1; | |
int total = 0; | |
for(int i = 0; i < nums.size(); i++) { | |
total += nums[i]; | |
} | |
int l = 0, r = total; | |
for(int j = 0; j < nums.size(); j++) { |