Skip to content

Commit 8d5a662

Browse files
add 950
1 parent 0689fa6 commit 8d5a662

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Your ideas/fixes/algorithms are more than welcome!
4242
|965|[Univalued Binary Tree](https://leetcode.com/problems/univalued-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_965.java) | O(n) | O(h) | |Easy| DFS, recursion|
4343
|961|[N-Repeated Element in Size 2N Array](https://leetcode.com/problems/n-repeated-element-in-size-2n-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_961.java) | O(n) | O(1) | |Easy|
4444
|953|[Verifying an Alien Dictionary](https://leetcode.com/problems/verifying-an-alien-dictionary/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_953.java) | O(1) | O(1) | |Easy|
45+
|950|[Reveal Cards In Increasing Order](https://leetcode.com/problems/reveal-cards-in-increasing-order/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_950.java) | O(nlogn) | O(n) | |Medium|
4546
|944|[Delete Columns to Make Sorted](https://leetcode.com/problems/delete-columns-to-make-sorted/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_944.java) | O(n) | O(1) | |Easy|
4647
|942|[DI String Match](https://leetcode.com/problems/di-string-match/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_942.java) | O(n) | O(n) | |Easy|
4748
|941|[Valid Mountain Array](https://leetcode.com/problems/valid-mountain-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_941.java) | O(n) | O(1) | |Easy|
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayDeque;
4+
import java.util.Arrays;
5+
import java.util.Deque;
6+
7+
/**
8+
* 950. Reveal Cards In Increasing Order
9+
*
10+
* In a deck of cards, every card has a unique integer. You can order the deck in any order you want.
11+
* Initially, all the cards start face down (unrevealed) in one deck.
12+
* Now, you do the following steps repeatedly, until all cards are revealed:
13+
* Take the top card of the deck, reveal it, and take it out of the deck.
14+
* If there are still cards in the deck, put the next top card of the deck at the bottom of the deck.
15+
* If there are still unrevealed cards, go back to step 1. Otherwise, stop.
16+
* Return an ordering of the deck that would reveal the cards in increasing order.
17+
*
18+
* The first entry in the answer is considered to be the top of the deck.
19+
*
20+
* Example 1:
21+
* Input: [17,13,11,2,3,5,7]
22+
* Output: [2,13,3,11,5,17,7]
23+
*
24+
* Explanation:
25+
* We get the deck in the order [17,13,11,2,3,5,7] (this order doesn't matter), and reorder it.
26+
* After reordering, the deck starts as [2,13,3,11,5,17,7], where 2 is the top of the deck.
27+
* We reveal 2, and move 13 to the bottom. The deck is now [3,11,5,17,7,13].
28+
* We reveal 3, and move 11 to the bottom. The deck is now [5,17,7,13,11].
29+
* We reveal 5, and move 17 to the bottom. The deck is now [7,13,11,17].
30+
* We reveal 7, and move 13 to the bottom. The deck is now [11,17,13].
31+
* We reveal 11, and move 17 to the bottom. The deck is now [13,17].
32+
* We reveal 13, and move 17 to the bottom. The deck is now [17].
33+
* We reveal 17.
34+
* Since all the cards revealed are in increasing order, the answer is correct.
35+
*
36+
* Note:
37+
* 1 <= A.length <= 1000
38+
* 1 <= A[i] <= 10^6
39+
* A[i] != A[j] for all i != j
40+
*/
41+
public class _950 {
42+
public static class Solution1 {
43+
public int[] deckRevealedIncreasing(int[] deck) {
44+
Arrays.sort(deck);
45+
Deque<Integer> deque = new ArrayDeque<>();
46+
for (int i = deck.length - 1; i >= 0; i--) {
47+
if (i != deck.length - 1) {
48+
deque.addFirst(deque.pollLast());
49+
}
50+
deque.addFirst(deck[i]);
51+
}
52+
int[] result = new int[deck.length];
53+
int i = 0;
54+
while (!deque.isEmpty()) {
55+
result[i++] = deque.pollFirst();
56+
}
57+
return result;
58+
}
59+
}
60+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._950;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertArrayEquals;
8+
9+
public class _950Test {
10+
private static _950.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _950.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertArrayEquals(new int[] {2, 13, 3, 11, 5, 17, 7},
20+
solution1.deckRevealedIncreasing(new int[] {17, 13, 11, 2, 3, 5, 7}));
21+
}
22+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy