Skip to content

Commit 6fdf5df

Browse files
author
Fettes
committed
update greedy
1 parent a7e2faa commit 6fdf5df

9 files changed

+279
-3
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* @lc app=leetcode id=121 lang=java
3+
*
4+
* [121] Best Time to Buy and Sell Stock
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public int maxProfit(int[] prices) {
10+
int minPrice = Integer.MAX_VALUE;
11+
int maxProfit = 0;
12+
13+
for (int i = 0; i < prices.length; i++) {
14+
if (prices[i] < minPrice) {
15+
minPrice = prices[i];
16+
} else if (prices[i] - minPrice > maxProfit) {
17+
maxProfit = prices[i] - minPrice;
18+
}
19+
}
20+
21+
return maxProfit;
22+
}
23+
}
24+
// @lc code=end
25+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* @lc app=leetcode id=81 lang=java
3+
*
4+
* [81] Search in Rotated Sorted Array II
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public boolean search(int[] nums, int target) {
10+
int low = 0;
11+
int high = nums.length - 1;
12+
13+
while (low <= high) {
14+
int mid = low + (high - low) / 2;
15+
if (nums[mid] == target) {
16+
return true;
17+
}
18+
if (nums[mid] < nums[low]) {
19+
if (target > nums[mid] && target <= nums[high]) {
20+
low = mid + 1;
21+
} else {
22+
high = mid - 1;
23+
}
24+
} else if (nums[mid] > nums[low]) {
25+
if (target < nums[mid] && target >= nums[low]) {
26+
high = mid - 1;
27+
} else {
28+
low = mid + 1;
29+
}
30+
} else {
31+
low = low + 1;
32+
}
33+
}
34+
return false;
35+
}
36+
}
37+
// @lc code=end
38+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* @lc app=leetcode id=122 lang=java
3+
*
4+
* [122] Best Time to Buy and Sell Stock II
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public int maxProfit2(int[] prices) {
10+
int valley = prices[0];
11+
int peak = prices[0];
12+
int maxProfit = 0;
13+
14+
for (int i = 0; i < prices.length - 1; i++) {
15+
while (i < prices.length - 1 && prices[i] >= prices[i + 1]) i++;
16+
valley = prices[i];
17+
18+
while (i < prices.length - 1 && prices[i] <= prices[i + 1]) i++;
19+
peak = prices[i];
20+
21+
maxProfit += peak - valley;
22+
}
23+
return maxProfit;
24+
}
25+
26+
public int maxProfit(int[] prices) {
27+
int maxProfit = 0;
28+
29+
for (int i = 1; i < prices.length; i++) {
30+
if (prices[i] > prices[i - 1]) {
31+
maxProfit += prices[i] - prices[i - 1];
32+
}
33+
}
34+
return maxProfit;
35+
}
36+
}
37+
// @lc code=end
38+

Greedy Algorithm/45.jump-game-ii.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* @lc app=leetcode id=45 lang=java
3+
*
4+
* [45] Jump Game II
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public int jump(int[] nums) {
10+
int count = 0;
11+
//initially, the start point is 0.
12+
int start = 0;
13+
//The upper bound should be 1 because only the first point is availble at first.
14+
int end = 1;
15+
16+
while (end < nums.length) {
17+
18+
int maxLength = 0;
19+
for (int i = start; i < end; i++) {
20+
//find the maximum length from the start point to end point
21+
maxLength = Math.max(maxLength, i + nums[i]);
22+
}
23+
//could be the minimum length of last jump, could just be the 'end' last time
24+
start = end;
25+
//the maximum length of last jump
26+
end = maxLength + 1;
27+
count++;
28+
}
29+
return count;
30+
}
31+
}
32+
// @lc code=end
33+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* @lc app=leetcode id=455 lang=java
3+
*
4+
* [455] Assign Cookies
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public int findContentChildren(int[] g, int[] s) {
10+
Arrays.sort(g);
11+
Arrays.sort(s);
12+
int child = 0;
13+
int cookie = 0;
14+
15+
while (child < g.length && cookie < s.length) {
16+
if (g[child] <= s[cookie]) {
17+
child++;
18+
}
19+
cookie++;
20+
}
21+
return child;
22+
}
23+
}
24+
// @lc code=end
25+

