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
- Search
- Minimum Loss
- Discussions
Minimum Loss
Minimum Loss
Sort by
recency
|
400 Discussions
|
Please Login in order to post a comment
Java-8 solution
public static int minimumLoss(List prices) { int n = prices.size(); Map indexMap = new HashMap<>(); for (int i = 0; i < n; i++) { indexMap.put(prices.get(i), i); }
}
My Python 3 code with a customized binary search tree. Passed all the tess. I know in the worst case, this is O(n^2). But in most cases, it is like O(n log n), if the tree is somewhat balanced.
class Node: def init(self, key): self.left = None self.right = None self.val = key
def minimumLoss(price): # Write your code here
!/bin/python3
import math import os import random import re import sys
def minimumLoss(price): n = len(price) # Create a dictionary of price to year index price_index = {p: i for i, p in enumerate(price)}
def minimumLoss(price): indexed_prices = [(p, i) for i, p in enumerate(price)] indexed_prices.sort(key=lambda x: x[0]) # sort by price
if name == "main": n = int(input()) price = list(map(int, input().split())) print(minimumLoss(price))