给定一个长为L宽为B的矩形,任务是打印可内接在其中的最大三角形的最大整数高度,使得三角形的高度应等于底边的一半。
例子:
Input: L = 3, B = 4
Output: 2
Input: L = 8, B = 9
Output: 4
Input: L = 325, B = 300
Output: 162
朴素的方法:最简单的方法是反向迭代范围[0, min(L, B)] ,如果当前值小于或等于max(L, B) / 2 ,则将当前值打印为答案并打破循环。
时间复杂度: O(min(L, B))
辅助空间: O(1)
二分搜索方法:上述方法可以通过使用二分搜索技术进行优化,并观察到在矩形的最大边长边上选择三角形的底总是最佳的这一事实。请按照以下步骤解决问题:
- 如果L大于B ,则交换值。
- 初始化三个变量,例如,低为0,高为L以在范围[0, L]上执行二分查找。
- 此外,初始化一个变量,比如res为0以存储高度的最大可能长度。
- 在low小于等于high时迭代,执行以下步骤:
- 初始化一个变量,比如mid ,并将其设置为low + (high – low) / 2 。
- 如果mid ≤ B / 2的值,则将mid分配给res ,将mid +1分配给low 。
- 否则,将high设置为mid – 1 。
- 最后,完成上述步骤后,打印res 中得到的值。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the greatest
// altitude of the largest triangle
// triangle that can be inscribed
// in the rectangle
int largestAltitude(int L, int B)
{
// If L is greater than B
if (L > B) {
swap(B, L);
}
// Variables to perform binary
// search
int low = 0, high = L;
// Stores the maximum altitude
// possible
int res = 0;
// Iterate until low is less
// than high
while (low <= high) {
// Stores the mid value
int mid = low + (high - low) / 2;
// If mide is less than
// or equal to the B/2
if (mid <= (B / 2)) {
// Update res
res = mid;
// Update low
low = mid + 1;
}
else
// Update high
high = mid - 1;
}
// Print the result
return res;
}
// Driver Code
int main()
{
// Given Input
int L = 3;
int B = 4;
// Function call
cout << largestAltitude(L, B);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG {
// Function to find the greatest
// altitude of the largest triangle
// triangle that can be inscribed
// in the rectangle
static int largestAltitude(int L, int B)
{
// If L is greater than B
if (L > B)
{
int t = L;
L = B;
B = t;
}
// Variables to perform binary
// search
int low = 0, high = L;
// Stores the maximum altitude
// possible
int res = 0;
// Iterate until low is less
// than high
while (low <= high)
{
// Stores the mid value
int mid = low + (high - low) / 2;
// If mide is less than
// or equal to the B/2
if (mid <= (B / 2))
{
// Update res
res = mid;
// Update low
low = mid + 1;
}
else
// Update high
high = mid - 1;
}
// Print the result
return res;
}
// Driver Code
public static void main(String[] args)
{
// Given Input
int L = 3;
int B = 4;
// Function call
System.out.print(largestAltitude(L, B));
}
}
// This code is contributed by splevel62.
Python3
# Python 3 program for the above approach
# Function to find the greatest
# altitude of the largest triangle
# triangle that can be inscribed
# in the rectangle
def largestAltitude(L, B):
# If L is greater than B
if (L > B):
temp = B
B = L
L = temp
# Variables to perform binary
# search
low = 0
high = L
# Stores the maximum altitude
# possible
res = 0
# Iterate until low is less
# than high
while (low <= high):
# Stores the mid value
mid = low + (high - low) // 2
# If mide is less than
# or equal to the B/2
if (mid <= (B / 2)):
# Update res
res = mid
# Update low
low = mid + 1
else:
# Update high
high = mid - 1
# Print the result
return res
# Driver Code
if __name__ == '__main__':
# Given Input
L = 3
B = 4
# Function call
print(largestAltitude(L, B))
# This ode is contributed by ipg2016107.
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the greatest
// altitude of the largest triangle
// triangle that can be inscribed
// in the rectangle
static int largestAltitude(int L, int B)
{
// If L is greater than B
if (L > B)
{
int t = L;
L = B;
B = t;
}
// Variables to perform binary
// search
int low = 0, high = L;
// Stores the maximum altitude
// possible
int res = 0;
// Iterate until low is less
// than high
while (low <= high)
{
// Stores the mid value
int mid = low + (high - low) / 2;
// If mide is less than
// or equal to the B/2
if (mid <= (B / 2))
{
// Update res
res = mid;
// Update low
low = mid + 1;
}
else
// Update high
high = mid - 1;
}
// Print the result
return res;
}
// Driver Code
public static void Main(string[] args)
{
// Given Input
int L = 3;
int B = 4;
// Function call
Console.Write(largestAltitude(L, B));
}
}
// This code is contributed by code_hunt
Javascript
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the greatest
// altitude of the largest triangle
// triangle that can be inscribed
// in the rectangle
int largestAltitude(int L, int B)
{
// If L is greater than B
if (L > B)
swap(L, B);
// Stores the maximum altitude
// value
int res = min(B / 2, L);
// Return res
return res;
}
// Driver Code
int main()
{
// Given Input
int L = 3;
int B = 4;
// Function call
cout << largestAltitude(L, B);
return 0;
}
Java
// C++ program for the above approach
import java.io.*;
class GFG
{
// Function to find the greatest
// altitude of the largest triangle
// triangle that can be inscribed
// in the rectangle
static int largestAltitude(int L, int B)
{
// If L is greater than B
if (L > B)
{
int t = L;
L = B;
B = t;
}
// Stores the maximum altitude
// value
int res = Math.min(B / 2, L);
// Return res
return res;
}
// Driver Code
public static void main(String[] args)
{
// Given Input
int L = 3;
int B = 4;
// Function call
System.out.print( largestAltitude(L, B));
}
}
// This code is contributed by shivanisinghss2110
Python3
# Python program for the above approach
# Function to find the greatest
# altitude of the largest triangle
# triangle that can be inscribed
# in the rectangle
def largestAltitude( L, B):
# If L is greater than B
if (L > B):
temp = B
B = L
L = temp
# Stores the maximum altitude
# value
res = min(B // 2, L)
# Return res
return res
# Driver Code
# Given Input
L = 3
B = 4
# Function call
print(largestAltitude(L, B))
# This ode is contributed by shivanisinghss2110
C#
// C# program for the above approach
using System;
class GFG
{
// Function to find the greatest
// altitude of the largest triangle
// triangle that can be inscribed
// in the rectangle
static int largestAltitude(int L, int B)
{
// If L is greater than B
if (L > B)
{
int t = L;
L = B;
B = t;
}
// Stores the maximum altitude
// value
int res = Math.Min(B / 2, L);
// Return res
return res;
}
// Driver Code
public static void Main(String[] args)
{
// Given Input
int L = 3;
int B = 4;
// Function call
Console.Write( largestAltitude(L, B));
}
}
// This code is contributed by shivanisinghss2110
Javascript
输出
2
时间复杂度: O(log(min(L, B)))
辅助空间: O(1)
有效的方法:通过观察将三角形的底放在长度max(L, B ) 的边上,可以进一步优化上述方法,最大高度将等于min(max(L, B)/ 2, min(L, B)) 。请按照以下步骤解决问题:
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the greatest
// altitude of the largest triangle
// triangle that can be inscribed
// in the rectangle
int largestAltitude(int L, int B)
{
// If L is greater than B
if (L > B)
swap(L, B);
// Stores the maximum altitude
// value
int res = min(B / 2, L);
// Return res
return res;
}
// Driver Code
int main()
{
// Given Input
int L = 3;
int B = 4;
// Function call
cout << largestAltitude(L, B);
return 0;
}
Java
// C++ program for the above approach
import java.io.*;
class GFG
{
// Function to find the greatest
// altitude of the largest triangle
// triangle that can be inscribed
// in the rectangle
static int largestAltitude(int L, int B)
{
// If L is greater than B
if (L > B)
{
int t = L;
L = B;
B = t;
}
// Stores the maximum altitude
// value
int res = Math.min(B / 2, L);
// Return res
return res;
}
// Driver Code
public static void main(String[] args)
{
// Given Input
int L = 3;
int B = 4;
// Function call
System.out.print( largestAltitude(L, B));
}
}
// This code is contributed by shivanisinghss2110
蟒蛇3
# Python program for the above approach
# Function to find the greatest
# altitude of the largest triangle
# triangle that can be inscribed
# in the rectangle
def largestAltitude( L, B):
# If L is greater than B
if (L > B):
temp = B
B = L
L = temp
# Stores the maximum altitude
# value
res = min(B // 2, L)
# Return res
return res
# Driver Code
# Given Input
L = 3
B = 4
# Function call
print(largestAltitude(L, B))
# This ode is contributed by shivanisinghss2110
C#
// C# program for the above approach
using System;
class GFG
{
// Function to find the greatest
// altitude of the largest triangle
// triangle that can be inscribed
// in the rectangle
static int largestAltitude(int L, int B)
{
// If L is greater than B
if (L > B)
{
int t = L;
L = B;
B = t;
}
// Stores the maximum altitude
// value
int res = Math.Min(B / 2, L);
// Return res
return res;
}
// Driver Code
public static void Main(String[] args)
{
// Given Input
int L = 3;
int B = 4;
// Function call
Console.Write( largestAltitude(L, B));
}
}
// This code is contributed by shivanisinghss2110
Javascript
输出
2
时间复杂度: O(1)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。