forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Four new problems - "Number of 1 Bits", "Reverse Bits", "Rotate Array…
…", "Best Time to Buy and Sell Stock IV"
- Loading branch information
Showing
5 changed files
with
262 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
76 changes: 76 additions & 0 deletions
76
src/bestTimeToBuyAndSellStock/bestTimeToBuyAndSellStock.IV.cpp
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Source : https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/ | ||
// Author : Hao Chen | ||
// Date : 2015-03-31 | ||
|
||
/********************************************************************************** | ||
* | ||
* Say you have an array for which the ith element is the price of a given stock on day i. | ||
* | ||
* Design an algorithm to find the maximum profit. You may complete at most k transactions. | ||
* | ||
* Note: | ||
* You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again). | ||
* | ||
* Credits:Special thanks to @Freezen for adding this problem and creating all test cases. | ||
* | ||
**********************************************************************************/ | ||
|
||
class Solution { | ||
public: | ||
/* | ||
* dp[i, j] | ||
* - `i` represents the number of transactions we've done so far. ( 0 <= i <= k ) | ||
* - `j` represents the number of days so far. ( 0 <= j <= prices.size() ) | ||
* | ||
* So, we have the initialization as below: | ||
* | ||
* dp[0, j] = 0; // 0 <= j <= prices.size() | ||
* dp[i, 0] = 0; // 0 <= i <= k | ||
* | ||
* And the iteration logic as below: | ||
* | ||
* dp[i, j] = max ( | ||
* dp[i, j-1], // same times transactions, but one days before. | ||
* dp[i-1, t] + prices[j] - prices[t+1] // for all of (0 <= t < j ) | ||
* // this means find max profit from previous any of days | ||
* ) | ||
* | ||
*/ | ||
|
||
int maxProfit(int k, vector<int> &prices) { | ||
int ndays = prices.size(); | ||
|
||
// error case | ||
if (ndays<=1) return 0; | ||
|
||
// Edge case | ||
// --------- | ||
// if the number of transactions is too many, it means we can make | ||
// as many transactions as we can, that brings us the problem back to | ||
// Best-Time-To-Buy-And-Sell-Stock-II which can be simply solve in O(n) | ||
// by using a greedy approach. | ||
if(k > ndays/2){ | ||
int sum = 0; | ||
for (int i=1; i<ndays; i++) { | ||
sum += max(prices[i] - prices[i-1], 0); | ||
} | ||
return sum; | ||
} | ||
|
||
//DP solution - O(kn) complexity | ||
vector< vector<int> > dp (k+1, vector<int>( ndays+1, 0)); | ||
|
||
for(int i=1; i<=k; i++) { | ||
int t = dp[i-1][0] - prices[0]; | ||
for (int j=1; j <= ndays; j++) { | ||
dp[i][j] = max(dp[i][j-1], t + prices[j-1]); | ||
if ( j < ndays ) { | ||
t = max(t, dp[i-1][j] - prices[j]); | ||
} | ||
} | ||
} | ||
|
||
return dp[k][ndays]; | ||
|
||
} | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Source : https://leetcode.com/problems/number-of-1-bits/ | ||
// Author : Hao Chen | ||
// Date : 2015-03-30 | ||
|
||
/********************************************************************************** | ||
* | ||
* Write a function that takes an unsigned integer and returns the number of ’1' bits it has | ||
* (also known as the Hamming weight). | ||
* | ||
* For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, | ||
* so the function should return 3. | ||
* | ||
* Credits:Special thanks to @ts for adding this problem and creating all test cases. | ||
* | ||
**********************************************************************************/ | ||
|
||
class Solution { | ||
public: | ||
int hammingWeight(uint32_t n) { | ||
int cnt = 0; | ||
for(;n>0; n/=2){ | ||
if (n & 0x1) cnt++; | ||
} | ||
return cnt; | ||
} | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Source : https://leetcode.com/problems/reverse-bits/ | ||
// Author : Hao Chen | ||
// Date : 2015-03-30 | ||
|
||
/********************************************************************************** | ||
* | ||
* Reverse bits of a given 32 bits unsigned integer. | ||
* | ||
* For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), | ||
* return 964176192 (represented in binary as 00111001011110000010100101000000). | ||
* | ||
* Follow up: | ||
* If this function is called many times, how would you optimize it? | ||
* | ||
* Related problem: Reverse Integer | ||
* | ||
* Credits:Special thanks to @ts for adding this problem and creating all test cases. | ||
* | ||
**********************************************************************************/ | ||
|
||
|
||
|
||
class Solution { | ||
public: | ||
uint32_t reverseBits(uint32_t n) { | ||
uint32_t ret=0; | ||
for(int i=0; i<32; i++) { | ||
ret = (ret*2) + (n & 0x1); | ||
n /=2 ; | ||
} | ||
return ret; | ||
} | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
// Source : https://leetcode.com/problems/rotate-array/ | ||
// Author : Hao Chen | ||
// Date : 2015-03-30 | ||
|
||
/********************************************************************************** | ||
* | ||
* Rotate an array of n elements to the right by k steps. | ||
* For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. | ||
* | ||
* Note: | ||
* Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem. | ||
* | ||
* Hint: | ||
* Could you do it in-place with O(1) extra space? | ||
* | ||
* Related problem: Reverse Words in a String II | ||
* | ||
* Credits:Special thanks to @Freezen for adding this problem and creating all test cases. | ||
* | ||
**********************************************************************************/ | ||
|
||
#include <stdlib.h> | ||
#include <time.h> | ||
#include <iostream> | ||
using namespace std; | ||
|
||
|
||
void reverseArray(int nums[],int start, int end){ | ||
int temp; | ||
while(start < end){ | ||
int temp = nums[start]; | ||
nums[start++] = nums[end]; | ||
nums[end--] = temp; | ||
} | ||
} | ||
|
||
/* | ||
* this solution is so-called three times rotate method | ||
* because (X^TY^T)^T = YX, so we can perform rotate operation three times to get the result | ||
* obviously, the algorithm consumes O(1) space and O(n) time | ||
*/ | ||
|
||
void rotate1(int nums[], int n, int k) { | ||
if (k<=0) return; | ||
k %= n; | ||
reverseArray(nums, n-k, n-1); | ||
reverseArray(nums, 0, n-k-1); | ||
reverseArray(nums, 0, n-1); | ||
} | ||
|
||
/* | ||
* How to change [0,1,2,3,4,5,6] to [4,5,6,0,1,2,3] by k = 3? | ||
* | ||
* We can change by following rules: | ||
* | ||
* [0]->[3], [3]->[6], [6]->[2], [2]->[5], [5]->[1], [1]->[4] | ||
* | ||
* | ||
*/ | ||
void rotate2(int nums[], int n, int k) { | ||
if (k<=0) return; | ||
k %= n; | ||
int currIdx=0, newIdx=k; | ||
int tmp1 = nums[currIdx], tmp2; | ||
int origin = 0; | ||
|
||
for(int i=0; i<n; i++){ | ||
|
||
tmp2 = nums[newIdx]; | ||
nums[newIdx] = tmp1; | ||
tmp1 = tmp2; | ||
|
||
currIdx = newIdx; | ||
|
||
//if we meet a circle, move the next one | ||
if (origin == currIdx) { | ||
origin = ++currIdx; | ||
tmp1 = nums[currIdx]; | ||
} | ||
newIdx = (currIdx + k) % n; | ||
|
||
} | ||
} | ||
|
||
void rotate(int nums[], int n, int k) { | ||
if (random()%2==0) { | ||
cout << "[1] "; | ||
return rotate1(nums, n, k); | ||
} | ||
cout << "[2] "; | ||
return rotate1(nums, n, k); | ||
} | ||
|
||
void printArray(int nums[], int n) { | ||
cout << "[ " ; | ||
for(int i=0; i<n; i++) { | ||
cout << nums[i] << ((i==n-1)? " " : ", "); | ||
} | ||
cout << "]" << endl; | ||
} | ||
|
||
void initArray(int nums[], int n) { | ||
for(int i=0; i<n; i++) { | ||
nums[i] = i; | ||
} | ||
} | ||
|
||
|
||
int main(int argc, char**argv) { | ||
|
||
srand(time(0)); | ||
|
||
int nums[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; | ||
|
||
const int n = sizeof(nums)/sizeof(int); | ||
|
||
for (int i=0; i<n; i++) { | ||
initArray(nums, n); | ||
rotate(nums, n, i); | ||
printArray(nums, n); | ||
} | ||
return 0; | ||
} |