Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions solutions/1901-2000/1920-build-array-from-permutation/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# 1920. Build Array from Permutation

[LeetCode problem 1920](https://leetcode.com/problems/build-array-from-permutation/)

- **Difficulty**: Easy
- **Tags**: array, simulation

## Description

Given a zero-indexed permutation `nums`, build an array `ans` where
`ans[i] == nums[nums[i]]` for every `i`.
18 changes: 18 additions & 0 deletions solutions/1901-2000/1920-build-array-from-permutation/solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "solution.hpp"

#include <iterator>

namespace leetcode::p1920 {

std::vector<int> solve(const std::vector<int>& nums) {
std::vector<int> result;
result.reserve(nums.size());

for (int i = 0; i < std::ssize(nums); ++i) {
result.push_back(nums.at(static_cast<std::size_t>(nums.at(static_cast<std::size_t>(i)))));
}

return result;
}

} // namespace leetcode::p1920
10 changes: 10 additions & 0 deletions solutions/1901-2000/1920-build-array-from-permutation/solution.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

#include <vector>

namespace leetcode::p1920 {

// optimized: O(n) time, O(n) space — direct lookup, nums[nums[i]].
std::vector<int> solve(const std::vector<int>& nums);

} // namespace leetcode::p1920
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <catch2/catch_test_macros.hpp>

#include "solution.hpp"

TEST_CASE("Problem 1920", "[p1920]") {
CHECK(leetcode::p1920::solve({0, 2, 1, 5, 3, 4}) == std::vector<int>{0, 1, 2, 4, 5, 3});
CHECK(leetcode::p1920::solve({5, 0, 1, 2, 3, 4}) == std::vector<int>{4, 5, 0, 1, 2, 3});
}
15 changes: 0 additions & 15 deletions swift/1930.swift

This file was deleted.

Loading