Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Minimum number of jumps DP
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
#include <bits/stdc++.h> using namespace std; int main() { //code int t; cin>>t; while(t--) { int n; cin>>n; vector<int>v(n); for(int i=0;i<n;i++) cin>>v[i]; vector<int>dp(n); fill(dp.begin(),dp.end(),INT_MAX-2); dp[0]=0; /* leetcode solution int jump(vector<int>& v) { int n=v.size(); vector<int>dp(n,INT_MAX-2); dp[0]=0; int i=0; int j=1; while(j<n) { if(j<=i+v[i]) { dp[j]=min(dp[j],dp[i]+1); j++; } else { i++; } } return dp[n-1]; } */ for(int i=0;i<n;i++) { for(int j=i+1;j<n;j++) { if(j<=i+v[i]) { dp[j]=min(dp[j],dp[i]+1); } } } if(dp[n-1]==INT_MAX-2) { cout<<-1<<endl; } else { cout<<dp[n-1]<<endl; } } return 0; }
g++
Show compiler warnings
[
+
] Compiler args
[
+
]
Show input
edit mode
|
history
|
discussion