📜  找到菱形图案中至少有 K 颗星的行

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

找到菱形图案中至少有 K 颗星的行

给定两个整数NK ,其中 N 表示具有 ( 2 * N) -1行的菱形图案,任务是找到在菱形图案中至少有 K个星的第一行的索引

请注意,K 的值总会有一个确定的答案。

例子

朴素方法:给定问题可以通过简单地迭代每一行并保持每行中星数的计数来解决。打印星数超过 K 的前两个。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the index of
// required row
void get(int N, int K)
{
    // Stores the count of stars
    // till ith row
    int sum = 0, ans;
 
    // Iterating over the rows
    for (int i = 1; i <= 2 * N - 1; i++) {
 
        // Upper half
        if (i <= N) {
            sum += i;
        }
 
        // Lower half
        else {
            sum += 2 * N - i;
        }
 
        // Atleast K stars are found
        if (sum >= K) {
            ans = i;
            break;
        }
    }
 
    // Print Answer
    cout << ans << endl;
}
 
// Driver Code
int main()
{
    int N = 3, K = 8;
    get(N, K);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG
{
 
  // Function to find the index of
  // required row
  static void get(int N, int K)
  {
 
    // Stores the count of stars
    // till ith row
    int sum = 0, ans = 0;
 
    // Iterating over the rows
    for (int i = 1; i <= 2 * N - 1; i++) {
 
      // Upper half
      if (i <= N) {
        sum += i;
      }
 
      // Lower half
      else {
        sum += 2 * N - i;
      }
 
      // Atleast K stars are found
      if (sum >= K) {
        ans = i;
        break;
      }
    }
 
    // Print Answer
    System.out.print(ans +"\n");
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int N = 3, K = 8;
    get(N, K);
  }
}
 
// This code is contributed by 29AjayKumar


Python3
# Python program for the above approach
 
# Function to find the index of
# required row
def get(N, K):
   
    # Stores the count of stars
    # till ith row
    sum = 0;
    ans = 0;
 
    # Iterating over the rows
    for i in range(1,2*N):
 
        # Upper half
        if (i <= N):
            sum += i;
 
        # Lower half
        else:
            sum += 2 * N - i;
 
        # Atleast K stars are found
        if (sum >= K):
            ans = i;
            break;
 
    # Print Answer
    print(ans);
 
# Driver Code
if __name__ == '__main__':
    N = 3;
    K = 8;
    get(N, K);
 
# This code is contributed by 29AjayKumar


C#
// C# program for the above approach
using System;
 
public class GFG
{
 
  // Function to find the index of
  // required row
  static void get(int N, int K)
  {
 
    // Stores the count of stars
    // till ith row
    int sum = 0, ans = 0;
 
    // Iterating over the rows
    for (int i = 1; i <= 2 * N - 1; i++) {
 
      // Upper half
      if (i <= N) {
        sum += i;
      }
 
      // Lower half
      else {
        sum += 2 * N - i;
      }
 
      // Atleast K stars are found
      if (sum >= K) {
        ans = i;
        break;
      }
    }
 
    // Print Answer
    Console.Write(ans +"\n");
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
    int N = 3, K = 8;
    get(N, K);
  }
}
 
// This code is contributed by 29AjayKumar


Javascript


C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to count the number of rows
int count_rows(int n, int k)
{
   
    // A diamond pattern contains
    // 2*n-1 rows so end = 2*n-1
    int start = 1, end = 2 * n - 1, ans = 0;
 
    // Loop for the binary search
    while (start <= end) {
        int mid = start - (start - end) / 2;
 
        int stars_till_mid = 0;
 
        if (mid > n) {
 
            // l_stars is no of rows in
            // lower triangle of diamond
            int l_stars = 2 * n - 1 - mid;
            int till_half = (n * (n + 1)) / 2;
 
            stars_till_mid
                = till_half + ((n - 1) * (n)) / 2
                  - ((l_stars) * (l_stars + 1)) / 2;
        }
        else {
 
            // No of stars till mid th row
            stars_till_mid = mid * (mid + 1) / 2;
        }
 
        // Check if k > starts_till_mid
        if (k <= stars_till_mid) {
            ans = mid;
            end = mid - 1;
        }
        else
            start = mid + 1;
    }
 
    // Return Answer
    return ans;
}
 
// Driver function
int main()
{
    int N = 3, K = 8;
 
    // Call the function
    // and print the answer
    cout << count_rows(N, K);
}


Java
// Java program for the above approach
import java.util.*;
class GFG{
 
  // Function to count the number of rows
  static int count_rows(int n, int k)
  {
 
    // A diamond pattern contains
    // 2*n-1 rows so end = 2*n-1
    int start = 1, end = 2 * n - 1, ans = 0;
 
    // Loop for the binary search
    while (start <= end) {
      int mid = start - (start - end) / 2;
 
      int stars_till_mid = 0;
 
      if (mid > n) {
 
        // l_stars is no of rows in
        // lower triangle of diamond
        int l_stars = 2 * n - 1 - mid;
        int till_half = (n * (n + 1)) / 2;
 
        stars_till_mid
          = till_half + ((n - 1) * (n)) / 2
          - ((l_stars) * (l_stars + 1)) / 2;
      }
      else {
 
        // No of stars till mid th row
        stars_till_mid = mid * (mid + 1) / 2;
      }
 
      // Check if k > starts_till_mid
      if (k <= stars_till_mid) {
        ans = mid;
        end = mid - 1;
      }
      else
        start = mid + 1;
    }
 
    // Return Answer
    return ans;
  }
 
  // Driver function
  public static void main(String[] args)
  {
    int N = 3, K = 8;
 
    // Call the function
    // and print the answer
    System.out.print(count_rows(N, K));
  }
}
 
// This code is contributed by 29AjayKumar


Python3
# python3 program for the above approach
 
# Function to count the number of rows
def count_rows(n, k):
 
        # A diamond pattern contains
        # 2*n-1 rows so end = 2*n-1
    start = 1
    end = 2 * n - 1
    ans = 0
 
    # Loop for the binary search
    while (start <= end):
        mid = start - (start - end) // 2
 
        stars_till_mid = 0
 
        if (mid > n):
 
            # l_stars is no of rows in
            # lower triangle of diamond
            l_stars = 2 * n - 1 - mid
            till_half = (n * (n + 1)) / 2
 
            stars_till_mid = till_half + \
                ((n - 1) * (n)) / 2 - ((l_stars) * (l_stars + 1)) / 2
 
        else:
 
            # No of stars till mid th row
            stars_till_mid = mid * (mid + 1) / 2
 
            # Check if k > starts_till_mid
        if (k <= stars_till_mid):
            ans = mid
            end = mid - 1
        else:
            start = mid + 1
 
        # Return Answer
    return ans
 
# Driver function
if __name__ == "__main__":
 
    N = 3
    K = 8
 
    # Call the function
    # and print the answer
    print(count_rows(N, K))
 
    # This code is contributed by rakeshsahni


C#
// C# program for the above approach
using System;
class GFG{
 
  // Function to count the number of rows
  static int count_rows(int n, int k)
  {
 
    // A diamond pattern contains
    // 2*n-1 rows so end = 2*n-1
    int start = 1, end = 2 * n - 1, ans = 0;
 
    // Loop for the binary search
    while (start <= end) {
      int mid = start - (start - end) / 2;
 
      int stars_till_mid = 0;
 
      if (mid > n) {
 
        // l_stars is no of rows in
        // lower triangle of diamond
        int l_stars = 2 * n - 1 - mid;
        int till_half = (n * (n + 1)) / 2;
 
        stars_till_mid
          = till_half + ((n - 1) * (n)) / 2
          - ((l_stars) * (l_stars + 1)) / 2;
      }
      else {
 
        // No of stars till mid th row
        stars_till_mid = mid * (mid + 1) / 2;
      }
 
      // Check if k > starts_till_mid
      if (k <= stars_till_mid) {
        ans = mid;
        end = mid - 1;
      }
      else
        start = mid + 1;
    }
 
    // Return Answer
    return ans;
  }
 
  // Driver function
  public static void Main()
  {
    int N = 3, K = 8;
 
    // Call the function
    // and print the answer
    Console.Write(count_rows(N, K));
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript


输出
4

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

高效方法:上述方法可以使用对[0, 2*n-1]中的行数的值进行二分搜索进行优化。请按照以下步骤解决问题:

  • 初始化变量start = 0、 end = (2 * N) – 1ans = 0。
  • start的值小于时执行以下步骤 结束
    • 计算值等于(start + end) / 2
    • 数星星的数量直到mid
    • 如果到 mid 的星数大于或等于 K,则将 mid 放入变量中并通过end = mid-1向 mid 左侧移动
    • 否则,从中间向右移动start = mid+1
  • 返回ans是所需的值。

下面是上述方法的实现:

C++

// C++ program for the above approach
#include 
using namespace std;
 
// Function to count the number of rows
int count_rows(int n, int k)
{
   
    // A diamond pattern contains
    // 2*n-1 rows so end = 2*n-1
    int start = 1, end = 2 * n - 1, ans = 0;
 
    // Loop for the binary search
    while (start <= end) {
        int mid = start - (start - end) / 2;
 
        int stars_till_mid = 0;
 
        if (mid > n) {
 
            // l_stars is no of rows in
            // lower triangle of diamond
            int l_stars = 2 * n - 1 - mid;
            int till_half = (n * (n + 1)) / 2;
 
            stars_till_mid
                = till_half + ((n - 1) * (n)) / 2
                  - ((l_stars) * (l_stars + 1)) / 2;
        }
        else {
 
            // No of stars till mid th row
            stars_till_mid = mid * (mid + 1) / 2;
        }
 
        // Check if k > starts_till_mid
        if (k <= stars_till_mid) {
            ans = mid;
            end = mid - 1;
        }
        else
            start = mid + 1;
    }
 
    // Return Answer
    return ans;
}
 
// Driver function
int main()
{
    int N = 3, K = 8;
 
    // Call the function
    // and print the answer
    cout << count_rows(N, K);
}

Java

// Java program for the above approach
import java.util.*;
class GFG{
 
  // Function to count the number of rows
  static int count_rows(int n, int k)
  {
 
    // A diamond pattern contains
    // 2*n-1 rows so end = 2*n-1
    int start = 1, end = 2 * n - 1, ans = 0;
 
    // Loop for the binary search
    while (start <= end) {
      int mid = start - (start - end) / 2;
 
      int stars_till_mid = 0;
 
      if (mid > n) {
 
        // l_stars is no of rows in
        // lower triangle of diamond
        int l_stars = 2 * n - 1 - mid;
        int till_half = (n * (n + 1)) / 2;
 
        stars_till_mid
          = till_half + ((n - 1) * (n)) / 2
          - ((l_stars) * (l_stars + 1)) / 2;
      }
      else {
 
        // No of stars till mid th row
        stars_till_mid = mid * (mid + 1) / 2;
      }
 
      // Check if k > starts_till_mid
      if (k <= stars_till_mid) {
        ans = mid;
        end = mid - 1;
      }
      else
        start = mid + 1;
    }
 
    // Return Answer
    return ans;
  }
 
  // Driver function
  public static void main(String[] args)
  {
    int N = 3, K = 8;
 
    // Call the function
    // and print the answer
    System.out.print(count_rows(N, K));
  }
}
 
// This code is contributed by 29AjayKumar

Python3

# python3 program for the above approach
 
# Function to count the number of rows
def count_rows(n, k):
 
        # A diamond pattern contains
        # 2*n-1 rows so end = 2*n-1
    start = 1
    end = 2 * n - 1
    ans = 0
 
    # Loop for the binary search
    while (start <= end):
        mid = start - (start - end) // 2
 
        stars_till_mid = 0
 
        if (mid > n):
 
            # l_stars is no of rows in
            # lower triangle of diamond
            l_stars = 2 * n - 1 - mid
            till_half = (n * (n + 1)) / 2
 
            stars_till_mid = till_half + \
                ((n - 1) * (n)) / 2 - ((l_stars) * (l_stars + 1)) / 2
 
        else:
 
            # No of stars till mid th row
            stars_till_mid = mid * (mid + 1) / 2
 
            # Check if k > starts_till_mid
        if (k <= stars_till_mid):
            ans = mid
            end = mid - 1
        else:
            start = mid + 1
 
        # Return Answer
    return ans
 
# Driver function
if __name__ == "__main__":
 
    N = 3
    K = 8
 
    # Call the function
    # and print the answer
    print(count_rows(N, K))
 
    # This code is contributed by rakeshsahni

C#

// C# program for the above approach
using System;
class GFG{
 
  // Function to count the number of rows
  static int count_rows(int n, int k)
  {
 
    // A diamond pattern contains
    // 2*n-1 rows so end = 2*n-1
    int start = 1, end = 2 * n - 1, ans = 0;
 
    // Loop for the binary search
    while (start <= end) {
      int mid = start - (start - end) / 2;
 
      int stars_till_mid = 0;
 
      if (mid > n) {
 
        // l_stars is no of rows in
        // lower triangle of diamond
        int l_stars = 2 * n - 1 - mid;
        int till_half = (n * (n + 1)) / 2;
 
        stars_till_mid
          = till_half + ((n - 1) * (n)) / 2
          - ((l_stars) * (l_stars + 1)) / 2;
      }
      else {
 
        // No of stars till mid th row
        stars_till_mid = mid * (mid + 1) / 2;
      }
 
      // Check if k > starts_till_mid
      if (k <= stars_till_mid) {
        ans = mid;
        end = mid - 1;
      }
      else
        start = mid + 1;
    }
 
    // Return Answer
    return ans;
  }
 
  // Driver function
  public static void Main()
  {
    int N = 3, K = 8;
 
    // Call the function
    // and print the answer
    Console.Write(count_rows(N, K));
  }
}
 
// This code is contributed by Samim Hossain Mondal.

Javascript


输出
4

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