algorithm

Generate all unique combinations of Items

六月ゝ 毕业季﹏ 提交于 2021-02-07 08:19:48
问题 I trying to generate all possible unique combination of items. Ex: item1, item2, item3 Combinations: item1+item2+item3 item1+item2 item1+item3 item2+item3 item1 item2 item3 I am unable to get an idea on how to solve this? for(int i=0;i<size;i++){ for(int j=i+1;j<size;j++){ System.out.println(list.item(i)+list.item(j)); } } The above code certainly works for all unique combination of two elements. But not for 3 element pair and more.. 回答1: If you have N items, count from 1 to 2^N-1. Each

How does Facebook do it?

落爺英雄遲暮 提交于 2021-02-07 08:13:26
问题 Have you ever noticed how facebook says “3 friends and 33 others liked this”? I was wondering what the best approach to do this is. I don’t think going through the friends list, and the list of users who “liked this” and comparing them is efficient at all! Do they keep a track of this in the database? That will make the database size very huge. What do you guys think? Thanks! 回答1: I would guess they outer join their friends table with their likes table to count both regular likes and friend

How to create dict from class without None fields?

非 Y 不嫁゛ 提交于 2021-02-07 07:17:34
问题 I have the following dataclass: @dataclass class Image: content_type: str data: bytes = b'' id: str = "" upload_date: datetime = None size: int = 0 def to_dict(self) -> Dict[str, Any]: result = {} if self.id: result['id'] = self.id if self.content_type: result['content_type'] = self.content_type if self.size: result['size'] = self.size if self.upload_date: result['upload_date'] = self.upload_date.isoformat() return result Is there any way to simplify to_dict method? I don't want to list all

How to find the farthest point (from a set of points) from a given point efficiently?

橙三吉。 提交于 2021-02-07 06:00:22
问题 I'm looking for an algorithm or data structure to solve the following problem: You are given a set of points S. And you are given Q queries in form of another point. For every query, find the farthest point in the set from the given point. There are at most 10^5 points in the set and 10^5 queries. All the coordinates for points are in range from 0 to 10^5. I am wondering if there is a way to store the set of points such that we can answer the queries in O(log n) or O(log^2 n) if necessary.

Get the closest value for combinations of an array (JS)

谁说胖子不能爱 提交于 2021-02-07 05:28:05
问题 I'm looking for an algorithm that I can use for combining values in array, to get as close as possible to "another value". For instance, the number I want to find out what combination that gives the closes result to is 2.5. And my array is [0.5, 1.0, 1.5, 2.0, 3.0] . The combination in this case would be 2.0+0.5 . 2.7 would yield the same combo (2.5 is the closest), while 3.7 would yield 3.0+0.5 and 7.0 would be 3.0+3.0+1.0 . I've been reading up on different algorithms to create available

Why is the complexity of BFS O(V+E) instead of O(V*E)?

半城伤御伤魂 提交于 2021-02-07 04:57:16
问题 Some pseudocode here (disregard my style) Starting from v1(enqueued): function BFS(queue Q) v2 = dequeue Q enqueue all unvisited connected nodes of v2 into Q BFS(Q) end // maybe minor problems here Since there are V vertices in the graph, and these V vertices are connected to E edges, and visiting getting connected nodes (equivalent to visiting connected edges) is in the inner loop (the outer loop is the recursion itself), it seems to me that the complexity should be O(V*E) rather than O(V+E)

How to delete items from a std::vector given a list of indices

試著忘記壹切 提交于 2021-02-07 04:57:13
问题 I have a vector of items items , and a vector of indices that should be deleted from items : std::vector<T> items; std::vector<size_t> indicesToDelete; items.push_back(a); items.push_back(b); items.push_back(c); items.push_back(d); items.push_back(e); indicesToDelete.push_back(3); indicesToDelete.push_back(0); indicesToDelete.push_back(1); // given these 2 data structures, I want to remove items so it contains // only c and e (deleting indices 3, 0, and 1) // ??? What's the best way to

Coin change with limited coins complexity

落花浮王杯 提交于 2021-02-07 04:35:23
问题 If there is an unlimited number of every coin then the complexity is O(n*m) where is n is the total change and m is the number of coin types. Now when the coins for every type are limited then we have to take into account the remaining coins. I managed to make it work with a complexity of O(n*m 2 ) using another for of size n so I can track the remaining coins for each type. Is there a way-trick to make the complexity better? EDIT : The problem is to compute the least ammount of coins

Coin change with limited coins complexity

丶灬走出姿态 提交于 2021-02-07 04:35:15
问题 If there is an unlimited number of every coin then the complexity is O(n*m) where is n is the total change and m is the number of coin types. Now when the coins for every type are limited then we have to take into account the remaining coins. I managed to make it work with a complexity of O(n*m 2 ) using another for of size n so I can track the remaining coins for each type. Is there a way-trick to make the complexity better? EDIT : The problem is to compute the least ammount of coins

Difference in Space Complexity of different sorting algorithms

大憨熊 提交于 2021-02-07 04:16:41
问题 I am trying to understand Space Complexities of different sorting algorithms. http://bigocheatsheet.com/?goback=.gde_98713_member_241501229 from the above link I found that the complexity of bubble sort,insertion and selection sort is O(1) where as quick sort is O(log(n)) and merge sort is O(n). we were actually not allocating extra memory in any of the algorithms. Then why the space complexities are different when we are using the same array to sort them? 回答1: When you run code, memory is