Skip to content

Commit 7b718be

Browse files
committed
Dec 3, 2018
1 parent 58e09e4 commit 7b718be

File tree

2 files changed

+181
-0
lines changed

2 files changed

+181
-0
lines changed

206__easy__reverse-linked-list.js

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
3+
Reverse a singly linked list.
4+
5+
Example:
6+
7+
Input: 1->2->3->4->5->NULL
8+
Output: 5->4->3->2->1->NULL
9+
10+
Follow up:
11+
A linked list can be reversed either iteratively or recursively. Could you implement both?
12+
13+
*/
14+
/**
15+
* Definition for singly-linked list.
16+
* function ListNode(val) {
17+
* this.val = val;
18+
* this.next = null;
19+
* }
20+
*/
21+
/**
22+
* @param {ListNode} head
23+
* @return {ListNode}
24+
*/
25+
/**
26+
*
27+
* O(n) time
28+
* O(1) space
29+
* Runtime: 56 ms, faster than 100.00%
30+
*/
31+
var reverseList = function(head) {
32+
let prev = null;
33+
while (head !== null) {
34+
let next = head.next; // hold next node
35+
head.next = prev; // reverse this and next (next -> curr)
36+
prev = head; // head is the prev of next now
37+
head = next; // move to next node (continue reverse)
38+
}
39+
return prev;
40+
};
41+
/**
42+
*
43+
* O(n) time
44+
* O(n) space
45+
* Runtime: 56 ms, faster than 100.00%
46+
*/
47+
var reverseList = function(head) {
48+
if (!head) return null; // end of linked list
49+
if (head.next === null) return head; // last node in linked list
50+
51+
let newHead = reverseList(head.next); // find the head of reversed linked list
52+
53+
head.next.next = head; // reverse (next -> curr)
54+
head.next = null; // curr next should be none
55+
56+
return newHead;
57+
};

489__hard__robot-room-cleaner.js

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/**
2+
3+
Given a robot cleaner in a room modeled as a grid.
4+
5+
Each cell in the grid can be empty or blocked.
6+
7+
The robot cleaner with 4 given APIs can move forward, turn left or turn right. Each turn it made is 90 degrees.
8+
9+
When it tries to move into a blocked cell, its bumper sensor detects the obstacle and it stays on the current cell.
10+
11+
Design an algorithm to clean the entire room using only the 4 given APIs shown below.
12+
13+
interface Robot {
14+
// returns true if next cell is open and robot moves into the cell.
15+
// returns false if next cell is obstacle and robot stays on the current cell.
16+
boolean move();
17+
18+
// Robot will stay on the same cell after calling turnLeft/turnRight.
19+
// Each turn will be 90 degrees.
20+
void turnLeft();
21+
void turnRight();
22+
23+
// Clean the current cell.
24+
void clean();
25+
}
26+
Example:
27+
28+
Input:
29+
room = [
30+
[1,1,1,1,1,0,1,1],
31+
[1,1,1,1,1,0,1,1],
32+
[1,0,1,1,1,1,1,1],
33+
[0,0,0,1,0,0,0,0],
34+
[1,1,1,1,1,1,1,1]
35+
],
36+
row = 1,
37+
col = 3
38+
39+
Explanation:
40+
All grids in the room are marked by either 0 or 1.
41+
0 means the cell is blocked, while 1 means the cell is accessible.
42+
The robot initially starts at the position of row=1, col=3.
43+
From the top left corner, its position is one row below and three columns right.
44+
45+
*/
46+
/**
47+
* // This is the robot's control interface.
48+
* // You should not implement it, or speculate about its implementation
49+
* function Robot() {
50+
*
51+
* // Returns true if the cell in front is open and robot moves into the cell.
52+
* // Returns false if the cell in front is blocked and robot stays in the current cell.
53+
* @return {boolean}
54+
* this.move = function() {
55+
* ...
56+
* };
57+
*
58+
* // Robot will stay in the same cell after calling turnLeft/turnRight.
59+
* // Each turn will be 90 degrees.
60+
* @return {void}
61+
* this.turnLeft = function() {
62+
* ...
63+
* };
64+
*
65+
* // Robot will stay in the same cell after calling turnLeft/turnRight.
66+
* // Each turn will be 90 degrees.
67+
* @return {void}
68+
* this.turnRight = function() {
69+
* ...
70+
* };
71+
*
72+
* // Clean the current cell.
73+
* @return {void}
74+
* this.clean = function() {
75+
* ...
76+
* };
77+
* };
78+
*/
79+
/**
80+
* @param {Robot} robot
81+
* @return {void}
82+
*/
83+
84+
/**
85+
*
86+
* O(n) time
87+
* O(n) space
88+
*
89+
* Runtime: 76 ms, faster than 98.80%
90+
*/
91+
var cleanRoom = function(robot) {
92+
const doneSet = new Set();
93+
// [x, y] up, right, down, left
94+
const dirs = [[0, 1], [1, 0], [0, -1], [-1, 0]];
95+
96+
dfs(0, 0, 0);
97+
98+
function dfs(lastDirIndex, x, y) {
99+
// move -> clean -> add to doneSet [x:y]
100+
robot.clean();
101+
doneSet.add(`${x}:${y}`);
102+
// dfs 4 direction (if not visited)
103+
for (let i = 0; i < 4; i++) {
104+
let currDirIndex = (lastDirIndex + i) % 4;
105+
let xx = x + dirs[currDirIndex][0];
106+
let yy = y + dirs[currDirIndex][1];
107+
108+
// if the facing grid not cleaned yet
109+
// try to move to the facing grid now
110+
if (!doneSet.has(`${xx}:${yy}`) && robot.move()) {
111+
dfs(currDirIndex, xx, yy);
112+
}
113+
114+
robot.turnRight(); // turn right 4 time so we face the same direction as we get here
115+
}
116+
117+
// we have done 4 directions here, move back to where we come from
118+
robot.turnRight();
119+
robot.turnRight();
120+
robot.move();
121+
robot.turnRight();
122+
robot.turnRight();
123+
}
124+
};

0 commit comments

Comments
 (0)