|
8 | 8 | 6. House Robber |
9 | 9 | 7. Degree of an array |
10 | 10 | 8. Decode Ways |
| 11 | +9. Best Time to sell stocks II |
| 12 | +10. Monotonic Array |
| 13 | +11. Longest Continuous Increasing Subsequence |
| 14 | +12. Maximum Product Subarray |
11 | 15 |
|
12 | 16 |
|
13 | 17 | ## Implementation |
@@ -377,4 +381,111 @@ public: |
377 | 381 | return dp[len - 1]; |
378 | 382 | } |
379 | 383 | }; |
| 384 | +``` |
| 385 | +### **Best Time to sell stocks II** |
| 386 | +
|
| 387 | +***Big O:*** O(n) speed, O(1) space |
| 388 | +``` |
| 389 | +Tips: |
| 390 | + |
| 391 | +Greedy. Buy low, Sell high. |
| 392 | +``` |
| 393 | +```c++ |
| 394 | +class Solution { |
| 395 | +public: |
| 396 | + int maxProfit(vector<int>& prices) { |
| 397 | + int prev = prices[0], res = 0; |
| 398 | + for (int curr: prices) { |
| 399 | + if (prev < curr) res += curr - prev; |
| 400 | + prev = curr; |
| 401 | + } |
| 402 | + return res; |
| 403 | + } |
| 404 | +}; |
| 405 | +``` |
| 406 | + |
| 407 | +### **Monotonic Array** |
| 408 | + |
| 409 | +***Big O:*** O(n) speed, O(1) space |
| 410 | +``` |
| 411 | +Tips: |
| 412 | +
|
| 413 | +Self evident. |
| 414 | +``` |
| 415 | +```c++ |
| 416 | +class Solution { |
| 417 | +public: |
| 418 | + bool isMonotonic(vector<int>& A) { |
| 419 | + bool increase = true; |
| 420 | + bool decrease = true; |
| 421 | + for(int i = 0; i < A.size() - 1; i++) { |
| 422 | + if(A[i] > A[i+1]) increase = false; |
| 423 | + if(A[i] < A[i+1]) decrease = false; |
| 424 | + if(increase == false && decrease == false) return false; |
| 425 | + } |
| 426 | + return true; |
| 427 | + } |
| 428 | +}; |
| 429 | +``` |
| 430 | +
|
| 431 | +### **Longest Continuous Increasing Subsequence** |
| 432 | +
|
| 433 | +***Big O:*** O(n) speed, O(1) space |
| 434 | +``` |
| 435 | +Tips: |
| 436 | + |
| 437 | +Self evident. |
| 438 | +``` |
| 439 | +```c++ |
| 440 | +class Solution { |
| 441 | +public: |
| 442 | + int findLengthOfLCIS(vector<int>& nums) { |
| 443 | + if(nums.size()<=1)return nums.size(); |
| 444 | + int answer=1,count=1; |
| 445 | + for(int i=0;i<nums.size()-1;i++){ |
| 446 | + if(nums[i]<nums[i+1]){ |
| 447 | + count++; |
| 448 | + answer=max(answer,count); |
| 449 | + } |
| 450 | + else{ |
| 451 | + count=1; |
| 452 | + } |
| 453 | + } |
| 454 | + return answer; |
| 455 | + } |
| 456 | +}; |
| 457 | +``` |
| 458 | + |
| 459 | +### **Maximum Product Subarray** |
| 460 | + |
| 461 | +***Big O:*** O(n) speed, O(1) space |
| 462 | +``` |
| 463 | +Tips: |
| 464 | +
|
| 465 | +Dynamic Programming. While going through numbers in nums, we will have to keep track of the maximum product up to that number (we will call max_so_far) and minimum product up to that number (we will call min_so_far). The reason behind keeping track of max_so_far is to keep track of the accumulated product of positive numbers. The reason behind keeping track of min_so_far is to properly handle negative numbers. |
| 466 | +``` |
| 467 | +```c++ |
| 468 | +class Solution { |
| 469 | +public: |
| 470 | + |
| 471 | + int maxProduct(vector<int>& nums) { |
| 472 | + if (nums.size() == 0) return 0; |
| 473 | + |
| 474 | + int max_so_far = nums[0]; |
| 475 | + int min_so_far = nums[0]; |
| 476 | + int result = max_so_far; |
| 477 | + |
| 478 | + for (int i = 1; i < nums.size(); i++) { |
| 479 | + int curr = nums[i]; |
| 480 | + int temp_max = max(curr, max(max_so_far * curr, min_so_far * curr)); |
| 481 | + min_so_far = min(curr, min(max_so_far * curr, min_so_far * curr)); |
| 482 | + |
| 483 | + max_so_far = temp_max; |
| 484 | + |
| 485 | + result = max(max_so_far, result); |
| 486 | + } |
| 487 | + |
| 488 | + return result; |
| 489 | + } |
| 490 | +}; |
380 | 491 | ``` |
0 commit comments