Questions tagged [algorithm]
An algorithm is a sequence of well-defined steps that define an abstract solution to a problem. Use this tag when your issue is related to algorithm design.
4,287
questions
1
vote
2answers
39 views
Leetcode: Cousins in Binary Tree
Mu solution to Leetcode problem Cousins in Binary Tree works, but the code feels bulky. Is there a better way to solve this problem, particularly to use less additional variables? I will appreciate ...
3
votes
1answer
69 views
friendship program using graph type python/algorithm
I have created a program but I feel the def common(friendships, person1, person2) function can be more efficient, if anyone has any good idea how I can improve it I ...
-1
votes
0answers
36 views
Maximum of expression in given interval [closed]
Given a special function
F(X,Y,Z) = (X ∧ Z)⋅(Y ∧ Z) where ∧ is bitwise AND operator; X,Y,Z are non-negative integers; and '.' represents product
We want to maximize the function F(X,Y,Z) for given X ...
-4
votes
1answer
19 views
Knapsack Problem Help in Algorithm [closed]
I am learning the 0/1 knapsack problem and I found a doubt here. In 0-1 knapsack problem after the first figure why we can't choose Item 4 two times and Item 2 one times, This would result in the sum ...
-3
votes
0answers
17 views
Greatest common divisor with Euclidean's algorithm [closed]
I read the wiki page and implemented the Euclidean algo to find the greatest common divisor(GCD) of two numbers. The first part that has to be done is determine which number is larger. I tried ...
3
votes
1answer
49 views
Python Binary Search
My take on a binary search algorithm without research or knowledge of an efficient one. It would be appreciated if one could show me an efficient way to do it using recursion or a more pythonic ...
3
votes
2answers
52 views
Print input one word per line (K&R 1-12 exercise) attempt
I'm currently trying to learn C from the K&R book, reading through and attempting the exercises. I came across exercise 1-12 yesterday and was a little stumped but managed to complete it. I was ...
2
votes
1answer
107 views
How to refactor/fix algorithim from mutating a reference to using inline variable
The function takes an input path like a.b.c and should output a nested structure json like:
...
1
vote
1answer
61 views
Bubble Sort Implementation | C-ish and Modern C++
For practice purpose, I wrote bubble sort in C++ two styles: C-ish and modern C++ compliant. I would like to have your comments on any point on these implementations.
...
1
vote
1answer
46 views
Data mining in Java: finding undrawn lottery rows - follow-up
(See the previous (initial) iteration.)
This time, I have substantially reduced the usage of the final and this keywords. Also, ...
2
votes
1answer
30 views
What is the optimal way to retrieve a list of sorted dates in a list of sorted periods in python?
Suppose I have a list with N sorted dates and M non-overlapping sorted periods with start date, end date (inclusive), and a tax rate for example. I have to make an efficient algorithm retrieve all tax ...
4
votes
1answer
94 views
Sardinas-Patterson Algorithm
Introduction
Basic Idea
The Sardinas-Patterson algorithm determines (in polynomial time), whether a code is uniquely decodeable or not.
Example
Code 1 is not uniquely decodeable, because "100" ...
3
votes
1answer
44 views
Staff Scheduler: Design/Algorithm questions (Python)
Background
I worked at a community center where everyday one of my coworkers would spend 20ish minutes trying to organize the day's schedule. The facility had three rooms: The Great Hall, The Club ...
0
votes
0answers
22 views
mergeSort implementation in Ruby
I'm currently going over Robert Sedgewick Algorithms book and I'm trying to recreate his Java implementations in Ruby. I would like to know If I'm following best practices. I try to use clean code ...
2
votes
1answer
77 views
Given 2 numbers a and b find the smallest number greater than b by interchanging the digits of a and if not possible print -1
I have made the solution for this question it is based on backtracking. Can anyone suggest me how to make it work faster, I haven't practiced backtracking that much, although my solution seems to work....