We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Algorithms
- Greedy
- Largest Permutation
- Discussions
Largest Permutation
Largest Permutation
+ 0 comments # ThinhNguyen97 """ I Miss Those Moments I Spent With You Kitchen Table Mirror Sofa """ # Python is scripting language use interpreter # import numpy as np # import matplotlib.pyplot as plt # import pandas as pd from math import * from builtins import staticmethod from collections import Counter from collections import defaultdict from collections import namedtuple from collections import deque from queue import LifoQueue import heapq import functools import hashlib from datetime import datetime, timedelta import json import re from itertools import * import queue from bisect import bisect_left def solve(N, k, arr): position = [0] * (N + 1) for i in range(N): position[arr[i]] = i I = 0 while k > 0 and I < N: if arr[I] != N - I: temp = arr[I] arr[I], arr[position[N - I]] = arr[position[N - I]], arr[I] position[temp] = position[N - I] position[N - I] = I k -= 1 I += 1 return arr def main(): n, k = map(int, input().rstrip().split()) a = list(map(int, input().rstrip().split())) print(*solve(n, k, a)) if __name__ == '__main__': main()
+ 0 comments Java8
public static List<Integer> largestPermutation(int k, List<Integer> arr) { int n = arr.size(); int[] position = new int[n + 1]; for (int i = 0; i < n; i++) { position[arr.get(i)] = i; } int a = 0; while (k > 0 && a < n) { if (arr.get(a) != n - a) { int temp = arr.get(a); int swapIndex = position[n - a]; arr.set(a, n - a); arr.set(swapIndex, temp); position[temp] = swapIndex; position[n - a] = a; k--; } a++; } return arr; }
+ 0 comments this is my java solution
public static List<Integer> largestPermutation(int k, List<Integer> unSorted) { List<Integer> sorted = new ArrayList<>(unSorted); int n = unSorted.size(); int count = 0 ; Map<Integer, Integer> indexMap = new HashMap<>(); for (int i = 0; i < n; i++) { indexMap.put(unSorted.get(i), i); } sorted.sort(Collections.reverseOrder()); System.out.println(sorted); for(int i = 0; i < n ; i++) { if(!Objects.equals(unSorted.get(i), sorted.get(i))) { if(count < k) { count++; int newValue = unSorted.get(i) ; int newIndex = indexMap.get(sorted.get(i)) ; indexMap.put(newValue , newIndex) ; Collections.swap(unSorted , i , newIndex); } else { break; } } } return unSorted; }
+ 0 comments vector<int> largestPermutation(int k, vector<int> arr) { int N = arr.size(); vector<int> position(N+1); for (int i=0; i < N; i++) position[arr[i]] = i; int I = 0; while (k > 0 and I < N) { if (arr[I] != N-I) { int temp = arr[I]; swap(arr[I], arr[position[N-I]]); position[temp] = position[N-I]; position[N-I] = I; k--; } I++; } return arr; }
+ 0 comments O(n)
vector<int> largestPermutation(int k, vector<int> arr) { int N = arr.size(); vector<int> position(N+1); for (int i=0; i < N; i++) position[arr[i]] = i; int I = 0; while (k > 0 and I < N) { if (arr[I] != N-I) { int temp = arr[I]; swap(arr[I], arr[position[N-I]]); position[temp] = position[N-I]; position[N-I] = I; k--; } I++; } return arr; }
Load more conversations
Sort 374 Discussions, By:
Please Login in order to post a comment