📜  以给定的移动和推送成本设置数字时钟计时器的最低成本

📅  最后修改于: 2022-05-13 01:56:05.516000             🧑  作者: Mango

以给定的移动和推送成本设置数字时钟计时器的最低成本

给定整数 A、B 和 N。任务是最小化在数字时钟中设置 N 秒的成本,其中时间以下列格式表示:

  • 最多99分99秒
  • 至少 1 秒
  • 前两位数字代表分钟,最后两分钟代表秒。
  • 如果按下少于 4 个数字来设置时间,它会在前面加上 0

A 是获得一个尚未按下来设置时间的新数字的成本,B 是按下任何数字来设置时间的成本。

例子:

方法:这个问题是基于实现的。找出可能的两种表示(可能只有一种可能的表示)以及这两种表示中的最小成本。请按照以下步骤操作:

  • 第一个观察结果应该是不需要按时钟自动添加的 0。
  • 因此,找到两个时钟时序: (x/60, x%60) 和 (x/60 -1, 60 + x%60) 不可能有其他组合。
  • 尝试仅在这两个时间之间找到最佳答案。

下面是上述方法的实现。

C++14
// C++ program to implement the above approach
#include 
using namespace std;
 
// Function to get the minimum time
int minCostTime(int moveCost, int pushCost,
                int targetSeconds)
{
    // Setting startAt to -1 as
    // nothing is pressed earlier
    // This variable shows last pressed button
    int startAt = -1;
 
    // Its the first timing which can be set
    int target_minute = targetSeconds / 60;
    int target_sec = targetSeconds % 60;
 
    // If time is greater than 99 min
    // and 60 sec then first time would come
    // somewhere in 100 min
    // which would actually be wrong.
    // as max min can be 99 only.
    if (target_minute > 99) {
        target_minute = target_minute - 1;
        target_sec = 60 + target_sec;
    }
 
    // Its the variables for
    // second timing possible
    int target_sec2 = 0;
    int target_minute2 = target_minute - 1;
 
    // Second timing is only possible
    // if first timing minute is
    // greater than zero and second
    // is less than 40 because
    // x mins and y seconds will be converted
    // to x-1 mins and 60 + y secs
    // and max seconds can be 99
    if (target_sec < 40
        && target_minute != 0) {
        target_minute2 = target_minute - 1;
        target_sec2 = 60 + target_sec;
    }
 
    // Convert 1st minute to optimal string
    string s = "";
 
    // Append  min only if min > 0
    // else, will be prepended automatically.
    if (target_minute > 0)
        s += to_string(target_minute);
 
    // If seconds is 1 digit number
    // then a 0 needs to be added before it
    if (target_sec < 10
        && target_minute != 0) {
        s += '0';
    }
    s += to_string(target_sec);
 
    // Calculate cost for 1st TIME.
    int temp = startAt;
    int ans = 0;
    for (int i = 0; i < s.length(); i++) {
        int x = s[i] - '0';
        if (x != temp) {
            ans += moveCost;
            ans += pushCost;
            temp = x;
        }
        else {
            ans += pushCost;
            temp = x;
        }
    }
    if (target_sec >= 40
        || target_minute == 0) {
        return ans;
    }
 
    // Find cost for 2nd TIME. */
    string s2 = "";
    if (target_minute2 != 0)
        s2 += to_string(target_minute2);
    if (target_sec2 < 10
        && target_minute2 != 0) {
        s2 += '0';
    }
    s2 += to_string(target_sec2);
    int temp2 = startAt;
    int ans2 = 0;
    for (int i = 0; i < s2.size(); i++) {
        int x = s2[i] - '0';
 
        if (x != temp2) {
            ans2 += moveCost;
            ans2 += pushCost;
            temp2 = x;
        }
        else {
            ans2 += pushCost;
            temp2 = x;
        }
    }
 
    // Returning the minimum of the two cost.
    return min(ans, ans2);
}
 
// Driver code
int main()
{
    int A = 1, B = 5, N = 300;
 
    // Function call
    int ans = minCostTime(A, B, N);
    cout << ans;
    return 0;
}


Java
import java.util.*;
class GFG
{
 