Greedy Algorithm/55.jump-game.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* @lc app=leetcode id=55 lang=java
3+
*
4+
* [55] Jump Game
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public boolean canJump(int[] nums) {
10+
int maxLength = 0;
11+
for (int i = 0; i < nums.length; i++) {
12+
if (i > maxLength) {
13+
return false;
14+
}
15+
maxLength = Math.max(maxLength, i + nums[i]);
16+
}
17+
return true;
18+
}
19+
}
20+
// @lc code=end
21+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* @lc app=leetcode id=860 lang=java
3+
*
4+
* [860] Lemonade Change
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public boolean lemonadeChange(int[] bills) {
10+
int fiveNum = 0;
11+
int tenNum = 0;
12+
13+
for (int i = 0; i < bills.length; i++) {
14+
if (bills[i] == 5) {
15+
fiveNum++;
16+
}
17+
if (bills[i] == 10) {
18+
if (fiveNum == 0) return false;
19+
fiveNum--;
20+
tenNum++;
21+
}
22+
if (bills[i] == 20) {
23+
if (fiveNum > 0 && tenNum > 0) {
24+
fiveNum--;
25+
tenNum--;
26+
} else if (tenNum == 0 && fiveNum >= 3) {
27+
fiveNum -= 3;
28+
} else {
29+
return false;
30+
}
31+
}
32+
}
33+
return true;
34+
}
35+
}
36+
// @lc code=end
37+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* @lc app=leetcode id=874 lang=java
3+
*
4+
* [874] Walking Robot Simulation
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public int robotSim(int[] commands, int[][] obstacles) {
10+
Set<String> set = new HashSet<>();
11+
for (int[] ob : obstacles) {
12+
set.add(ob[0] + "," + ob[1]);
13+
}
14+
15+
//follow the order with north->east->south->west
16+
int[][] directions = new int[][]{{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
17+
18+
int dir = 0;
19+
int x = 0;
20+
int y = 0;
21+
int result = 0;
22+
23+
for (int com : commands) {
24+
if (com == -1) {
25+
dir = (dir + 1) % 4;
26+
}else if (com == -2) {
27+
dir = (dir + 3) % 4;
28+
} else {
29+
while (com > 0) {
30+
int newX = x + directions[dir][0];
31+
int newY = y + directions[dir][1];
32+
String next = newX + "," + newY;
33+
34+
if (set.contains(next)) {
35+
break;
36+
}
37+
x = newX;
38+
y = newY;
39+
com--;
40+
}
41+
}
42+
result = Math.max(result, x * x + y * y);
43+
}
44+
45+
return result;
46+
}
47+
}
48+
// @lc code=end
49+

README.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#<span id="0016">0016 </span> | [3Sum Closest](https://leetcode.com/problems/3sum-closest/) | [View](Array/16.3-sum-closest.java) | Medium | [3Sum](#0015), [3Sum Smaller](#0259)
1818
#<span id="0167">0167 </span> | [Two Sum II](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/) | [View](Array/167.two-sum-ii-input-array-is-sorted.java) | Easy | [Two Sum](#0001), [Two Sum IV](#0653), [Two Sum Less Than K](#1099)
1919
#<span id="0170">0170 </span> | [Two Sum III](https://leetcode.com/problems/two-sum-iii-data-structure-design/) | [View](Array/170.two-sum-iii-data-structure-design.java) | Easy | [Two Sum](#0001), [Unique Word Abbreviation](#0288), [Two Sum IV](#0653)
20+
#<span id="0121">0121 </span> | [Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/) | [View](Array/121.best-time-to-buy-and-sell-stock.java) | Easy | [Maximum Subarray](#0053), [Best Time to Buy and Sell Stock II](#0122), [Best Time to Buy and Sell Stock III](#0123), [Best Time to Buy and Sell Stock IV](#0188), [Best Time to Buy and Sell Stock with Cooldown](#0309)
2021
#<span id="0259">0259 </span> | [3Sum Smaller](https://leetcode.com/problems/3sum-smaller/) | [View](Array/259.3-sum-smaller.java) | Medium | [3Sum](#0015), [3Sum Closest](#0016), [Valid Triangle Number](#0611), [Two Sum Less Than K](#1099)
2122
#<span id="1099">1099 </span> | [Two Sum Less Than K](https://leetcode.com/problems/two-sum-less-than-k/) | [View](Array/1099.two-sum-less-than-k.java) | Easy | [Two Sum](#0001), [Two Sum II](#0167), [3Sum Smaller](#0259), [Subarray Product Less Than K](#0713)
2223
#<span id="0442"> 0442 </span> | [Find All Duplicates in an Array](https://leetcode.com/problems/find-all-duplicates-in-an-array/) | [View](./Array/FindAllDuplicatesInAnArray.cpp) | Medium |||
@@ -27,8 +28,7 @@
2728
1277 | [Count Square Submatrices with All Ones](https://leetcode.com/problems/count-square-submatrices-with-all-ones/) | [View](./Array/CountSquareSubmatricesWithAllOnes.cpp) | Medium |||
2829
1475 | [Final Prices With a Special Discount in a Shop](https://leetcode.com/problems/final-prices-with-a-special-discount-in-a-shop/) | [View](./Array/FinalPricesWithSpecialDiscountInAShop.cpp) | Easy |||
2930
0287 | [Find the Duplicate Number](https://leetcode.com/problems/find-the-duplicate-number/) | [View](./Array/FindTheDuplicateNumber.cpp) | Medium |||
30-
1481 | [Least Number of Unique Integers after K Removals](https://leetcode.com/problems/least-number-of-unique-integers-after-k-removals/) | [View](./Array/LeastNumberOfUniqueIntegersAfterKRemovals.cpp) | Medium
31-
|||
31+
1481 | [Least Number of Unique Integers after K Removals](https://leetcode.com/problems/least-number-of-unique-integers-after-k-removals/) | [View](./Array/LeastNumberOfUniqueIntegersAfterKRemovals.cpp) | Medium |||
3232
0004 | [Median of Two Sorted Arrays](https://leetcode.com/problems/median-of-two-sorted-arrays/) | [View](./Array/MedianOfTwoSortedArrays.cpp) | Hard |||
3333
0918 | [Maximum Sum Circular Subarray](https://leetcode.com/problems/maximum-sum-circular-subarray/) | [View](./Array/MaximumSumCircularSubarray.cpp) | Medium |||
3434
0496 | [Next Greater Element I](https://leetcode.com/problems/next-greater-element-i/) | [View](./Array/NextGreaterElementI.cpp) | Easy |||
@@ -62,11 +62,11 @@
6262
#<span id="0034">0034 </span> | [Find First and Last Position of Element in Sorted Array](https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/) | [View](Binary%20Seach/34.find-first-and-last-position-of-element-in-sorted-array.java) | Medium | [First Bad Version](#0278)
6363
#<span id="0069">0069 </span> | [Sqrt(x)](https://leetcode.com/problems/sqrtx/) | [View](Binary%20Seach/69.sqrt-x.java) | Easy | [Pow(x, n)](#0050), [Valid Perfect Square](#0367)
6464
#<span id="0074">0074 </span> | [Search a 2D Matrix](https://leetcode.com/problems/search-a-2d-matrix/) | [View](Binary%20Seach/74.search-a-2-d-matrix.java) | Medium | [Search a 2D Matrix II](#0240)
65+
#<span id="0081">0081 </span> | [Search in Rotated Sorted Array II](https://leetcode.com/problems/search-in-rotated-sorted-array-ii/) | [View](Binary%20Seach/81.search-in-rotated-sorted-array-ii.java) | Medium | [Search in Rotated Sorted Array](#0033)
6566
#<span id="0153">0153 </span> | [Find Minimum in Rotated Sorted Array](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/) | [View](Binary%20Seach/153.find-minimum-in-rotated-sorted-array.java) | Medium | [Search in Rotated Sorted Array](#0033), [Find Minimum in Rotated Sorted Array II](#154)
6667
#<span id="0154">0154 </span> | [Find Minimum in Rotated Sorted Array II](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/) | [View](Binary%20Seach/154.find-minimum-in-rotated-sorted-array-ii.java) | Hard | [Find Minimum in Rotated Sorted Array](#0153)
6768
#<span id="0240">0240 </span> | [Search a 2D Matrix II](https://leetcode.com/problems/search-a-2d-matrix-ii/) | [View](Binary%20Seach/240.search-a-2-d-matrix-ii.java) | Medium | [Search a 2D Matrix](#0074)
6869
#<span id="0367">0367 </span> | [Valid Perfect Square](https://leetcode.com/problems/valid-perfect-square/) | [View](Binary%20Seach/367.valid-perfect-square.java) | Easy | [Sqrt(x)](#0069), [Sum of Square Numbers](#0633)
69-
0034 | [Find First and Last Position of Element in Sorted Array](https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/) | [View](./BinarySearch/FindFirstAndLastPositionOfElementInSortedArray.cpp) | Medium |||
7070
0162 | [Find Peak Element](https://leetcode.com/problems/find-peak-element/) | [View](./BinarySearch/FindPeakElement.cpp) | Medium |||
7171
0744 | [Find Smallest Letter Greater Than Target](https://leetcode.com/problems/find-smallest-letter-greater-than-target/) | [View](./BinarySearch/FindSmallestLetterGreaterThanTarget.cpp) | Easy |||
7272
0275 | [H-Index II](https://leetcode.com/problems/h-index-ii/) | [View](./BinarySearch/H-IndexII.cpp) | Medium |||
@@ -100,6 +100,16 @@
100100
1035 | [Uncrossed Lines](https://leetcode.com/problems/uncrossed-lines/) | [View](./DP/UncrossedLines.cpp) | Medium |||
101101
0062 | [Unique Paths](https://leetcode.com/problems/unique-paths/) | [View](./DP/UniquePaths.cpp) | Medium |||
102102

103+
## Greedy Algorithm
104+
| ID | Title | Solution | Difficulty | Similar Questions |
105+
|----- |---------------- | --------------- |-------------- | ----------------- |
106+
#<span id="0045">0045 </span> | [Jump Game II](https://leetcode.com/problems/jump-game-ii/) | [View](Greedy%20Algorithm/45.jump-game-ii.java) | Hard | [Jump Game](#0055), [Jump Game III](#1306)
107+
#<span id="0055">0055 </span> | [Jump Game](https://leetcode.com/problems/jump-game/) | [View](Greedy%20Algorithm/55.jump-game.java) | Medium | [Jump Game II](#0045), [Jump Game III](#1306)
108+
#<span id="0121">0122 </span> | [Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/) | [View](Greedy%20Algorithm/122.best-time-to-buy-and-sell-stock-ii.java) | Easy |
109+
#<span id="0455">0455 </span> | [Assign Cookies](https://leetcode.com/problems/assign-cookies/) | [View](Greedy%20Algorithm/455.assign-cookies.java) | Easy |
110+
#<span id="0860">0860 </span> | [Lemonade Change](https://leetcode.com/problems/lemonade-change/) | [View](Greedy%20Algorithm/860.lemonade-change.java) | Easy | [Best Time to Buy and Sell Stock](#0121), [Best Time to Buy and Sell Stock III](#0123), [Best Time to Buy and Sell Stock IV](#0188), [Best Time to Buy and Sell Stock with Cooldown](#0309), [Best Time to Buy and Sell Stock with Transaction Fee](#0714)
111+
#<span id="0874">0874 </span> | [Walking Robot Simulation](https://leetcode.com/problems/walking-robot-simulation/) | [View](Greedy%20Algorithm/874.walking-robot-simulation.java) | Easy |
112+
103113
## Linked List
104114
| # | Title | Solution | Difficulty |
105115
|-----|---------------- | --------------- |------------- |

0 commit comments

Comments
 (0)