algorithm

How to properly implement disjoint set data structure for finding spanning forests in Python?

落爺英雄遲暮 提交于 2021-02-05 08:34:51
问题 Recently, I was trying to implement the solutions of google kickstater's 2019 programming questions and tried to implement Round E's Cherries Mesh by following the analysis explanation. Here is the link to the question and the analysis. https://codingcompetitions.withgoogle.com/kickstart/round/0000000000050edb/0000000000170721 Here is the code I implemented: t = int(input()) for k in range(1,t+1): n, q = map(int,input().split()) se = list() for _ in range(q): a,b = map(int,input().split()) se

Reverse polish notation C# don't work correctly

狂风中的少年 提交于 2021-02-05 08:10:41
问题 I write an rpn, with a struktogram. Newest Problem: It is'nt work correctly now. If input string is "5 + ((1 + 2) * 4) - 3" My output is: 5 1 2 + 4 * 3 - + I have to got this result: 5 1 2 + 4 * + 3 - Edited the source * That was the original problem, but helped me, and now the original mistakes fixed: * , At the debug when the loop or int i = 12, the c value is 0\0 or something else and this value is added to output (name: formula) string as a '(' bracket. And I don't know why. And the last

Check if cyclic (modulo 16) number is larger than another?

跟風遠走 提交于 2021-02-05 07:40:39
问题 I have two a cyclic integer, modulo 16, so they assume values between 0 and 15. I need to compare two numbers to determine if n_1 is greater than n_0 n_1 > n_0 Obviously, this is not exactly defined, so I define n_1 to be greater than n_0 if it is less than 8 "numbers" ahead, otherwise, it is lesser than n_0 (if not equal). I.e. if: n_0 = 0 if n_1 is between 1 and 8 (both inclusive) then n_1 is greater than n_0. n_0 = 5 if n_1 is between 6 and 15 (both inclusive) then n_1 is greater than n_0.

Check if cyclic (modulo 16) number is larger than another?

一曲冷凌霜 提交于 2021-02-05 07:39:41
问题 I have two a cyclic integer, modulo 16, so they assume values between 0 and 15. I need to compare two numbers to determine if n_1 is greater than n_0 n_1 > n_0 Obviously, this is not exactly defined, so I define n_1 to be greater than n_0 if it is less than 8 "numbers" ahead, otherwise, it is lesser than n_0 (if not equal). I.e. if: n_0 = 0 if n_1 is between 1 and 8 (both inclusive) then n_1 is greater than n_0. n_0 = 5 if n_1 is between 6 and 15 (both inclusive) then n_1 is greater than n_0.

Functional/Stream programming for the graph problem “Reconstruct Itinerary”

[亡魂溺海] 提交于 2021-02-05 07:36:17
问题 I am trying to solve the reconstruct itinerary problem (https://leetcode.com/problems/reconstruct-itinerary/) in Scala using functional approach. Java solution works but Scala doesn't. One reason I found out was the hashmap is being updated and every iteration has the latest hashmap (even when popping from recursion) which is weird. Here is the solution in Java: import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map;

function that returns the last met of each numbers in a sorted array

我与影子孤独终老i 提交于 2021-02-05 06:52:30
问题 I wrote a function that returns the first met of each numbers from 0 to 9 array = [0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9] def lower(a, val, left, right): if left == right: return left mid = (left + right) // 2 if a[mid] < val: return lower(a, val, mid+1, right) else: return lower(a, val, left, mid) for i in range(10): print(lower_bound(array, i , 0, len(array)-1), end =' ') result: 0 2 4 6 8 10 12 14 16 18 . I tried to write a function that return the last met of each numbers from 0 to 9.

function that returns the last met of each numbers in a sorted array

浪子不回头ぞ 提交于 2021-02-05 06:51:36
问题 I wrote a function that returns the first met of each numbers from 0 to 9 array = [0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9] def lower(a, val, left, right): if left == right: return left mid = (left + right) // 2 if a[mid] < val: return lower(a, val, mid+1, right) else: return lower(a, val, left, mid) for i in range(10): print(lower_bound(array, i , 0, len(array)-1), end =' ') result: 0 2 4 6 8 10 12 14 16 18 . I tried to write a function that return the last met of each numbers from 0 to 9.

Compare elements of two arrays in less than O(n^2) time?

喜你入骨 提交于 2021-02-05 06:50:12
问题 I have two integer arrays. I need to find out two numbers, one from each array, whose sum is equal to 2. This is very simple in O(n^2) but is there a way to do it faster? 回答1: You can do it in O(N+M) time and O(N) space like this: Put elements of array a into a hash set Walk through array b , and check if hash table contains 2-b[i] Constructing a hash set of N elements takes O(N) time and O(N) space. Checking each of M elements against the hash set takes O(1), for a total of O(N+M) time. 来源:

Getting all possible combinations of a List of KeyValue Pairs in C#

我们两清 提交于 2021-02-05 06:39:45
问题 I have a key value pair like this: var accounts = new List<KeyValuePair<int,int>>(); and the contents of accounts looks like this: {[4,10000]} {[4,19000]} {[4,11000]} {[4,12000]} {[4,13036]} {[4,47100]} {[5,19300]} {[5,32900]} {[5,95800]} {[6,95800]} How can I get all possible combinations of the key value pairs in accounts such that I have: [{4,10000},{5,19300},{6,95800}], [{4,10000},{5,32900},{6,95800}]..... The data structure containing the final result is not of much importance to me, I'm

Algorithm to split people into groups with most diversity per group [closed]

ⅰ亾dé卋堺 提交于 2021-02-05 05:55:27
问题 Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 3 years ago . Improve this question I'd like an algorithm to put people into groups for an upcoming conference. There's lots of people going, from different regions, departments, genders etc, and they want to split people up as much as possible so get diversity in each group. So is there either a