Why is my iterative solution slower than a recursive solution?

I am trying to obtain a solution for this question https://www.spoj.com/problems/COINS/.

But oddly enough, my iterative solution:

#include <iostream> using namespace std;  int main() {     int n;     while(cin >> n){         long long int dp[n+2];         dp[0]=0;         for(long long int i=1; i<=n; i++)             dp[i]=max(dp[i/2]+dp[i/3]+dp[i/4], i);         cout << dp[n] <<endl;     }     return 0; } 

gets a TLE, while the recursive solution for this(Not mine) gets accepted in no time:

#include <cstdio> #include <map> #include <algorithm>  using namespace std;  map<int, long long> dp;  long long f(int n){     if(n==0) return 0;      if(dp[n]!=0) return dp[n];      long long aux=f(n/2)+f(n/3)+f(n/4);      if(aux>n) dp[n]=aux;     else dp[n]=n;      return dp[n]; }  int main(){         int n;      while(scanf("%d",&n)==1) printf("%lld\n",f(n));      return 0; } 

Shouldn’t it be the opposite? I am genuinely confused. Please tell me how I can improve the first code.

Rapid requests to REST or alternate solution

React Application on top of Flask

Currently I am developing a REST API where users will submit tasks, these tasks will run for an indeterminate amount of time and it is quite literally impossible for me to detect when they are done.

When the users submit a task I am creating a simple entry in a redis cache that contains the start time, job uuid, data and current state. When a job comes in a bunch of queue workers on a separate system go to work and as I mentioned previously can take quite a while to complete or can take no time at all (1 second vs 1 day).

My current implementation works just fine but I am trying to see if there is an alternate solution to optimize things to reduce the amount of calls I need to make to the back from the front.

So essentially the flow is:

  1. User submits job
  2. Cache entry created, user immediately acked with job uuid
  3. Workers start task 3A. Workers update cache entry on completion
  4. User asks endpoint if job is completed (check cache STATE var)
  5. Return user data. 5A. If not complete keep asking every X amount of time (after completion of previous req) 5B. If complete stop asking endpoint if completed

This setup works just fine but it seems very very wasteful to send several hundred requests every few minutes to check state.

Any input would be appreciated. If this is not the proper place to post please let me know and I can remove/move.

Error when compiling after importing into my solution the sp-page-context npm package

I am getting this error when I import the sp-page-context into my angular solution: ERROR in ./node_modules/@microsoft/sp-core-library/lib/SPCoreLibraryStrings.resx.js Module not found: Error: Can’t resolve ‘resx-strings’ in ‘XXXXXXXXXXXXX\node_modules@microsoft\sp-core-library\lib’ ERROR in ./node_modules/@microsoft/sp-dynamic-data/lib/loc/Strings.resx.js Module not found: Error: Can’t resolve ‘resx-strings’ in ‘XXXXXXXXXXXXX\node_modules@microsoft\sp-dynamic-data\lib\loc’ ERROR in ./node_modules/@microsoft/sp-page-context/lib/loc/Strings.resx.js Module not found: Error: Can’t resolve ‘resx-strings’ in ‘XXXXXXXXXXXXX\node_modules@microsoft\sp-page-context\lib\loc’

Designing solution to critical section problem with two semaphores vs single semaphore

I came across following problem:

Let two processes P1 and P2 are there:

