PHP程式:到達終點的最小跳躍次數


什麼是PHP?

PHP(超文字預處理器)是一種廣泛使用的伺服器端指令碼語言,用於Web開發。它允許開發人員在HTML檔案中嵌入程式碼,從而建立動態網頁並與資料庫進行互動。PHP以其簡單性、多功能性和與流行資料庫的廣泛整合能力而聞名。它提供了廣泛的擴充套件,並擁有龐大的開發者社群,確保了充足的資源和支援。

PHP程式:到達終點的最小跳躍次數

方法1:樸素遞迴方法

樸素遞迴方法是一種基本演算法方法,透過遞迴地將問題分解成更小的子問題來解決問題。在尋找到達陣列末尾的最小跳躍次數的上下文中,樸素遞迴方法涉及遞迴地探索每個位置的所有可能路徑,並選擇最小的跳躍次數。

示例

<?php
function minJumpsRecursive($arr, $start, $end) {
   // Base case: If the starting index is the last index, no jumps are needed
   if ($start == $end) {
      return 0;
   }
   // If the current element is 0, it is not possible to make any further jumps
   if ($arr[$start] == 0) {
      return PHP_INT_MAX;
   }
 // Initialize the minimum number of jumps to a large value
   $minJumps = PHP_INT_MAX;
   // Try all possible jumps from the current position
   // and choose the one that requires the minimum number of jumps
   for ($i = $start + 1; $i <= $end && $i <= $start + $arr[$start]; $i++) {
      $jumps = minJumpsRecursive($arr, $i, $end);
      if ($jumps != PHP_INT_MAX && $jumps + 1 < $minJumps) {
         $minJumps = $jumps + 1;
      }
   }
   return $minJumps;
}
// Example usage:
$arr = [1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9];
$n = count($arr);
$minJumps = minJumpsRecursive($arr, 0, $n - 1);
if ($minJumps != PHP_INT_MAX) {
   echo "Minimum number of jumps required to reach the end: " . $minJumps;
} else {
   echo "It is not possible to reach the end.";
}
?>

輸出

Minimum number of jumps required to reach the end: 3

方法2:動態規劃

動態規劃是一種用於計算機程式設計的技術,用於透過將複雜問題分解成重疊的子問題並僅解決每個子問題一次來解決複雜問題。它將子問題的解決方案儲存在表或陣列中,以便有效地查詢和重複使用先前計算的結果。這種方法有助於避免冗餘計算並提高演算法的整體效率。

示例

<?php
function minJumpsDynamic($arr, $n) {
   // Create an array to store the minimum number of jumps needed
   $minJumps = array_fill(0, $n, PHP_INT_MAX);
   $minJumps[0] = 0; // Base case: No jumps needed to reach the first element
   // Calculate the minimum number of jumps for each position
   for ($i = 1; $i < $n; $i++) {
      for ($j = 0; $j < $i; $j++) {
         // Check if it is possible to reach position $i from position $j
         if ($j + $arr[$j] >= $i) {
            // Update the minimum number of jumps for position $i
            // by considering the minimum of the current jumps and jumps from position $j plus one
            $minJumps[$i] = min($minJumps[$i], $minJumps[$j] + 1);
         }
      }
   }
   // Return the minimum number of jumps needed to reach the end
   return $minJumps[$n - 1];
}
// Example usage:
$arr = [1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9];
$n = count($arr);
$minJumps = minJumpsDynamic($arr, $n);
if ($minJumps != PHP_INT_MAX) {
   echo "Minimum number of jumps required to reach the end: " . $minJumps;
} else {
   echo "It is not possible to reach the end.";
}
?>

輸出

Minimum number of jumps required to reach the end: 3

結論

總之,用於查詢到達陣列末尾的最小跳躍次數的PHP程式可以使用各種方法實現。樸素遞迴方法探索所有可能的路徑,但它存在指數時間複雜度的問題,對於大型陣列效率不高。另一方面,動態規劃方法透過將問題分解成重疊的子問題並將解決方案儲存在陣列中來最佳化解決方案。這種方法消除了冗餘計算,並顯著提高了演算法的效率,使其適用於更大的陣列。透過利用動態規劃技術,PHP程式可以有效地確定到達陣列末尾所需的最小跳躍次數。

更新於: 2023年8月1日

73 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.