  // Function to get the minimum time
  public static int minCostTime(int moveCost,
                                int pushCost,
                                int targetSeconds)
  {
    // Setting startAt to -1 as
    // nothing is pressed earlier
    // This variable shows last pressed button
    int startAt = -1;
 
    // Its the first timing which can be set
    int target_minute = targetSeconds / 60;
    int target_sec = targetSeconds % 60;
 
    // If time is greater than 99 min
    // and 60 sec then first time would come
    // somewhere in 100 min
    // which would actually be wrong.
    // as max min can be 99 only.
    if (target_minute > 99) {
      target_minute = target_minute - 1;
      target_sec = 60 + target_sec;
    }
 
    // Its the variables for
    // second timing possible
    int target_sec2 = 0;
    int target_minute2 = target_minute - 1;
 
    // Second timing is only possible
    // if first timing minute is
    // greater than zero and second
    // is less than 40 because
    // x mins and y seconds will be converted
    // to x-1 mins and 60 + y secs
    // and max seconds can be 99
    if (target_sec < 40 && target_minute != 0) {
      target_minute2 = target_minute - 1;
      target_sec2 = 60 + target_sec;
    }
 
    // Convert 1st minute to optimal string
    String s = "";
 
    // Append min only if min > 0
    // else, will be prepended automatically.
    if (target_minute > 0)
      s += Integer.toString(target_minute);
 
    // If seconds is 1 digit number
    // then a 0 needs to be added before it
    if (target_sec < 10 && target_minute != 0) {
      s += '0';
    }
    s += Integer.toString(target_sec);
 
    // Calculate cost for 1st TIME.
    int temp = startAt;
    int ans = 0;
    for (int i = 0; i < s.length(); i++) {
      int x = s.charAt(i);
      if (x != temp) {
        ans += moveCost;
        ans += pushCost;
        temp = x;
      }
      else {
        ans += pushCost;
        temp = x;
      }
    }
    if (target_sec >= 40 || target_minute == 0) {
      return ans;
    }
    // Find cost for 2nd TIME. */
    String s2 = "";
    if (target_minute2 != 0)
      s2 += Integer.toString(target_minute2);
    if (target_sec2 < 10 && target_minute2 != 0) {
      s2 += '0';
    }
    s2 += Integer.toString(target_sec2);
    int temp2 = startAt;
    int ans2 = 0;
    for (int i = 0; i < s2.length(); i++) {
      int x = s2.charAt(i);
      if (x != temp2) {
        ans2 += moveCost;
        ans2 += pushCost;
        temp2 = x;
      }
      else {
        ans2 += pushCost;
        temp2 = x;
      }
    }
 
    // Returning the minimum of the two cost.
    return Math.min(ans, ans2);
  }
 
  // Driver code
  public static void main(String[] args)
  {
    int A = 1, B = 5, N = 300;
 
    // Function call
    int ans = minCostTime(A, B, N);
    System.out.print(ans);
  }
}
 
// This code is contributed by Taranpreet


Python3
# Python program to implement the above approach
 
# Function to get the minimum time
def minCostTime(moveCost, pushCost, targetSeconds):
 
    # Setting startAt to -1 as
    # nothing is pressed earlier
    # This variable shows last pressed button
    startAt = -1
 
    # Its the first timing which can be set
    target_minute = targetSeconds // 60
    target_sec = targetSeconds % 60
 
    # If time is greater than 99 min
    # and 60 sec then first time would come
    # somewhere in 100 min
    # which would actually be wrong.
    # as max min can be 99 only.
    if (target_minute > 99):
        target_minute = target_minute - 1
        target_sec = 60 + target_sec
 
    # Its the variables for
    # second timing possible
    target_sec2 = 0
    target_minute2 = target_minute - 1
 
    # Second timing is only possible
    # if first timing minute is
    # greater than zero and second
    # is less than 40 because
    # x mins and y seconds will be converted
    # to x-1 mins and 60 + y secs
    # and max seconds can be 99
    if (target_sec < 40 and target_minute != 0):
        target_minute2 = target_minute - 1
        target_sec2 = 60 + target_sec
 
    # Convert 1st minute to optimal string
    s = ""
 
    # Append  min only if min > 0
    # else, will be prepended automatically.
    if (target_minute > 0):
        s = s + str(target_minute)
 
    # If seconds is 1 digit number
    # then a 0 needs to be added before it
    if (target_sec < 10 and target_minute != 0):
        s = s + '0'
 
    s = s + str(target_sec)
 
    # Calculate cost for 1st TIME.
    temp = startAt
    ans = 0
    for i in range(0, len(s)):
        x = ord(s[i])
        if (x != temp):
            ans = ans + moveCost
            ans = ans + pushCost
            temp = x
 
        else:
            ans = ans + pushCost
            temp = x
 
    if (target_sec >= 40 or target_minute == 0):
        return ans
 
    # Find cost for 2nd TIME. */
    s2 = ""
    if (target_minute2 != 0):
        s2 = s2 + str(target_minute2)
    if (target_sec2 < 10 and target_minute2 != 0):
        s2 = s2 + '0'
 
    s2 = s2 + str(target_sec2)
    temp2 = startAt
    ans2 = 0
    for i in range(0, len(s2)):
        x = ord(s2[i])
 
        if (x != temp2):
            ans2 = ans2 + moveCost
            ans2 = ans2 + pushCost
            temp2 = x
 
        else:
            ans2 = ans2 + pushCost
            temp2 = x
 
    # Returning the minimum of the two cost.
    return min(ans, ans2)
 
