Skip to content

Commit d9f34b2

Browse files
committed
feat: solve No.210,950
1 parent 4b25395 commit d9f34b2

File tree

2 files changed

+115
-12
lines changed

2 files changed

+115
-12
lines changed

201-300/210. Course Schedule II.md

+10-12
Original file line numberDiff line numberDiff line change
@@ -74,22 +74,20 @@ var findOrder = function(numCourses, prerequisites) {
7474
requiringMap[prerequisites[i][0]]++;
7575
requiredByMap[prerequisites[i][1]].push(prerequisites[i][0]);
7676
}
77-
var queue = new MinPriorityQueue();
77+
var queue = [];
7878
for (var j = 0; j < numCourses; j++) {
79-
queue.enqueue(j, requiringMap[j]);
79+
requiringMap[j] === 0 && queue.push(j);
8080
}
8181
var res = [];
82-
while (queue.size()) {
83-
var item = queue.dequeue();
84-
if (requiringMap[item.element] !== item.priority) continue;
85-
if (item.priority !== 0) return [];
86-
res.push(item.element);
87-
for (var k = 0; k < requiredByMap[item.element].length; k++) {
88-
requiringMap[requiredByMap[item.element][k]]--;
89-
queue.enqueue(requiredByMap[item.element][k], requiringMap[requiredByMap[item.element][k]]);
82+
while (queue.length) {
83+
var course = queue.pop();
84+
res.push(course);
85+
for (var k = 0; k < requiredByMap[course].length; k++) {
86+
requiringMap[requiredByMap[course][k]]--;
87+
requiringMap[requiredByMap[course][k]] === 0 && queue.push(requiredByMap[course][k]);
9088
}
9189
}
92-
return res;
90+
return res.length === numCourses ? res : [];
9391
};
9492
```
9593

@@ -99,5 +97,5 @@ nope.
9997

10098
**Complexity:**
10199

102-
* Time complexity : O(n * log(n)).
100+
* Time complexity : O(n).
103101
* Space complexity : O(n).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# 950. Reveal Cards In Increasing Order
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Array, Queue, Sorting, Simulation.
5+
- Similar Questions: .
6+
7+
## Problem
8+
9+
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]`.
10+
11+
You can order the deck in any order you want. Initially, all the cards start face down (unrevealed) in one deck.
12+
13+
You will do the following steps repeatedly until all cards are revealed:
14+
15+
16+
17+
- Take the top card of the deck, reveal it, and take it out of the deck.
18+
19+
- If there are still cards in the deck then put the next top card of the deck at the bottom of the deck.
20+
21+
- If there are still unrevealed cards, go back to step 1. Otherwise, stop.
22+
23+
24+
Return **an ordering of the deck that would reveal the cards in increasing order**.
25+
26+
**Note** that the first entry in the answer is considered to be the top of the deck.
27+
28+
 
29+
Example 1:
30+
31+
```
32+
Input: deck = [17,13,11,2,3,5,7]
33+
Output: [2,13,3,11,5,17,7]
34+
Explanation:
35+
We get the deck in the order [17,13,11,2,3,5,7] (this order does not matter), and reorder it.
36+
After reordering, the deck starts as [2,13,3,11,5,17,7], where 2 is the top of the deck.
37+
We reveal 2, and move 13 to the bottom. The deck is now [3,11,5,17,7,13].
38+
We reveal 3, and move 11 to the bottom. The deck is now [5,17,7,13,11].
39+
We reveal 5, and move 17 to the bottom. The deck is now [7,13,11,17].
40+
We reveal 7, and move 13 to the bottom. The deck is now [11,17,13].
41+
We reveal 11, and move 17 to the bottom. The deck is now [13,17].
42+
We reveal 13, and move 17 to the bottom. The deck is now [17].
43+
We reveal 17.
44+
Since all the cards revealed are in increasing order, the answer is correct.
45+
```
46+
47+
Example 2:
48+
49+
```
50+
Input: deck = [1,1000]
51+
Output: [1,1000]
52+
```
53+
54+
 
55+
**Constraints:**
56+
57+
58+
59+
- `1 <= deck.length <= 1000`
60+
61+
- `1 <= deck[i] <= 106`
62+
63+
- All the values of `deck` are **unique**.
64+
65+
66+
67+
## Solution
68+
69+
```javascript
70+
/**
71+
* @param {number[]} deck
72+
* @return {number[]}
73+
*/
74+
var deckRevealedIncreasing = function(deck) {
75+
deck.sort((a, b) => a - b);
76+
var res = Array(deck.length);
77+
var i = 0;
78+
var j = 0;
79+
var skip = false;
80+
while (i < deck.length) {
81+
// find space for card
82+
if (res[j] === undefined) {
83+
// place card if it's not skip round
84+
if (!skip) {
85+
res[j] = deck[i];
86+
i++;
87+
}
88+
// revert skip flag
89+
skip = !skip;
90+
}
91+
// try next place
92+
j = (j + 1) % deck.length;
93+
}
94+
return res;
95+
};
96+
```
97+
98+
**Explain:**
99+
100+
nope.
101+
102+
**Complexity:**
103+
104+
* Time complexity : O(n * log(n)).
105+
* Space complexity : O(n).

0 commit comments

Comments
 (0)