Skip to content

Commit

Permalink
add 370 java, update progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Dec 21, 2024
1 parent a023b94 commit 5fcae7a
Show file tree
Hide file tree
Showing 7 changed files with 370 additions and 200 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@
308| [Range Sum Query 2D - Mutable](https://leetcode.com/problems/range-sum-query-2d-mutable/) | [Python](./leetcode_python/Array/range-sum-query-2d-mutable.py) | _O(n * (c+1))_ | _O(1)_ | Hard |LC 303, pre-sum, matrix, tree, 🔒, `google`| not start
311| [Sparse Matrix Multiplication](https://leetcode.com/problems/sparse-matrix-multiplication/) | [Python](./leetcode_python/Array/sparse-matrix-multiplication.py) | _O(m * n * l)_ | _O(m * l)_ | Medium |🔒, matrix, `basic`, `fb`| OK**** (4)
334| [Increasing Triplet Subsequence](https://leetcode.com/problems/increasing-triplet-subsequence/) | [Python](./leetcode_python/Array/increasing-triplet-subsequence.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Array/IncreasingTripletSubsequence.java) | _O(n)_ | _O(1)_ | Medium |AGAIN, check `# 300 Longest Increasing Subsequence`, `trick`, `fb`, google|OK***** (but again) (4)
370| [Range Addition](https://leetcode.com/problems/range-addition/) | [Python](./leetcode_python/Array/range-addition.py) | _O(k + n)_ | _O(1)_ | Medium |prefix sum, array, 🔒, check `# 598 Range Addition II`, `amazon`| AGAIN************** (4) (MUST)
370| [Range Addition](https://leetcode.com/problems/range-addition/) | [Python](./leetcode_python/Array/range-addition.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Array/RangeAddition.java) | _O(k + n)_ | _O(1)_ | Medium |prefix, diff array, check `# 598 Range Addition II`, `amazon`, LC 1109, google| AGAIN************** (4) (MUST)
384| [Shuffle an Array](https://leetcode.com/problems/shuffle-an-array/) | [Python](./leetcode_python/Array/shuffle-an-array.py) | _O(n)_ | _O(n)_ | Medium | EPI, `trick` | OK*
396| [Rotate Function](https://leetcode.com/problems/rotate-function/) | [Python](./leetcode_python/Array/rotate-function.py) | _O(n)_ | _O(1)_ | Medium |can pass, math, `trick`, `amazon`| AGAIN*** (3)
412| [Fizz Buzz](https://leetcode.com/problems/fizz-buzz/) | [Python](./leetcode_python/Array/fizz-buzz.py), [Scala](./leetcode_scala/Array/fizzBuzz.scala)| _O(n)_ | _O(1)_ | Easy || OK
Expand Down Expand Up @@ -317,7 +317,7 @@
1014 | [Best Sightseeing Pair](https://leetcode.com/problems/best-sightseeing-pair/) | [Python](./leetcode_python/Array/best_sightseeing_pair.py) | | | Medium |dp, array, good basic, Spotify| AGAIN (not start)
1027 | [Longest Arithmetic Subsequence](https://leetcode.com/problems/longest-arithmetic-subsequence/) | [Python](./leetcode_python/Array/longest-arithmetic-subsequence.py) | | | Medium |dp, hash table, trick, google, amazon| AGAIN*** (not start)
1041 | [Robot Bounded In Circle](https://leetcode.com/problems/robot-bounded-in-circle/) | [Python](./leetcode_python/Array/robot-bounded-in-circle.py) | | | Medium |array, math, `amazon`| AGAIN** (2)
1109 | [ Corporate Flight Bookings](https://leetcode.com/problems/corporate-flight-bookings/) | [Python](./leetcode_python/Array/corporate-flight-bookings.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Array/CorporateFlightBookings.java) | | |Medium |difference array, good basic, `amazon`, google|AGAIN********* (MUST) (3)
1109 | [ Corporate Flight Bookings](https://leetcode.com/problems/corporate-flight-bookings/) | [Python](./leetcode_python/Array/corporate-flight-bookings.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Array/CorporateFlightBookings.java) | | |Medium |LC 370, difference array, good basic, `amazon`, google|AGAIN********* (MUST) (3)
1248 | [Count Number of Nice Subarrays](https://leetcode.com/problems/count-number-of-nice-subarrays/) | [Python](./leetcode_python/Array/count-number-of-nice-subarrays.py) | | | Medium |LC 828, hash map, Prefix sum, dict, windown, good basic, deque, array, `amazon`| AGAIN************** (3) (MUST)
1275 | [Find Winner on a Tic Tac Toe Game](https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game/) | [Python](./leetcode_python/Array/find-winner-on-a-tic-tac-toe-game.py) | | | Easy |`amazon`| AGAIN (not start)
1567 | [Maximum Length of Subarray With Positive Product](https://leetcode.com/problems/maximum-length-of-subarray-with-positive-product/) | [Python](./leetcode_python/Array/maximum-length-of-subarray-with-positive-product.py) | | | Medium |good trick, array, dp, 2 pointers, amazon| AGAIN**** (1) (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 @@
20241221:
20241221: 370
20241220: 815,871,593,1109
20241214: 560,523
20241208: 304,853,325
Expand Down
17 changes: 9 additions & 8 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
2025-02-14 -> ['370']
2025-02-13 -> ['815,871,593,1109']
2025-02-07 -> ['560,523']
2025-02-01 -> ['304,853,325']
2025-01-26 -> ['370(todo)']
2025-01-24 -> ['34,767']
2025-01-24 -> ['370', '34,767']
2025-01-23 -> ['815,871,593,1109']
2025-01-20 -> ['722,380']
2025-01-19 -> ['33,81']
2025-01-17 -> ['560,523', '253']
2025-01-16 -> ['776,31']
2025-01-15 -> ['004(todo),34(todo),162(todo),275(todo)']
2025-01-14 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2025-01-11 -> ['304,853,325', '394']
2025-01-11 -> ['370', '304,853,325', '394']
2025-01-10 -> ['815,871,593,1109', '833,950']
2025-01-05 -> ['370(todo)']
2025-01-04 -> ['560,523', '53,210,207']
2025-01-03 -> ['34,767', '444']
2025-01-03 -> ['370', '34,767', '444']
2025-01-02 -> ['815,871,593,1109', '1188,130,855(again)']
2024-12-30 -> ['722,380']
2024-12-29 -> ['304,853,325', '33,81']
2024-12-29 -> ['370', '304,853,325', '33,81']
2024-12-28 -> ['815,871,593,1109', '900']
2024-12-27 -> ['560,523', '253', '26,27', '802,1197,26']
2024-12-26 -> ['776,31']
2024-12-26 -> ['370', '776,31']
2024-12-25 -> ['815,871,593,1109', '004(todo),34(todo),162(todo),275(todo)']
2024-12-24 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2024-12-23 -> ['815,871,593,1109', '370(todo)']
2024-12-22 -> ['815,871,593,1109', '560,523']
2024-12-24 -> ['370', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2024-12-23 -> ['370', '815,871,593,1109', '370(todo)']
2024-12-22 -> ['370', '815,871,593,1109', '560,523']
2024-12-21 -> ['815,871,593,1109', '304,853,325', '34,767', '394', '855,846']
2024-12-20 -> ['833,950', '932']
2024-12-19 -> ['560,523']
Expand Down
83 changes: 83 additions & 0 deletions leetcode_java/src/main/java/LeetCodeJava/Array/RangeAddition.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package LeetCodeJava.Array;

// https://leetcode.com/problems/range-addition/description/
// https://leetcode.ca/all/370.html

import java.util.Arrays;

/**
* 370. Range Addition Assume you have an array of length n initialized with all 0's and are given k
* update operations.
*
* <p>Each operation is represented as a triplet: [startIndex, endIndex, inc] which increments each
* element of subarray A[startIndex ... endIndex] (startIndex and endIndex inclusive) with inc.
*
* <p>Return the modified array after all k operations were executed.
*
* <p>Example:
*
* <p>Input: length = 5, updates = [[1,3,2],[2,4,3],[0,2,-2]] Output: [-2,0,3,5,3] Explanation:
*
* <p>Initial state: [0,0,0,0,0]
*
* <p>After applying operation [1,3,2]: [0,2,2,2,0]
*
* <p>After applying operation [2,4,3]: [0,2,5,5,3]
*
* <p>After applying operation [0,2,-2]: [-2,0,3,5,3] Difficulty: Medium Lock: Prime Company: Google
*/
public class RangeAddition {

// V0
// IDEA : DIFFERENCE ARRAY
// LC 1109
// https://github.com/yennanliu/CS_basics/blob/master/leetcode_java/src/main/java/LeetCodeJava/Array/CorporateFlightBookings.java
public static int[] getModifiedArray(int length, int[][] updates) {

int[] tmp = new int[length + 1]; // or new int[length]; both works
for (int[] x : updates) {
int start = x[0];
int end = x[1];
int amount = x[2];

// add
tmp[start] += amount;

// subtract (remove the "adding affect" on "NEXT" element)
/**
* NOTE !!!
*
* <p>we remove the "adding affect" on NEXT element (e.g. end + 1)
*/
if (end + 1 < length) { // NOTE !!! use `end + 1`
tmp[end + 1] -= amount;
}
}

// prepare final result
for (int i = 1; i < tmp.length; i++) {
tmp[i] += tmp[i - 1];
}

return Arrays.copyOfRange(tmp, 0, length); // return the sub array between 0, lengh index
}

// V1
// https://leetcode.ca/2016-12-04-370-Range-Addition/
public int[] getModifiedArray_1(int length, int[][] updates) {
int[] d = new int[length];
for (int[] e : updates) {
int l = e[0], r = e[1], c = e[2];
d[l] += c;
if (r + 1 < length) {
d[r + 1] -= c;
}
}
for (int i = 1; i < length; ++i) {
d[i] += d[i - 1];
}
return d;
}

// V2
}
Loading

0 comments on commit 5fcae7a

Please sign in to comment.