Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 3973adc

Browse files
committed
Add solution #1734
1 parent c7e787d commit 3973adc

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,507 LeetCode solutions in JavaScript
1+
# 1,508 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1335,6 +1335,7 @@
13351335
1728|[Cat and Mouse II](./solutions/1728-cat-and-mouse-ii.js)|Hard|
13361336
1732|[Find the Highest Altitude](./solutions/1732-find-the-highest-altitude.js)|Easy|
13371337
1733|[Minimum Number of People to Teach](./solutions/1733-minimum-number-of-people-to-teach.js)|Medium|
1338+
1734|[Decode XORed Permutation](./solutions/1734-decode-xored-permutation.js)|Medium|
13381339
1748|[Sum of Unique Elements](./solutions/1748-sum-of-unique-elements.js)|Easy|
13391340
1749|[Maximum Absolute Sum of Any Subarray](./solutions/1749-maximum-absolute-sum-of-any-subarray.js)|Medium|
13401341
1752|[Check if Array Is Sorted and Rotated](./solutions/1752-check-if-array-is-sorted-and-rotated.js)|Easy|
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* 1734. Decode XORed Permutation
3+
* https://leetcode.com/problems/decode-xored-permutation/
4+
* Difficulty: Medium
5+
*
6+
* There is an integer array perm that is a permutation of the first n positive integers,
7+
* where n is always odd.
8+
*
9+
* It was encoded into another integer array encoded of length n - 1, such that
10+
* encoded[i] = perm[i] XOR perm[i + 1]. For example, if perm = [1,3,2], then encoded = [2,1].
11+
*
12+
* Given the encoded array, return the original array perm. It is guaranteed that the answer
13+
* exists and is unique.
14+
*/
15+
16+
/**
17+
* @param {number[]} encoded
18+
* @return {number[]}
19+
*/
20+
var decode = function(encoded) {
21+
const n = encoded.length + 1;
22+
let totalXor = 0;
23+
for (let i = 1; i <= n; i++) {
24+
totalXor ^= i;
25+
}
26+
27+
let oddXor = 0;
28+
for (let i = 1; i < encoded.length; i += 2) {
29+
oddXor ^= encoded[i];
30+
}
31+
32+
const result = new Array(n);
33+
result[0] = totalXor ^ oddXor;
34+
35+
for (let i = 0; i < n - 1; i++) {
36+
result[i + 1] = result[i] ^ encoded[i];
37+
}
38+
39+
return result;
40+
};

0 commit comments

Comments
 (0)