Skip to content

Commit

Permalink
add 135 java, update progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Nov 8, 2023
1 parent 4a60b96 commit 2b8f959
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1089,7 +1089,7 @@
084| [Largest Rectangle in Histogram](https://leetcode.com/problems/largest-rectangle-in-histogram/)|[Python](./leetcode_python/Greedy/largest-rectangle-in-histogram.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Stack/LargestRectangleInHistogram.java)| _O(n)_ | _O(1)_| Hard |top 100 like, brute force, divide and conquer, good trick, `stack`, LC 085, amazon, fb| AGAIN**** (1)
122| [Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/)| [Python](./leetcode_python/Greedy/best-time-to-buy-and-sell-stock-ii.py) | _O(n)_ | _O(1)_ | Easy |compare with `#309 Best Time to Buy and Sell Stock with Cooldown `, `#714 Best Time to Buy and Sell Stock with Transaction Fee`, `amazon`| OK* (2)
134| [Gas Station](https://leetcode.com/problems/gas-station/)| [Python](./leetcode_python/Greedy/gas-station.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Greedy/GasStation.java) | _O(n)_ | _O(1)_ | Medium|trick, greedy,`amazon`| AGAIN****** (5)
135| [Candy](https://leetcode.com/problems/candy/)| [Python](./leetcode_python/Greedy/candy.py) | _O(n)_ | _O(1)_ | Hard|LC 123, greedy, array, `amazon`| AGAIN (not start)
135| [Candy](https://leetcode.com/problems/candy/)| [Python](./leetcode_python/Greedy/candy.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Greedy/Candy.java) | _O(n)_ | _O(1)_ | Hard|LC 123, greedy, boundary, array, `amazon`| AGAIN (2)
218| [The Skyline Problem](https://leetcode.com/problems/the-skyline-problem/description/)| [Python](./leetcode_python/Greedy/the_skyline_problem.py) | _O(n)_ | _O(1)_ | Hard|brute force, swipe line, Priority Queue, Union Find, Divide-and-Conquer, apple, microsoft| AGAIN (not start)
330| [Patching Array](https://leetcode.com/problems/patching-array/)| [Python](./leetcode_python/Greedy/patching-array.py) | _O(n)_ | _O(1)_ | Hard| array, greedy,`amazon`| AGAIN (not start)
358| [Rearrange String k Distance Apart](https://leetcode.com/problems/rearrange-string-k-distance-apart/)| [Python](./leetcode_python/Greedy/rearrange-string-k-distance-apar.py) | _O(n)_ | _O(1)_ | Medium|heap, greedy, check LC 767| not start
Expand Down
2 changes: 1 addition & 1 deletion data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
20231108: 452,406
20231108: 452,406,135
20231106: 134
20231103: 53
20231006: quick_sort,286
Expand Down
18 changes: 9 additions & 9 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
2024-01-02 -> ['452,406']
2024-01-02 -> ['452,406,135']
2023-12-31 -> ['134']
2023-12-28 -> ['53']
2023-12-12 -> ['452,406']
2023-12-12 -> ['452,406,135']
2023-12-10 -> ['134']
2023-12-07 -> ['53']
2023-11-30 -> ['quick_sort,286']
2023-11-29 -> ['452,406']
2023-11-29 -> ['452,406,135']
2023-11-27 -> ['134', '994']
2023-11-25 -> ['417']
2023-11-24 -> ['53', '133,695']
2023-11-21 -> ['452,406']
2023-11-21 -> ['452,406,135']
2023-11-19 -> ['134']
2023-11-16 -> ['452,406', '53']
2023-11-16 -> ['452,406,135', '53']
2023-11-14 -> ['134']
2023-11-13 -> ['452,406']
2023-11-11 -> ['452,406', '134', '53']
2023-11-10 -> ['452,406']
2023-11-09 -> ['452,406', '134', 'quick_sort,286']
2023-11-13 -> ['452,406,135']
2023-11-11 -> ['452,406,135', '134', '53']
2023-11-10 -> ['452,406,135']
2023-11-09 -> ['452,406,135', '134', 'quick_sort,286']
2023-11-08 -> ['134', '53']
2023-11-07 -> ['134']
2023-11-06 -> ['53', '994']
Expand Down
161 changes: 161 additions & 0 deletions leetcode_java/src/main/java/LeetCodeJava/Greedy/Candy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package LeetCodeJava.Greedy;

// https://leetcode.com/problems/candy/

import java.util.Arrays;

public class Candy {

// V0
// IDEA : GREEDY
// https://www.bilibili.com/video/BV1ev4y1r7wN/?share_source=copy_web&vd_source=49348a1975630e1327042905e52f488a
// TODO : fix it
// public int candy(int[] ratings) {
//
// if (ratings == null || ratings.length == 0){
// return 0;
// }
//
// int ans = 0;
// int[] tmp = new int[]{};
// for (int i = 0; i < ratings.length; i++){
// tmp[i] = 1;
// }
//
// /** TIPS : DEAL WITH 1 DIRECTION AT ONCE */
//
// // step 1) loop from left (compare element left)
// for (int j = 1; j < ratings.length-1; j++){
// if (ratings[j] > ratings[j-1]){
// tmp[j] += 1;
// }
// }
//
// // step 2) loop from right
// for (int k = ratings.length-2; k >= 0; k--){
// if (ratings[k] > ratings[k+1]){
// tmp[k] += 1;
// }
// }
//
// // step 3) get sum
// for (int z=0; z < tmp.length; z ++){
// ans += tmp[z];
// }
//
// return ans;
// }


// V1
// IDEA : BRUTE FORCE
// https://leetcode.com/problems/candy/editorial/
public int candy_2(int[] ratings) {
int[] candies = new int[ratings.length];
Arrays.fill(candies, 1);
boolean hasChanged = true;
while (hasChanged) {
hasChanged = false;
for (int i = 0; i < ratings.length; i++) {
if (i != ratings.length - 1 && ratings[i] > ratings[i + 1] && candies[i] <= candies[i + 1]) {
candies[i] = candies[i + 1] + 1;
hasChanged = true;
}
if (i > 0 && ratings[i] > ratings[i - 1] && candies[i] <= candies[i - 1]) {
candies[i] = candies[i - 1] + 1;
hasChanged = true;
}
}
}
int sum = 0;
for (int candy : candies) {
sum += candy;
}
return sum;
}

// V2
// IDEA : Using two arrays
// https://leetcode.com/problems/candy/editorial/
public int candy_3(int[] ratings) {
int sum = 0;
int[] left2right = new int[ratings.length];
int[] right2left = new int[ratings.length];
Arrays.fill(left2right, 1);
Arrays.fill(right2left, 1);
for (int i = 1; i < ratings.length; i++) {
if (ratings[i] > ratings[i - 1]) {
left2right[i] = left2right[i - 1] + 1;
}
}
for (int i = ratings.length - 2; i >= 0; i--) {
if (ratings[i] > ratings[i + 1]) {
right2left[i] = right2left[i + 1] + 1;
}
}
for (int i = 0; i < ratings.length; i++) {
sum += Math.max(left2right[i], right2left[i]);
}
return sum;
}

// V3
// IDEA : Using one array
// https://leetcode.com/problems/candy/editorial/
public int candy_4(int[] ratings) {
int[] candies = new int[ratings.length];
Arrays.fill(candies, 1);
for (int i = 1; i < ratings.length; i++) {
if (ratings[i] > ratings[i - 1]) {
candies[i] = candies[i - 1] + 1;
}
}
int sum = candies[ratings.length - 1];
for (int i = ratings.length - 2; i >= 0; i--) {
if (ratings[i] > ratings[i + 1]) {
candies[i] = Math.max(candies[i], candies[i + 1] + 1);
}
sum += candies[i];
}
return sum;
}

// V4
// IDEA : Single Pass Approach with Constant Space
// https://leetcode.com/problems/candy/editorial/
public int count(int n) {
return (n * (n + 1)) / 2;
}
public int candy_5(int[] ratings) {
if (ratings.length <= 1) {
return ratings.length;
}
int candies = 0;
int up = 0;
int down = 0;
int oldSlope = 0;
for (int i = 1; i < ratings.length; i++) {
int newSlope = (ratings[i] > ratings[i - 1]) ? 1
: (ratings[i] < ratings[i - 1] ? -1
: 0);

if ((oldSlope > 0 && newSlope == 0) || (oldSlope < 0 && newSlope >= 0)) {
candies += count(up) + count(down) + Math.max(up, down);
up = 0;
down = 0;
}
if (newSlope > 0) {
up++;
} else if (newSlope < 0) {
down++;
} else {
candies++;
}

oldSlope = newSlope;
}
candies += count(up) + count(down) + Math.max(up, down) + 1;
return candies;
}

}

0 comments on commit 2b8f959

Please sign in to comment.