+-----------------------+-----------------------+ | P1                    | P2                    | | while(1)              | while(1)              | | {                     | {                     | |    //s1               |    //s1               | |    //s2               |    //s2               | |    //critical section |    //critical section | |    signal(mutex1);    |    signal(mutex1);    | |    signal(mutex2);    |    signal(mutex2);    | | }                     | }                     | +-----------------------+-----------------------+ 

Lets mutex1 and mutex2 both are initialized to 1. So in order to avoid deadlock s1, s2, s3 and s4 (respectively) will be replaced by
(A) wait(mutex2), wait(mutex1), wait(mutex1), wait(mutex2)
(B) wait(mutex1), wait(mutex2), wait(mutex2), wait(mutex1)
(C) wait(mutex1), wait(mutex2), wait(mutex2), wait(mutex2)
(D) wait(mutex1), wait(mutex2), wait(mutex1), wait(mutex2)

Doubt

Given solution is D, which I understood why. But I am guessing whether having two semaphores mutex1 and mutex2 serve any special purpose in above code? Does below single semaphore based programs achieve same as what is achieved by above code?

+-----------------------+-----------------------+ | P1                    | P2                    | | while(1)              | while(1)              | | {                     | {                     | |    wait(mutex1);      |    wait(mutex1);      | |    //critical section |    //critical section | |    signal(mutex1);    |    signal(mutex1);    | | }                     | }                     | +-----------------------+-----------------------+ 

[GET][NULLED] – JNews – One Stop Solution for Web Publishing v5.0.3

[​IMG]
[​IMG]

[GET][NULLED] – JNews – One Stop Solution for Web Publishing v5.0.3

ecommerce solution help

Hi

I am looking for some help for the future of our web shop. I am sorry for the length of this, but it is only to try and explain the situation the best way possible. Any help at all will be much appreciated.

Before I started working for this company they had a WordPress with a premium theme + WooCommerce + Mamut – Visma as their complete ecommerce solution. An external company did the entire job some years ago and is responsible for maintaining and develop the solution. Any changes big…

ecommerce solution help

What’s a workable solution to disabling a button based on a server response?

I’m working with React-Native, Express, Node.js, and socket.io.

I have a game room where two users send data across a socket connection to the server which then updates both client’s front end.

there is client-side validation that occurs after a button press (lets call it a power button) before data is sent. my issue is if the move ends up being invalid, the button is disabled because I programmed it to disable after press, however it actually only want it to disabled after data has been validated.

initially I thought validation occurred server side, then changed to client side to help with this issue, but now i’m stuck mentally.

here is the button component for a visual:

import React from "react"; import { StyleSheet, Text, View, TouchableOpacity } from "react-native";  function PowerMoveBtn(props) {   const disabled = this.props.disabled;    function onSelect(type) {     props.onSelect(type);   }    return (     <View style={styles.btn}>       <TouchableOpacity         disabled={disabled}         onPress={disabled ? null : () => onSelect(props.type)}       >         <Text>{props.type}</Text>       </TouchableOpacity>     </View>   ); }  const styles = StyleSheet.create({   btn: {     borderWidth: 1   } });  export default PowerMoveBtn; 

Any ideas? Is server-side or client-side validation a better working solution?

K Closest Points to Origin C# solution deemed too slow by Leetcode.com

LeetCode challenge

I know I could simply use the built in .Sort or .Order by to solve this question in 1 line. However, for interviews I would need to show the whole workings of my solution.

I am using a min-heap to calculate and store the distances from the origin. Then I simply return the K number of distances.

My code fails due to the “Time limit exceeded” error, indicating my code is too slow. However, it does return the correct answer.

Is there a more efficient way of using a min-heap? Or should my code be using a completely different algorithm and data structure?

public class Solution {     public int[][] KClosest(int[][] points, int K)     {         var lists = new int[K][];          Heap head = new Heap();          for (int i = 0; i < points.Length; i++)         {             int a = points[i][0];             int b = points[i][1];             var testMin = a * a + b * b;             var newNode = new Node             {                 val = testMin,                 index = points[i]             };             head.Add(newNode);         }          for (int i = 0; i < K; i++)         {             lists[i] = head.Pop().index;         }          return lists;     } }  class Heap {     public Node head;     public Node tail;      public void Add(Node newNode)     {         if (head == null)         {             head = newNode;             tail = newNode;         }         else         {             newNode.parent = tail;             tail.next = newNode;             tail = tail.next;         }         HeapifyUp();     }      void HeapifyUp()     {         Node curr = tail;         while (curr.parent != null)         {             if (curr.val < curr.parent.val)             {                 var tVal = curr.val;                 var tIndex = curr.index;                  curr.val = curr.parent.val;                 curr.index = curr.parent.index;                  curr.parent.val = tVal;                 curr.parent.index = tIndex;             }             curr = curr.parent;         }     }      public Node Pop()     {         var tempHead = new Node         {             index = head.index,             val = head.val         };          // Set head value to tail value         head.index = tail.index;         head.val = tail.val;          // Deference tail         if (tail.parent != null)         {             tail.parent.next = null;             tail = tail.parent;         }         else         {             tail = null;             head = null;         }          HeapifyDown();          return tempHead;     }      void HeapifyDown()     {         Node curr = head;         while (curr != null && curr.next != null)         {             if (curr.val > curr.next.val)             {                 var tVal = curr.val;                 var tIndex = curr.index;                  curr.val = curr.next.val;                 curr.index = curr.next.index;                  curr.next.val = tVal;                 curr.next.index = tIndex;             }             curr = curr.next;         }     } }  class Node {     public int[] index;     public int val;     public Node next;     public Node parent; }