# Driver code
A = 1
B = 5
N = 300
 
# Function call
ans = minCostTime(A, B, N)
print(ans)
 
# This code is contributed by Taranpreet


C#
using System;
class GFG
{
 
  // Function to get the minimum time
  static int minCostTime(int moveCost,
                         int pushCost,
                         int targetSeconds)
  {
 
    // Setting startAt to -1 as
    // nothing is pressed earlier
    // This variable shows last pressed button
    int startAt = -1;
 
    // Its the first timing which can be set
    int target_minute = targetSeconds / 60;
    int target_sec = targetSeconds % 60;
 
    // If time is greater than 99 min
    // and 60 sec then first time would come
    // somewhere in 100 min
    // which would actually be wrong.
    // as max min can be 99 only.
    if (target_minute > 99) {
      target_minute = target_minute - 1;
      target_sec = 60 + target_sec;
    }
 
    // Its the variables for
    // second timing possible
    int target_sec2 = 0;
    int target_minute2 = target_minute - 1;
 
    // Second timing is only possible
    // if first timing minute is
    // greater than zero and second
    // is less than 40 because
    // x mins and y seconds will be converted
    // to x-1 mins and 60 + y secs
    // and max seconds can be 99
    if (target_sec < 40 && target_minute != 0) {
      target_minute2 = target_minute - 1;
      target_sec2 = 60 + target_sec;
    }
 
    // Convert 1st minute to optimal string
    string s = "";
 
    // Append min only if min > 0
    // else, will be prepended automatically.
    if (target_minute > 0)
      s += target_minute.ToString();;
 
    // If seconds is 1 digit number
    // then a 0 needs to be added before it
    if (target_sec < 10 && target_minute != 0) {
      s += '0';
    }
    s += target_sec.ToString();;
 
    // Calculate cost for 1st TIME.
    int temp = startAt;
    int ans = 0;
    for (int i = 0; i < s.Length; i++) {
      int x = s[i];
      if (x != temp) {
        ans += moveCost;
        ans += pushCost;
        temp = x;
      }
      else {
        ans += pushCost;
        temp = x;
      }
    }
    if (target_sec >= 40 || target_minute == 0) {
      return ans;
    }
 
    // Find cost for 2nd TIME. */
    string s2 = "";
    if (target_minute2 != 0)
      s2 += target_minute2.ToString();;
    if (target_sec2 < 10 && target_minute2 != 0) {
      s2 += '0';
    }
    s2 += target_sec2.ToString();;
    int temp2 = startAt;
    int ans2 = 0;
    for (int i = 0; i < s2.Length; i++) {
      int x = s2[i];
      if (x != temp2) {
        ans2 += moveCost;
        ans2 += pushCost;
        temp2 = x;
      }
      else {
        ans2 += pushCost;
        temp2 = x;
      }
    }
 
    // Returning the minimum of the two cost.
    return Math.Min(ans, ans2);
  }
 
  // Driver code
  public static void Main()
  {
    int A = 1, B = 5, N = 300;
 
    // Function call
    int ans = minCostTime(A, B, N);
    Console.Write(ans);
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
17

时间复杂度: O(1)
辅助空间: O(1)。