Skip to content

Commit

Permalink
add 950 java, progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Nov 16, 2024
1 parent 760e052 commit 5d44b13
Show file tree
Hide file tree
Showing 5 changed files with 239 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@
945 | [Minimum Increment to Make Array Unique](https://leetcode.com/problems/minimum-increment-to-make-array-unique/) | [Python](./leetcode_python/Array/minimum-increment-to-make-array-unique.py) | _O(nlogn)_ | _O(n)_ | Medium |`trick`, `good`| AGAIN**
947 | [Most Stones Removed with Same Row or Column](https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/) | [Python](./leetcode_python/Array/most-stones-removed-with-same-row-or-column.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Array/MostStonesRemovedWithSameRowOrColumn.java) | _O(n)_ | _O(n)_ | Medium | Union Find , dfs, trick,google| AGAIN** (1) (not start)
949 | [Largest Time for Given Digits](https://leetcode.com/problems/largest-time-for-given-digits/) | [Python](./leetcode_python/Array/largest-time-for-given-digits.py) | _O(1)_ | _O(1)_ | Easy || OK*
950 | [Reveal Cards In Increasing Order](https://leetcode.com/problems/reveal-cards-in-increasing-order/) | [Python](./leetcode_python/Array/reveal-cards-in-increasing-order.py) | _O(n)_ | _O(n)_ | Medium || AGAIN (not start)
950 | [Reveal Cards In Increasing Order](https://leetcode.com/problems/reveal-cards-in-increasing-order/) | [Python](./leetcode_python/Array/reveal-cards-in-increasing-order.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Array/RevealCardsInIncreasingOrder.java) | _O(n)_ | _O(n)_ | Medium |queue, 2 pointers, google| AGAIN (not start) (2)
954 | [Array of Doubled Pairs](https://leetcode.com/problems/array-of-doubled-pairs/) | [Python](./leetcode_python/Array/array-of-doubled-pairs.py) | _O(n + klogk)_ | _O(k)_ | Medium |`counter`, `dict`, `trick`| AGAIN**
961 | [N-Repeated Element in Size 2N Array](https://leetcode.com/problems/n-repeated-element-in-size-2n-array/) | [Python](./leetcode_python/Array/n-repeated-element-in-size-2n-array.py) | _O(n)_ | _O(1)_ | Easy || OK
989 | [Add to Array-Form of Integer](https://leetcode.com/problems/add-to-array-form-of-integer/) | [Python](./leetcode_python/Array/add-to-array-form-of-integer.py) | _O(n)_ | _O(1)_ | Easy |add xxx to sum, fb| AGAIN (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 @@
20241116: 833
20241116: 833,950
20241114: 80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)
20241110: 53,210,207
20241109: 444
Expand Down
18 changes: 9 additions & 9 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
2025-01-10 -> ['833']
2025-01-10 -> ['833,950']
2025-01-08 -> ['80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2025-01-04 -> ['53,210,207']
2025-01-03 -> ['444']
2025-01-02 -> ['1188,130,855(again)']
2024-12-28 -> ['900']
2024-12-27 -> ['26,27', '802,1197,26']
2024-12-21 -> ['855,846']
2024-12-20 -> ['833', '932']
2024-12-20 -> ['833,950', '932']
2024-12-18 -> ['80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '951,792']
2024-12-14 -> ['53,210,207', '163,1048']
2024-12-13 -> ['444', '298,729']
2024-12-12 -> ['1188,130,855(again)', '1146']
2024-12-08 -> ['737']
2024-12-07 -> ['833', '900', '686,734,737']
2024-12-07 -> ['833,950', '900', '686,734,737']
2024-12-06 -> ['26,27', '802,1197,26', '353']
2024-12-05 -> ['80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '528,334']
2024-12-03 -> ['1145']
2024-12-01 -> ['53,210,207']
2024-11-30 -> ['444', '855,846', '1145,1219']
2024-11-29 -> ['833', '1188,130,855(again)', '932']
2024-11-29 -> ['833,950', '1188,130,855(again)', '932']
2024-11-27 -> ['80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '951,792', '524,221,889']
2024-11-26 -> ['743,889']
2024-11-25 -> ['837']
2024-11-24 -> ['833', '900']
2024-11-24 -> ['833,950', '900']
2024-11-23 -> ['53,210,207', '26,27', '802,1197,26', '163,1048', '981']
2024-11-22 -> ['80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '444', '298,729', '1087']
2024-11-21 -> ['833', '1188,130,855(again)', '1146']
2024-11-21 -> ['833,950', '1188,130,855(again)', '1146']
2024-11-20 -> ['939']
2024-11-19 -> ['833', '80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2024-11-18 -> ['833', '53,210,207', '430']
2024-11-17 -> ['833', '80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '444', '855,846', '737', '363']
2024-11-19 -> ['833,950', '80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2024-11-18 -> ['833,950', '53,210,207', '430']
2024-11-17 -> ['833,950', '80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '444', '855,846', '737', '363']
2024-11-16 -> ['80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '1188,130,855(again)', '900', '932', '686,734,737', '1032,844,1011']
2024-11-15 -> ['80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '53,210,207', '26,27', '802,1197,26', '353', '947']
2024-11-14 -> ['444', '951,792', '528,334']
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
package LeetCodeJava.Array;

// https://leetcode.com/problems/reveal-cards-in-increasing-order/description/

import java.util.*;

/**
* 950. Reveal Cards In Increasing Order
* Medium
* Topics
* Companies
* You are given an integer array deck. There is a deck of cards where every card has a unique integer. The integer on the ith card is deck[i].
*
* You can order the deck in any order you want. Initially, all the cards start face down (unrevealed) in one deck.
*
* You will do the following steps repeatedly until all cards are revealed:
*
* Take the top card of the deck, reveal it, and take it out of the deck.
* If there are still cards in the deck then put the next top card of the deck at the bottom of the deck.
* If there are still unrevealed cards, go back to step 1. Otherwise, stop.
* Return an ordering of the deck that would reveal the cards in increasing order.
*
* Note that the first entry in the answer is considered to be the top of the deck.
*
*
*
* Example 1:
*
* Input: deck = [17,13,11,2,3,5,7]
* Output: [2,13,3,11,5,17,7]
* Explanation:
* We get the deck in the order [17,13,11,2,3,5,7] (this order does not matter), and reorder it.
* After reordering, the deck starts as [2,13,3,11,5,17,7], where 2 is the top of the deck.
* We reveal 2, and move 13 to the bottom. The deck is now [3,11,5,17,7,13].
* We reveal 3, and move 11 to the bottom. The deck is now [5,17,7,13,11].
* We reveal 5, and move 17 to the bottom. The deck is now [7,13,11,17].
* We reveal 7, and move 13 to the bottom. The deck is now [11,17,13].
* We reveal 11, and move 17 to the bottom. The deck is now [13,17].
* We reveal 13, and move 17 to the bottom. The deck is now [17].
* We reveal 17.
* Since all the cards revealed are in increasing order, the answer is correct.
* Example 2:
*
* Input: deck = [1,1000]
* Output: [1,1000]
*
*
* Constraints:
*
* 1 <= deck.length <= 1000
* 1 <= deck[i] <= 106
* All the values of deck are unique.
*
*/
public class RevealCardsInIncreasingOrder {

// V0
// TODO: implement
// public int[] deckRevealedIncreasing(int[] deck) {
//
// }

// V1-1
// IDEA : Simulation with Queue
// https://leetcode.com/problems/reveal-cards-in-increasing-order/editorial/
/**
* IDEA :
*
* In this solution, we also start by sorting the deck and creating a result array.
*
* How do we know what order to put the cards in?
*
* The result array will not be revealed in order. Instead, the indexes of the result array will be revealed in a certain order.
*
* Input: [17,13,11,2,3,5,7]
* Output: [2,13,3,11,5,17,7]
*
* Order of indexes revealed: 0, 2, 4, 6, 3, 1, 5
*
* We can work backward from the sorted order since we can easily sort the deck in ascending order.
*
* Sorted Order: [2,3,5,7,11,13,17]
*
* We can simulate the revealing process using a queue of indices to find the order the indices will be revealed. We do this by removing the front card from the queue and then moving the next index in the queue to the back. A deque could alternatively be used to simulate this process, but we have chosen to use a queue since we only need to remove cards from the front and add cards to the back.
*
* From the sorted order, we can place each card at the correct index to get the desired output:
*
* Put card 2 at index 0
* Put card 3 at index 2
* Put card 5 at index 4
* Put card 7 at index 6
* Put card 11 at index 3
* Put card 13 at index 1
* Put card 17 at index 5
* We can add cards to the result as we simulate the revealing process with the queue. Each time we remove an index from the queue to reveal a card, we add the next card from the deck to the result at that index.
*
*
*/
/**
* Algorithm
*
*
* step 1) Initialize N to the length of the deck.
*
* step 2) Create a queue to store the indices of the cards,
* and add the indices 0 to N to the queue.
*
* step 3) Sort the deck.
*
* step 4) Initialize an array result of size N to store the answer.
*
* step 5) Loop through the cards, placing each one in the correct spot in result:
* - Set result at the front index in the queue to deck[i].
* - Take the next index in the queue and move it to the back of the queue.
*
* step 6) Return result.
*
*/
public int[] deckRevealedIncreasing_1_1(int[] deck) {
int N = deck.length;
Queue<Integer> queue = new LinkedList<>();

// Create a queue of indexes
/**
* NOTE !!!
*
* put INDEXES into queue
*/
for (int i = 0; i < N; i++) {
queue.add(i);
}

Arrays.sort(deck);

// Put cards at correct index in result
int[] result = new int[N];
for (int i = 0; i < N; i++) {
// Reveal Card and place in result
result[queue.poll()] = deck[i];

// Move next card to bottom
queue.add(queue.poll());
}
return result;
}

// V1-2
// IDEA : 2 POINTERS
// https://leetcode.com/problems/reveal-cards-in-increasing-order/editorial/
public int[] deckRevealedIncreasing_1_2(int[] deck) {
int N = deck.length;
int[] result = new int[N];
boolean skip = false;
int indexInDeck = 0;
int indexInResult = 0;

Arrays.sort(deck);

while (indexInDeck < N) {
// There is an available gap in result
if (result[indexInResult] == 0) {

// Add a card to result
if (!skip) {
result[indexInResult] = deck[indexInDeck];
indexInDeck++;
}

// Toggle skip to alternate between adding and skipping cards
skip = !skip;
}
// Progress to next index of result array
indexInResult = (indexInResult + 1) % N;
}
return result;
}

// V2
// https://leetcode.com/problems/reveal-cards-in-increasing-order/solutions/5001469/faster-lesser-detailed-explaination-sorting-deque-step-by-step-explaination-python-java-c/
public int[] deckRevealedIncreasing_2(int[] deck) {
Arrays.sort(deck); // Sort the deck in increasing order

int n = deck.length;
int[] result = new int[n];
Deque<Integer> indices = new LinkedList<>();

for (int i = 0; i < n; i++) {
indices.add(i); // Initialize deque with indices 0, 1, 2, ..., n-1
}

for (int card : deck) {
int idx = indices.poll(); // Get the next available index
result[idx] = card; // Place the card in the result array
if (!indices.isEmpty()) {
indices.add(indices.poll()); // Move the used index to the end of deque
}
}

return result;
}

// V3
// https://leetcode.com/problems/reveal-cards-in-increasing-order/submissions/1454215743/
public int[] deckRevealedIncreasing_3(int[] deck) {
int n=deck.length;
Arrays.sort(deck);
Deque <Integer> st=new ArrayDeque<>();
st.addFirst(deck[n-1]);
for(int i=n-2;i>=0;i--){
st.addFirst(st.removeLast());
st.addFirst(deck[i]);
}
//we can either create a new array or change the existing since we dont need it right??but it is not recommended

for(int i=0;i<n;i++){
deck[i]=(int)st.removeFirst();
}
return deck;
}

}
7 changes: 7 additions & 0 deletions leetcode_java/src/main/java/dev/workspace5.java
Original file line number Diff line number Diff line change
Expand Up @@ -3193,6 +3193,13 @@ public String findReplaceString(String s, int[] indices, String[] sources, Strin
return null;
}

// LC 950
// https://leetcode.com/problems/reveal-cards-in-increasing-order/
// 5.14 pm - 5.30 pm
public int[] deckRevealedIncreasing(int[] deck) {
return null;
}


}

Expand Down

0 comments on commit 5d44b13

Please sign in to comment.