Inglizcha: Given a zero-based permutationnums (0-indexed), build an array ans of the same length where !# ans[i] = nums[nums[i]] for each 0 <= i < nums.length and return it.
A zero-based permutationnums is an array of distinct integers from 0 to nums.length - 1 (inclusive).
O'zbekcha: Nolga asoslangan ya'ni index 0 dan boshlanuvchi nums permutatsiya berilgan. Shu bilan bir xil uzunlikda ans array yaratib 0 <= i < nums.length oraliqda ans[i] = nums[nums[i]] tenglab natijani qaytarish kerak ekan.
Misol 1
$ Input: nums=[0,2,1,5,3,4]$ Output: [0,1,2,4,5,3]$ Explanation: The array ans is built as follows:
ans = [nums[nums[0]], nums[nums[1]], nums[nums[2]], nums[nums[3]], nums[nums[4]], nums[nums[5]]] = [nums[0], nums[2], nums[1], nums[5], nums[3], nums[4]] = [0,1,2,4,5,3]
Misol 2
$ Input: nums=[5,0,1,2,3,4]$ Output: [4,5,0,1,2,3]$ Explanation: The array ans is built as follows:
ans = [nums[nums[0]], nums[nums[1]], nums[nums[2]], nums[nums[3]], nums[nums[4]], nums[nums[5]]] = [nums[5], nums[0], nums[1], nums[2], nums[3], nums[4]] = [4,5,0,1,2,3]
Agar yangi array yaratmay to'g'ridan to'g'ri nums[i] = nums[nums[i]] ga tenglab qo'ysak bizda nums[i] ni o'zida turgan qiymat yo'qolib qoladi shu sababli ans list yaratib olamiz. ans uzunligi(length) nums uzunligi bilan teng bo'lgani sababli yaratish davrida boshlang'ich 0 qiymat berib chiqamiz keyin sikl yaratib ushbu siklda ans[i] = nums[nums[i]] ga tenglab olamiz va ans ni natija sifatida qaytarib yuboramiz.
# Author: Abdulaminkhon Khaydarov# Date: 30/10/22 # Problem URL: https://leetcode.com/problems/build-array-from-permutation/fromtypingimportListclassSolution:defbuildArray(self,nums:List[int])->List[int]:# "nums" ni uzunligi bilan teng "ans" listini yaratib olamizans:List[int]=[0for_innums]# "0" dan "nums" uzunligigacha sikl yaratib "ans[i]" ni "nums[nums[i]]" ga tenglab chiqamiz.foriinrange(len(nums)):ans[i]=nums[nums[i]]returnansif__name__=='__main__':solution=Solution()# Example 1print(solution.buildArray([0,2,1,5,3,4])==[0,1,2,4,5,3])# Example 2print(solution.buildArray([5,0,1,2,3,4])==[4,5,0,1,2,3])
Birinchi yechimda biz qo'shimcha array dan foydalandik. Buni qo'shimcha array ishlatmay ishlash usuli mavjud biz bunda xotiradan constant joy ishlatamiz. nums ni uzunligini n ga tenglab olamiz keyin 0 dan n gacha sikl ochamiz bunda har bir sikl bosqichida biz nums[i] ni num ga tenglab turamiz. Agar biz nums[num] ni n ga bo'lib qoldiqni olsak bizda hozirgi nums[i] teng bo'ladigan qiymat kelib chiqadi va buni biz b o'zgaruvchisiga saqlab turamizda nums[i] da turgan qiymatga n * b + num shuni tenglab qo'yamiz. Bizga yana bitta sikl kerak bo'ladi, bu ham 0 dan n gacha bo'ladi. Biz nums[i] ni n ga bo'lib butun qismini olsak bizga avvalroq saqlab qo'ygan qiymatimizni qaytaradi va buni nums[i] ga tenglab qo'yamiz.
# Author: Abdulaminkhon Khaydarov# Date: 30/10/22 # Problem URL: https://leetcode.com/problems/build-array-from-permutation/fromtypingimportListclassSolution:defbuildArray(self,nums:List[int])->List[int]:# nums ni uzunligini n ga tenglab olamiz.n=len(nums)foriinrange(len(nums)):# "nums[i]" elementini vaqtincha "num" ga olib turamiznum=nums[i]# "nums[num]" elementini "n" ga bo'lib qoldiqni "b" ga tenglab turamiz.b=nums[num]%n# "nums[i]" ga "n * b + num" ni tenglab olamiz.nums[i]=n*b+numforiinrange(len(nums)):# "nums[i]" ni "nums[i]" ni "n" ga bo'lib butun qismiga tenglab olamiz.nums[i]=nums[i]//nreturnnumsif__name__=='__main__':solution=Solution()# Example 1print(solution.buildArray([0,2,1,5,3,4])==[0,1,2,4,5,3])# Example 2print(solution.buildArray([5,0,1,2,3,4])==[4,5,0,1,2,3])